So I have this CGI-BIN application written in C++ that uses several very old libraries. It was originally written for OS/2, then ported to OS X, and now Windows as I switched servers over a number of years.
My new server is running Ubuntu 9.10 (not the server edition.) I initially spent a few days trying to port the CGI app to Linux before finally giving up when the old library code started spitting out thousands of template errors because the old templates didn't match current C++ standards.
While I was working on that, I read this article on Wine which made me think - maybe Wine can run a Windows version of the app under Apache2!
I thought that it might be simpler to get a Windows version running since I had prebuilt versions of the old libraries for Windows. I initially tried with OpenWatcom since the original OS/2 version was developed with OpenWatcom so project files existed, but I ran into insurmountable problems linking. Next I tried Visual Studio 2005 which after recreating the project, compiled and linked right away.
I had installed Apache2 and Wine, and they seemed to work fine, but running my app, I had some problems.
The first problem was running the app at all, because it complained it couldn't load MSVCP80D.dll and MSVCR80D.dll which are the debug runtimes for Visual Studio. I never did figure out how to fix this exact issue, however I did find a solution to a related issue - how to install the Release runtimes. I discovered winetricks which was happy to install the libraries for me:
sudo ./winetricks vcrun2005sp1
Cool, now let's try having apache2 run it! Copied the .exe to /usr/lib/cgi-bin/mywebsite/mycgi.exe and called it from my webpage. Lonnnnnnnng wait. Error!
Ok, so it was probably installing Wine for the www-data user which apache runs under. Cool. It turns out that it installs wine in /var/www/.wine so the question then became, how to debug as that user.
First modify the sudoers file with
sudo visudo
to add the following:
www-data ALL=(ALL) NOPASSWD:ALL
Then you can run as the www-data user with this command
sudo -H -u www-data ./mycgi.exe
Fantastic! Error messages. Turns out, I needed to go back and run winetricks again to install the VS runtime for the www-data user.
sudo -H -u www-data ./winetricks vcrun2005sp1
Crash! The installer couldn't create an X session. Needed permissions for that.
xhost +SI:localuser:www-data
now winetricks runs and installs the dll's for www-data.
xhost -SI:localuser:www-data
to undo the X permissions since they aren't needed anymore.
Fantastic, it was running! But it wasn't loading its configuration file. Turns out that was being placed in Windows Application Data folder, which in this case, ended up in
/var/www/.wine/drive_c/users/Public/Application Data/mycgi/mycgi.ini
(the last two parts of the path are specified by my app.)
Next the cgi data. That needed to be in the c:mycgi folder in Windows, or the
/var/www/.wine/drive_c/mycgi
folder in Wine.
Lastly, undo the edits to the sudoers file.
I glossed over quite a few
sudo chown www-data:www-data *
commands I needed so all my executable and data files including /var/www were owned by apache. I also glossed over the various file permission adjustments, but that shouldn't be anything new if you use Linux.
Final hurdle? It turns out MySQL table names are case sensitive on Linux, so I had to go through and update all the queries to match the database I imported.
You might be wondering how I debugged my app in Release without a debugger, and no console output. The program is built to log to a text file which I could review after each run. Lucky.
And we're done. Hope this helps someone who is trying to do what I thought might never work.
Homepage: www.onemanmmo.com email:one at onemanmmo dot com