Apache 2.2 and mod_python on OS X

While working on a forthcoming project (watch this space...), I decided it'd be a lot easier to test the code on my local machine rather than SUCS's server.

Tiger (yes, I'm a Mac user these days) comes with Apache 1.3, which is rather outdated, so I looked into updating to a more recent release. Fink has Apache 2.0, but I decided to go all-out and try and get Apache 2.2 running. It turned out to be relatively easy.

Here's my setup:

First of all, we need to get Apache compiled and installed.

$ wget http://www.mirror.ac.uk/mirror/ftp.apache.org/httpd/httpd-2.2.3.tar.gz
$ tar zxf httpd-2.2.3.tar.gz
$ cd httpd-2.2.3
$ ./configure --with-mpm=worker --enable-so
$ make
$ sudo make install

That should install Apache into /usr/local/apache2.

Next, download and compile mod_python:

$ wget http://www.mirrorservice.org/sites/ftp.apache.org/httpd/modpython/mod_python-3.2.10.tgz
$ tar zxf mod_python-3.2.10.tgz
$ cd mod_python-3.2.10
$ ./configure --with-apxs=/usr/local/apache2/bin/apxs --with-python=/usr/local/bin/python
$ make
$ sudo make install

That'll build mod_python and put the appropriate files in /usr/local/apache2/modules.

Configuring our newly-compiled Apache to replace OS X's default one is easy enough - a few changes to /usr/local/apache2/conf/httpd.conf and extra/httpd-userdir.conf is all it takes. Download an archive with all the necessary patch files here. Note - these configuration changes don't cause Apache 2 to behave exactly like Tiger's Apache 1.3, and there may be parts I've overlooked that could be exploited.

To apply the patches, run the following as root (from the directory with the extracted contents of the archive):

# patch /usr/local/apache2/conf/httpd.conf httpd.conf.patch
# cp httpd-userdir.conf /usr/local/apache2/conf/extra/.

By now, you should have a working Apache 2 setup on your Mac, but how can we make it start at boot? This is relatively easy - we can use /Library/StartupItems to accomplish this. The zip file contains the files you need to put in this directory to automate Apache's startup.

Unzip the archive and then cp -r Apache2 /Library/StartupItems/. as root.

That's it! To get the server started, you can run sudo /sbin/SystemStarter start "Web Server" and then browse to http://localhost/ to test your new Apache installation! And then, of course, you can dive into the world of Python.

Update - 26th Jan '07: I had cause to reinstall Apache on a new machine, and the above instructions work perfectly with Apache 2.2.4, mod_python 3.3.0b and Python 2.5. The only change required, apart from the differences in filenames, is to change /usr/local/bin/python to /usr/local/bin/python2.5 on the configure line of the mod_python build stage.

date: Wed Aug 16 22:00:52 2006 | permalink | tags: osx python howto