Building mod_python into MAMP & OSX

First of all let me apologise to my readers who are probably awaiting the next instalment of my travel/life blog, I will try not to make a habit of posting geeky stuff here but somebody at some point might benefit from this. So if you are not at all geeky then please feel free to delete this without reading it.

Okay warning over, during my travel I have some downtime no doubt and I thought I might put this to some good use by installing mod_python into my MAMP installation, I had no idea at the time what sort of dilemma I was letting myself into but the good news is that I got it done and I am prepared to share how with all of you. Let me just say that this may only work with the specific versions I have noted so don’t blame me if something goes wrong, you should backup anything you feel important of course.

I am running the following

  • MAMP 2.1.1
  • OSX 10.7.5
  • Python 2.6.6 (built into OSX)
  • mod_python 3.3.1

Thanks goto the following in no particular order

Ensure you have Xcode installed and you have the “Command Line Tooles” installed, if you don’t have them installed start Xcode and goto the preferences, click on “Downloads” and install the “Command Line Tools”

Grab & install MAMP, also download the MAMP Components

Grab mod_python and unzip it

Start a terminal type in the following commands

$ ./configure --prefix=/Applications/MAMP/Library --with-apxs=/Applications/MAMP/Library/bin/apxs --with-python=/usr/bin/python

This might fail (it did for me) with a message that looks something similar to this

checking Apache version... cannot open /Applications/MAMP/Library/build/ No such file or directory at /applications/mamp/library/bin/apxs line 218.
cannot open /Applications/MAMP/Library/build/ No such file or directory at /applications/mamp/library/bin/apxs line 218.

So mod_python cannot find the build directory, this is where Nobu’s post gave me a bit of a hint as to what to do and this is why you need the MAMP Components. In your terminal goto the place where you downloaded and unpacked them then run the following commands

$ tar -zxvf unpack httpd-2.2.17
$ cd httpd-2.2.17
$ ./configure --enable-so --prefix=/tmp/httpd-2.2.17
$ make
$ make install
$ cd /tmp/httpd-2.2.17
$ cp -r build include /Application/MAMP/Library/

Now re-try the configure command

$ ./configure --prefix=/Applications/MAMP/Library --with-apxs=/Applications/MAMP/Library/bin/apxs --with-python=/usr/bin/python

With any luck we should now have a configured environment for mod_python, hurrah! lets make it

$ make

Again my hopes were dashed because make failed saying something like

connobject.c: In function ‘_conn_read’:
connobject.c:142: error: request for member ‘next’ in something not a structure or union
apxs:Error: Command failed with rc=65536

This is where Christian’s post came in really useful, you need to patch the mod_python code. I know what you might be thinking “Oh my god do I have to? I’ll break it for sure”. You won’t it’s okay. This is almost a copy and paste from Christian but everything in one place makes life easier I find.

Download this patch for src/connobject.c

Save it to the same directory you saved mod_python (presumably the Downloads directory of OS X)

$ cd ..
$ patch -p0 < mod_python-3.3.1-apr.patch
$ cd mod_python-3.3.1
$ make
$ sudo make install

With any luck that worked.. or did it? Because for me it decided to install the python module in the /tmp/httpd-2.2.17 directory that I had made earlier, so I ran a quick copy on that file

$ cp /tmp/httpd-2.2.17/modules/ /Applications/MAMP/Library/modules

Then to test I edited my MAMP Apache config file

$ vim /Applications/MAMP/conf/apache/httpd.conf

And added the following bits of code in relevant places

LoadModule python_module modules/

<Directory "/Applications/MAMP/htdocs">
    #Lines added to make mod_python work
    AddHandler mod_python .py
    PythonHandler mptest 
    PythonDebug On

I then created a quick python file

$ vim /Applications/MAMP/htdocs/

and dropped in the following content

from mod_python import apache

def handler(req):
    req.content_type = 'text/plain'
    req.write("Hello World!")
    return apache.OK

Restart MAMP and goto http://localhost/, hey presto! Not hard at all right, it’s amazing what you can do in an hour when you can’t sleep and don’t get interrupted.

One thought on “Building mod_python into MAMP & OSX

Leave a Reply

Your email address will not be published.