How to setup django with lighttpd?

I have a live website that is running under lighttpd. I now have a sub-domain that I want to run under django. I do not want to move to Apache as I don't currently have the resources to support two production web servers. The django part is actually working well with the django builtin development server so now I just need to port it into production.

It seems that all documentation to setup django with lighttpd is over 10 years old and uses fastcgi that from what I can see was deprecated and removed from django. The new docs point to use wscgi - scgi under lighttpd. But even that is being hard to get any documentation.

Do I need to run uwsgi and use that to serve the pages to mod_scgi? Or should I use gunicorn? I am sort of at a loss on what to do.

Suggestions welcome!

There are a few different options you can consider for serving a Django application under Lighttpd. One popular option is to use mod_proxy to forward requests to a separate process running your Django application, such as Gunicorn or uWSGI.

Gunicorn can be easily integrated with Lighttpd using mod_proxy. You can run Gunicorn in the background as a daemon, and configure Lighttpd to forward requests to it using mod_proxy

uWSGI can also be integrated with Lighttpd using mod_proxy and the main advantage of uWSGI over Gunicorn is its scalability and performance, but it can be more complex to set up.

And if you are new to this, I would recommend trying to set up Gunicorn first as it is the simpler option, and then moving on to uWSGI if you need more performance!

UPDATE: As you mentioned in the comment, you want to use mod_fastcgi to connect Lighttpd to your Django application, so you will need to start Django using the FastCGI process manager provided by flup.

How to start your Django app using flup:

#!/bin/bash

# activate your virtual environment
source /path/to/venv/bin/activate

# Start the FastCGI process manager
python -m flup.server.fcgi -d -m django.core.servers.fastcgi

You will also need to configure Lighttpd to use mod_fastcgi and point it to the socket created by flup in order to connect to the Django application

fastcgi.server = ( "/" =>
    ( "localhost" =>
        (
            "socket" => "/tmp/fcgi.sock",
            "check-local" => "disable",
        )
    )
)

lighttpd supports uwsgi with mod_scgi and scgi.protocol = "uwsgi"

scgi.protocol = "uwsgi"
scgi.server = ( "/" =>
    (
        (
            "socket" => "/tmp/scgi.sock",
            "check-local" => "disable",
        )
    )
)

Using mod_scgi and the uwsgi protocol to communicate with the Django server will likely be faster and use fewer resources than using gunicorn (and communicating using lighttpd mod_proxy)

How to use Django with uWSGI

The uWSGI docs linked there recommend configuring the web server to serve static files rather than sending those requests to Django.

Back to Top