Mod_Python and on Ubuntu



First install mod_python for Apache and then restart/reload the server.

apt-get install libapache2-mod-python
/etc/init.d/apache2 force-reload
apache2ctl restart
Next grab the framework from You can grab the tar or use easy_install depending on your setup.

tar xzvf
cd web
sudo python install

or if you use easy_install


Download this file This python package can (with this version) go anywhere in your python path "sys.path". However, I placed it in the wsgiref directory. I'm running python2.6 so I did the following

after downloading

mv /usr/lib/python2.6/wsgiref/

It's important to remember that in the VirtualHost file you create the PythonHandler directive will need to reference this module. If you place it outside of the wsgiref directory remember to change the directive as well.

The VirtualHost

Now we need to create a VirtualHost for our application. You can run apps without apache and this is good for development mode. To do this the command is

python 4567
where the is your application filename and the 4567 is the port number you want to start the server on. I think it defaults to 8080. If you're running apache or another web server port 80 will be in use. This is where mod_python (or cgi or fastcgi) come into play.

<VirtualHost *>
  DocumentRoot /var/www/python/myapp

   #Aliases can trip you up! Pay attention here
   Alias /myapp /var/www/python/myapp
   <Directory /var/www/python/myapp>
      <IfModule python_module>
        PythonPath "sys.path +['/var/www/python/myapp']"
        AddHandler python-program .py
        # modpython_gateway is a file you'll need to download and place in wsgiref
        PythonHandler wsgiref.modpython_gateway::handler
        # This is your python program
        PythonOption wsgi.application pcode::main
        PythonOption SCRIPT_NAME /myapp
        PythonDebug on
</VirtualHost> Application applications can be very simple. Everything can be placed in one file. In this example I created a file "" and saved it to "/var/www/python/myapp/". The contents look like this

import web #this is the frameword
web.webapi.internalerror = web.debugerror #lets capture errors for debugging

# map paths to classes
urls = (
    "/", "hello"

# this is the main from the PythonOption wsgi.application pcode::main from our VirtualHost
main = web.application(urls, globals()).wsgifunc()

class hello:
    def GET(self):
        #  web.header... otherwise you'll be prompted to save the file rather than view it in the browser
        yield "Finally got to work!"

if __name__ == "__main__":

Save the file, reload apache and visit the location on the web (In my case Remember to include that last "/" after the You might get a "not found" message from otherwise.

Pretty URLs

You can get pretty urls like "" if you use the mod_rewrite apache module.

a2enmod rewrite
You can either set up the rewrite rules in your VirtualHost or in a .htaccess file. I'll do the .htaccess so I don't have to reload the server for the changes to take effect.

In the same directory as your file create a file ".htaccess" and in it place this code

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteBase /
  RewriteCond %{REQUEST_URI} !^/icons
  RewriteCond %{REQUEST_URI} !^/favicon.ico$
  RewriteCond %{REQUEST_URI} !^(/.)
  RewriteRule ^(.)$$1 [PT]


I ran into some trouble with the Alias in the VirtualHost file. It looks like this

Alias /myapp /var/www/python/myapp
This will map incoming urls to the full path on the filesystem so that your python program will run properly. I kept getting the not found error message from without it. There might be a better way to set this up but this is how I got it to work.

Here are some additional resources I found helpful

Tagged w/ #.htacess #apache #framework #modpython_gateway #mod_python #mod_rewrite #python #web.pypython

Apps I've Built

Easily change the location on your photos and videos
Click here for more info about the app
Easily change the dates and times on your photos and videos
Click here for more info about the app
English dictionary with notifications so you won't forget what you're studying!
Click here for more info about the app
The app that quizzes and scores you on your vocabulary!
Click here for more info about the app