Things about me, the web and the wonderful world of software development.

  • Ack! Looks like Twitter's codes are broken!


Serve Django on Gentoo with lighttpd

In this post you'll find a step by step guide on how you can serve your Django site on Gentoo, using lighthttpd and FastCGI.
Even though this guide is written specifically for Gentoo, it might also be helpful if you use another Linux distribution.

Let's start with installing the required packages. As root, run the following command (flup is required for Django's init.d script)
emerge flup lighttpd
Next, go and grab yourself a copy of Django's InitdScript for Gentoo and give it an appropriate name. Mine is called django-fastcgi. Just to mention it, a version for other Linux distributions is also available here. The script is pretty self-explaining. You only have to change the DJANGO_SITES and SITES_PATH variables. The DJANGO_SITES variable is the name of your Django project, and the SITES_PATH variable is the path to your project (without a trailing slash I think).
Let's assume the absolute path to your project is /var/www/myproject/. The configuration for this setup would look as follows


Now you can go ahead and copy the script to the init.d directory by running
cp scriptname /etc/init.d/
Make sure the script is executable
chmod +x /etc/init.d/scriptname
Try to start the fast-cgi process like this
/etc/init.d/scriptname start
If you don't see any errors showing up, you can add the script to the default runlevel to make it start automatically in case you have to reboot your server. In order to do so, simply run
rc-update add scriptname default
Alright, still with me? Cause we're not done yet.
We can now configure our lighttpd server. With your editor of choice, open up lighty's config file. You can find it in /etc/lighttpd/lighttpd.conf.
Make sure you have the following modules enabled

server.modules = (

Now let's configure lighty to work with our fast-cgi process. Somewhere at the bottom of the configuration file, add the following part

$HTTP["host"] =~ "(^|\.)yourdomain\.com$" {
server.document-root = "path/to/your/django/project" # same path as the SITES_PATH in the fastcgi script
server.errorlog = "/path/to/your/logdir/yourdomain-error.log"
accesslog.filename = "/path/to/your/logdir/yourdomain-access.log"
fastcgi.server = (
"/youdomain.fcgi" => (
"main" => (
"host" => "",
"port" => 3000,
alias.url = (
"/media" => "/usr/lib64/python2.5/site-packages/django/contrib/admin/media/",
"/favicon.ico" => "/path/to/favicon/favicon.ico",
url.rewrite-once = (
"^(/media.*)$" => "$1",
"^/favicon.ico" => "/favicon.ico",
"^(/.*)$" => "/yourdomain.fcgi$1",

Some quick notes about this. The regex pattern at the top makes sure you can use both and to visit the site. The media alias url is used for the Django Admin site and it's media content. So change this path to wherever you have installed the Django source-code. The favicon part is optional. You only have to add that if you actually have a favicon and want to display it. As you can see, there's also another fast-cgi script mentioned in the configuration. We are going to create this file now. Open an editor and put the following line in it

export DJANGOSETTINGSMODULE=yourproject.settings.main

Place this script in your Django project folder. Also make sure it is executable
chmod +x /path/to/your/docroot/yourdomain.fcgi
Now try to start lighttpd
/etc/init.d/lighttpd start
If this works, you can also add lighttpd to the default runlevel
rc-update add lighttpd default
Ok, now you should be able to visit and see your Django application. When I first tried this, I ran into a small problem. Sometimes, the url looked weird because it displayed the name of the fast-cgi script in it. I was able to solve this issue by adding the following line to my file
I hope I didn't forget anything. It's been a while since I set up this environment.

One last note, you could also run the Django fast-cgi process using Unix sockets rather than using a TCP connection. I didn't try it myself but it looks pretty easy. You can find more information about that here.

I hope this post helps some people to get things running correctly.

 Tags: django, gentoo

« Go Back

blog comments powered by Disqus