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.3Python 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
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:
The following text should show up (If not, you may have your $PATH environment variable set incorrectly).:
Step 2 - Download and install mod_wsgiThere 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/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:
tar -zxvf mod_wsgi-3.3.tar.gz
sudo make install
ls /usr/libexec/apache2/mod_wsgi.soThe terminal should print back the file path (if not, something went awry, try again):
Step 3 - Test mod_wsgi on ApacheYou 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:
- 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:
- Add the following lines to the bottom of the file and save it:
Options Indexes FollowSymLinks Includes
Allow from all
WSGIScriptAlias / /Library/WebServer/test-cherrypy/wsgi-scripts/test-cherrypy.py
Allow from all
- 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:
- 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):
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:
- 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'),
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!
Step 4 - Download and install CherryPy 3.2.2So, you have mod_wsgi working, now let's hook CherryPy up to it. Download and install CherryPy 3.2.2. Open up your terminal and type the following commands:
cd ~/DownloadsYou now have CherryPy installed! Let's hook it up to your Apache VirtualHost and try it out.
tar -zxvf CherryPy-3.2.2.tar.gz
python setup.py install
- Open up the following file in your favorite text editor:
- Replace the contents of the file with the following code:
sys.stdout = sys.stderr
if cherrypy.__version__.startswith('3.0') and cherrypy.engine.state == 0:
return 'Hello, from CherryPy!'
index.exposed = True
application = cherrypy.Application(Root(), script_name=None, config=None)
Hello, from CherryPy!
Congratulations, you have successfully connected CherryPy to Apache on Snow Leopard using mod_wsgi!
Daniel, you should update for Yosemite, Python 3, ...ReplyDelete