I've got Lighttpd setup on my Raspberry Pi, but I'd like to get server-side code working now. I'm familiar with PHP, but I figure I should try Python as it's supposed to be the "go to" language for the Raspberry Pi. How can I get Python handling server-side code via Lighttpd?

Mark Ingram
  • 879
  • 3
  • 11
  • 18
  • http://www.penzilla.net/tutorials/python/cgi/ link is broken but thanks for the great writeup. My Pi arrives tomorrow! –  Aug 14 '12 at 00:33

2 Answers2


What you need is CGI support for lighttpd.

Open the lighttpd configuration file (/etc/lighttpd/lighttpd.conf) and uncomment the "mod_cgi" line (remove the # from the beginning of the line if one exists) or add this line if not present.

server.modules = (

Add the following to the bottom of the file:

$HTTP["url"] =~ "^/cgi-bin/" {
        cgi.assign = ( ".py" => "/usr/bin/python" )

Restart the lighttpd daemon:

sudo service lighttpd force-reload

Then create a cgi-bin directory under your webserver's root directory. Any files ending with .py in this directory will be processed by Python.

You can now write Python scripts to handle web requests. You may want to read this tutorial on writing CGI programs with Python.

If on the other hand you would rather use a framework to handle some of the low level details and improve developer productivity, I suggest checking out web.py. You can install it using apt:

sudo apt-get install python-webpy

Lucas at the Cloud 101 Blog has posted a great tutorial on writing web pages using the webpy framework.

  • 103
  • 4
Steve Robillard
  • 34,478
  • 17
  • 103
  • 109
  • Is this FastCGI or CGI? – Mark Ingram Jul 30 '12 at 15:14
  • Note that plain CGI requires starting the python interpreter for every request so it is only well suited for the occasional request. – Thorbjørn Ravn Andersen Aug 14 '12 at 06:06
  • Alas this only worked partially. I can now run python by calling "http://(IP)/cgi-bin/test.py?parameter=xxx" but not "http://(IP)/cgi-bin/test.cgi?parameter=xxx" - the later gives a 404 error... – 576i Feb 05 '14 at 21:40
  • @576i Try changing this line cgi.assign = ( ".py" => "/usr/bin/python" ) to cgi.assign = ( ".cgi" => "/usr/bin/python" ) – Steve Robillard Feb 06 '14 at 00:07
  • @Steve. Alas this does not work. I still get the 404 error, but now "http://(IP)/cgi-bin/test.py?parameter=xxx" will just offer me to download the file. – 576i Feb 18 '14 at 20:08
  • 1
    @576i Rather than continue this discussion in the comments of another question, Please open a new question and include what works and what you have tried. I will then delete these comments. BTW any reason why you can't just go with the .py extension, which you said works? – Steve Robillard Feb 18 '14 at 21:41
  • 404 is returned when the alias definition is missing. Please see my post below. – NDB Oct 26 '17 at 03:53

The accepted answer did not work for me and it also ignores the pre-configured packages that are available for Lighttpd.

The correct way to install Python on Lighttpd for the Raspberry is:

First enable cgi by

sudo lighttpd-enable-mod cgi

This creates a new configuration file for Lighttpd:


Edit the configuration file nano /etc/lighttpd/conf-enabled/10-cgi.conf, to look similar to this

server.modules += ( "mod_cgi" )

$HTTP["url"] =~ "^/cgi-bin/" {
        alias.url += ( "/cgi-bin/" => "/var/www/cgi-bin" )
        cgi.assign = (
                ".py"  => "/usr/bin/python",

Make sure python 2 is installed by executing:

/usr/bin/python --version

Now, restart

sudo /etc/init.d/lighttpd force-reload

Good luck!

  • 269
  • 3
  • 8