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?
2 Answers
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.
-
-
Note that plain CGI requires starting the python interpreter for every request so it is only well suited for the occasional request. Commented Aug 14, 2012 at 6: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...– 576iCommented Feb 5, 2014 at 21:40
-
@576i Try changing this line cgi.assign = ( ".py" => "/usr/bin/python" ) to cgi.assign = ( ".cgi" => "/usr/bin/python" ) Commented Feb 6, 2014 at 0:07
-
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? Commented Feb 18, 2014 at 21:41
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!