Michael Crosby

web.py server setup

Python is my favorite language to work with. It is so versatile. I can automate tasks, write quick scripts, process data, and even run a web application. It is very portable and I love the syntax. For a web framework I use web.py because it stays out of your way and does not impose itself on your application. With other frameworks you have to do it their way or struggle the entire time. It's awesome but setting up a "production" server is a bitch. Tutorials suck and getting Python to run under Apache is a waste of 6+ hours with all the config bullshit. Leave it and do it my way.

What you will learn:

  • Install lighttpd on Ubuntu/Debian
  • Install MySQL
  • Setup web.py to run under lighttpd
  • Setup MySQLDB for Python to MySQL

Ubuntu Server

Ubuntu or any Debian distro will work with this tutorial. I am currently running my server on Ubuntu Server 11.04. You need to make sure that Apache is not installed on your server. If it is remove everything Apache. Make sure you have shell plus access to install software on your server.


Now we will need to install MySQL on your server before we do anything else. Just skip this part if you don't use MySQL. This is very easy, just run these two commands to install MySQL and the development libraries that Python needs.

apt-get install mysql-server libmysqlclient-dev

Remember that you should be running these as root or sudo. If you didn't know this just give up now. You will also need to setup your MySQL username and permissions for your web application to use. I am not going to go over this process because I am lazy. Google it if you don't know how.


Now we can install the lighttpd server.

apt-get install lighttpd

You can go ahead and browse to the site to see if lighttpd is functioning properly.

Python Build Tools and MySQLDB

With our web server and MySQL database setup on the server it is time to get our Python environment setup. Start by installing the buildtools and other Python development tools.

apt-get install python-setuptools python-dev build-essential

This will install "easy_install" and other tools so we can build the python MySQL driver. After this is complete we need to download and install the MySQLDB driver. This is easy if you have the libmysqlclient-dev and setuptools installed.

wget http://downloads.sourceforge.net/project/mysql-python/mysql-python/1.2.3/MySQL-python-1.2.3.tar.gz
tar -zxf MySQL-python-1.2.3.tar.gz

This will download and unzip the driver. Now "cd" in to the folder and run these two commands:

python setup.py build
python setup.py install


All we have left to do is install web.py, flup, and configure lighttpd for our application. The first two are easy.

easy_install web.py
easy_install flup

Now lets configure lighttpd.

cd /etc/lighttpd/

Replace the contents of lighttpd.conf with the text below:

server.modules = (

server.document-root        = "/var/www"
server.upload-dirs          = ( "/var/cache/lighttpd/uploads" )
server.errorlog             = "/var/log/lighttpd/error.log"
server.pid-file             = "/var/run/lighttpd.pid"
server.username             = "www-data"
server.groupname            = "www-data"

## Use ipv6 if available
#include_shell "/usr/share/lighttpd/use-ipv6.pl"

compress.cache-dir          = "/var/cache/lighttpd/compress/"
compress.filetype           = ( "application/x-javascript", "text/css", "text/html", "text/plain" )

include_shell "/usr/share/lighttpd/create-mime.assign.pl"
include_shell "/usr/share/lighttpd/include-conf-enabled.pl"

server.modules   += ( "mod_fastcgi" )
server.modules   += ( "mod_rewrite" )

 fastcgi.server = ( "/index.py" =>
 ("/" => ( "socket" => "/tmp/fastcgi.socket",
    "bin-path" => "/var/www/index.py",
    "max-procs" => 1,
   "bin-environment" => (
     "REAL_SCRIPT_NAME" => ""
   "check-local" => "disable"

 url.rewrite-once = (
   "^/favicon.ico$" => "/static/favicon.ico",
   "^/static/(.*)$" => "/static/$1",
   "^/(.*)$" => "/index.py/$1",

Note the server.username and server.groupname. This user/group will need proper permissions to your application files. Study the config above. Everything is simple and you should be able to change whatever settings that you need.

Make sure you updated the server.document-root path to where the location that you are going to be placing your application in. I just use /var/www for my applications. This path will need to be updated in the fastcgi.server section along with your main python file for web.py. I usually call my main application file "index.py" and the above config is setup to reflect that.

Now that we have lighttpd set up we can go into our /var/www directory and set up a sample web.py application ( if you do not already have a working app ). We can just use this simple template. Make a note of the shebang at the top of the file. You will need to add that to your application file. Just save the file as "index.py".

#!/usr/bin/env python

import web

urls = ('/(.*)', 'Index')

application = web.application(urls, globals())
web.config.debug = True

class Index:

    def GET(self, name=''):
        return 'Hello World'

    def POST(self, name=''):
        return 'Hello World'

if __name__ == '__main__':application.run()

Make the file's owner www-data and make it executable. After that restart lighttpd.

chown www-data:www-data index.py
chmod +x index.py
service lighttpd restart

Go ahead and navigate to your site in your browser and you should see "Hello World". If you do not, cat out the lighttpd error log at:

cat /var/log/lighttpd/error.log

That's it. I hope this helps to save hours of your life, hours that I wasted trying to get web.py to run under Apache bullshit. This is the way to go, lighttpd is fast, easy to config, and perfect for web.py.

comments powered by Disqus