Setting Up mod_python on an Ubuntu Rackspace Cloud Server

February 6th, 2010 by derek

I just went in on a Rackspace cloud server to do some very simple web serving through python.  The rackspace cloud wiki doesn’t let users edit it, so I will post my solution here.  The instance I used was Ubuntu Jaunty 9.04, but the installation should work for all versions of Ubuntu, and the configuration for all linux-based servers.

mod_python

This is the bare minimum method way of serving up python files via your web server.  It uses only CGI and mod_python, so each python requests causes a fork of the web server.  Not the most efficient, but it works.  See mod_wsgi for a better solution.

This command will install apache and mod_python.  Not all these packages may be necessary, but it worked for me.

sudo aptitude install apache2 apache2.2-common apache2-mpm-prefork apache2-utils libexpat1 ssl-cert libapache2-mod-python

Next, add a handler to your default configuration which you can edit by running

sudo nano /etc/apache2/sites-enabled/000-default

and add the three lines after the comment below

   Options Indexes FollowSymLinks MultiViews
   AllowOverride None
   Order allow,deny
   allow from all
 
# Add these three lines
   AddHandler mod_python .py
   PythonHandler mod_python.publisher
   PythonDebug On

Restart Apache

/etc/init.d/apache2 restart

Make a test file in your www directory

sudo nano /var/www/test.py

And use the following code

def index(req):
 return "Test successful"

Then test it out at http://youripaddress/test.py.  It should display “Test successful” in your browser.

See Also

  • http://cloudservers.mosso.com/index.php/Ubuntu_-_Apache_and_PHP_install