Running Python on your OSX Apache server: mod_python
Want to interpret Python scripts in Apache on your OSX installation? Look no further!
After about 4 hours of repeated head-to-wall collisions, I finally managed to get mod_python running with Apache 2.2.14 on my Snow Leopard machine. Here’s what I had to do:
- Use the MacPorts Apache instead of MAMP. I absolutely could not get this to work for MAMP, which might be because MAMP uses version 2.062 of Apache. All the following instructions are for MacPorts installs. Once you install MacPorts, you can install Apache by doing
sudo port install apache
- Now you need mod_python, which is a module for Apache that allows the HTTP server to use the python interpreter “inside” the server. With MacPorts, just do
sudo port install mod_python
If for some reason this doesn’t work, I’ve packaged it up along with the mod_python python package described next. Put it in
- Make sure you have the mod_python Python package (yeah, it’s got the exact same name as the Apache module but is a completely different thing). Get into your Python interpreter and try
If it works, rejoice. If not, I’ve included mine in a zip because I could not for the life of me figure out where to get it from, but I had it in one of my other Python installs. It goes here:
- Edit your httpd.conf file by adding the following:
LoadModule python_module modules/mod_python.so SetHandler mod_python PythonHandler mod_python.publisher PythonDebug On
This first tells Apache to load the module, which is a shared object file (.so). Technical note for the curious: A shared object is a binary that contains compiled functions that can be directly addressed in memory, so you can call functions in it given a header file. It’s similar to a Windows .dll.
Anyway, the rest tells Apache to use mod_python’s VERY handy “publisher” function, described here. This is a REALLY fast and easy way to start writing Python programs for the web. Follow the examples there and you should be good to go! Enjoy!