Tell me more ×
Raspberry Pi Stack Exchange is a question and answer site for users and developers of hardware and software for Raspberry Pi. It's 100% free, no registration required.

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?

share|improve this question
penzilla.net/tutorials/python/cgi link is broken but thanks for the great writeup. My Pi arrives tomorrow! – user1135 Aug 14 '12 at 0:33

1 Answer

up vote 12 down vote accepted

What you need is CGI support for lighthttpd.

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 creatge 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.

share|improve this answer
Is this FastCGI or CGI? – Mark Ingram Jul 30 '12 at 15:14
Mark I had some trouble verifying the steps needed for fastCGI so have fallen back to CGI. – Steve Robillard Jul 30 '12 at 15:58
OK, thanks Steve, I'm sure it'll be fine (CGI vs FastCGI) as it's only for a home project :). – Mark Ingram Jul 30 '12 at 17:04
Brilliant, just tried it now. Thanks Steve! – Mark Ingram Jul 30 '12 at 20:23
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 6:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.