A word of caution:
This tutorial requires you to modify sensitive system files on your computer. If you are uncomfortable doing this or unaware of the repercussions, take some time and do your due-diligence to understand the repercussions before following these steps or do not follow the steps outlined in this tutorial.Step 1 (optional) - Download and install Python 2.7.3
Python is installed on SnowLeopard by default, but it's an older version 2.6. If you want to use that, you can skip this section.- Download Python 2.7.3 from the Python standard releases page
- Browse to the downloaded python-2.7.3-macosx10.6.dmg file in Finder
- Double-click on it and follow the installation instruction
/Library/Frameworks/Python.framework/Versions/2.7/bin
Add the above location to your $PATH environment variable.
Python 2.7.3 should now be up-and-running. To test this, open a terminal and type:
python -V
The following text should show up (If not, you may have your $PATH environment variable set incorrectly).:
Python 2.7.3
Step 2 - Download and install mod_wsgi
There are a few ways to do this. If you are sticking with Python 2.6 that comes pre-installed on Snow Leopard, you can download and install the Snow Leopard binary. If you have macports or homebrew you can use one of those if you prefer. Here, I will demonstrate how to compile it from source. It's quick, easy and puts the resulting mod_wsgi.so file in the same place as all of the other Apache mods.To download and compile from source, open up a terminal and type the following commands (Hit the [return] key after each command, you will see output from some of them. Also, the terminal may ask for your password at some point):
cd ~/Downloads/
wget http://modwsgi.googlecode.com/files/mod_wsgi-3.3.tar.gz
tar -zxvf mod_wsgi-3.3.tar.gz
cd mod_wsgi-3.3
sudo ./configure
sudo make
sudo make install
Now, you should have mod_wsgi.so installed with your other Apache modules in /usr/libexec/apache2. To confirm this, keep the terminal open and type the command:
ls /usr/libexec/apache2/mod_wsgi.so
The terminal should print back the file path (if not, something went awry, try again):
/usr/libexec/apache2/mod_wsgi.so
Step 3 - Test mod_wsgi on Apache
You have downloaded mod_wsgi, now let's get Apache to load it.First, you need to edit some of the Apache configuration files. FYI, you will need to make hidden files and folders visible if they aren't already.
- Tell Apache to load the mod_wsgi.so file that you installed. Open the following file in your favorite text editor:
Macintosh HD/private/etc/apache2/httpd.conf
- Find the big list of lines that begin with LoadModule. After the last LoadModule line, add the following line and save the file. You will be prompted for your password when saving. (Note, if you used macports or homebrew to install mod_wsgi, the location of mod_wsgi.so will be different):
LoadModule wsgi_module libexec/apache2/mod_wsgi.so
- Now, create a VirtualHost in Apache. Open up the following file in your favorite text editor:
Macintosh HD/private/etc/apache2/extra/httpd-vhosts.conf
- Add the following lines to the bottom of the file and save it:
<VirtualHost *:80>
ServerAdmin me@test-cherry-py
ServerName test-cherrypy
DocumentRoot "/Library/WebServer/test-cherrypy/documents"
<Directory "/Library/WebServer/test-cherrypy/documents">
Options Indexes FollowSymLinks Includes
AllowOverride All
Order allow,deny
Allow from all
</Directory>
WSGIScriptAlias / /Library/WebServer/test-cherrypy/wsgi-scripts/test-cherrypy.py
<Directory "/Library/WebServer/test-cherrypy/wsgi-scripts">
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
- Fake the domain name. In Finder, open up the file Macintosh HD/etc/hosts in your favorite text editor and add the following line to the end of the file:
127.0.0.1 test-cherrypy
- Create the directories and files that your VirtualHost directive above refers to. Go to the terminal and type the following commands (hit [return] after each command):
cd /Library/Webserver
sudo mkdir test-cherrypy
sudo mkdir test-cherrypy/documents
sudo mkdir test-cherrypy/wsgi-scripts
sudo touch test-cherrypy/wsgi-scripts/test-cherrypy.py - In Finder, open up the following file in your favorite text editor:
Macintosh HD/Library/Webserver/test-cherrypy/wsgi-scripts/test-cherrypy.py
- It should be empty. Add the following lines of code to the file and save it:
def application(environ, start_response):
status = '200 OK'
output = 'Hello, from mod_wsgi!'
response_headers = [('Content-type', 'text/plain'),
('Content-Length', str(len(output)))]
start_response(status, response_headers)
return [output]
sudo apachectl restart
Open up your favorite browser and go to http://test-cherrypy. You should be greeted with the following text:
Hello, from mod_wsgi!