This is instructions how to setup so you can run python applications through Apache 2 on your MAMP installation. This is instructions on how to install the mod_wsgi module on Mac OS X (Lion), along with command-line tools. Everything will be done in the terminal, except installation of XCode and the command-line tools.
I assume that you already have MAMP installed. I am using MAMP 2.1.2.
Make sure that XCode is installed, and also that the command line tools has been installed (XCode > Preferences > Downloads).
Before we install the module, you will need to have the build for Apache2 for apxs. Unfortunately it is not included by standard code, so we will need to download and compile Apache 2.
# cd ~/downloads/MAMP_components_2.0.2/ # tar xzvf httpd-2.2.17.tar.gz # 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 /Applications/MAMP/Library
Time to install the mod_wsgi, which will let MAMP run python script.
# cd ~/downloads # curl -O https://modwsgi.googlecode.com/files/mod_wsgi-3.4.tar.gz # tar xzvf mod_wsgi-3.4.tar.gz # cd mod_wsgi-3.4 # ./configure --with-apxs=/Applications/MAMP/Library/bin/apxs ; make ; sudo make install # mv /tmp/httpd-2.2.17/modules/mod_wsgi.so /Applications/MAMP/Library/modules/
When it has been installed, you enable it in Apache 2 by doing this:
# echo " LoadModule wsgi_module modules/mod_wsgi.so" >> /Applications/MAMP/conf/apache2/httpd.conf
Or just add LoadModule wsgi_module modules/mod_wsgi.so to your httpd.conf.
You also need to set the script alias in the vhost configuration. Add this to any vhost where you want to enable python:
WSGIScriptAlias / /Applications/MAMP/htdocs/
Here’s a simple test script to see that it works:
import sys def application(environ, start_response): status = '200 OK' output = '' output += 'sys.version = %s\n' % repr(sys.version) output += 'sys.prefix = %s\n' % repr(sys.prefix) response_headers = [('Content-type', 'text/plain'), ('Content-Length', str(len(output)))] start_response(status, response_headers) return [output]
You should see something like this:
sys.version = '2.7.1 (r271:86832, Jun 16 2011, 16:59:05) \n[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)]' sys.prefix = '/System/Library/Frameworks/Python.framework/Versions/2.7'