28

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
  • 889
  • 3
  • 11
  • 18

2 Answers2

18

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 = (
            "mod_access",
            "mod_alias",
            "mod_accesslog",
            "mod_auth",
            "mod_ssi",
            "mod_cgi",
            "mod_compress",
            "mod_fastcgi",
            "mod_rewrite",
            "mod_magnet",
)

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.

Bozzy
  • 103
  • 4
Steve Robillard
  • 34,988
  • 18
  • 106
  • 110
3

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:

/etc/lighttpd/conf-enabled/10-cgi.conf

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!

NDB
  • 269
  • 3
  • 8