Installing Lighttpd with Python CGI support

Lighttpd is a lightweight web server that is designed for use in high performance environments. However, because it has a relatively low memory footprint and has been optimized to make efficient use of the available resources, it also works well on a low power system such as the Raspberry Pi. Normally server side scripts would use PHP but since python is the scripting language of choice on the Raspberry Pi, particularly when using the GPIO pins, I wanted to configure lighttpd to allow me to use python instead.

Although these instructions were written for a Raspberry Pi using Raspbian they will work on other versions of Debian as well, providing the lighttpd package is included in the distribution.

Install the required packages

First you need to be running as root.

$ su
Password: 

OR

$ sudo -i
Password:


Then start by installing python (which will probably already be installed) and lighttpd.

# apt-get install python
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following extra packages will be installed:
  libpython-stdlib libpython2.7-minimal libpython2.7-stdlib python-minimal
  python2.7 python2.7-minimal
Suggested packages:
  python-doc python-tk python2.7-doc binutils binfmt-support
The following NEW packages will be installed:
  libpython-stdlib libpython2.7-minimal libpython2.7-stdlib python
  python-minimal python2.7 python2.7-minimal
0 upgraded, 7 newly installed, 0 to remove and 0 not upgraded.
Need to get 3799 kB of archives.
After this operation, 15.1 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
  :
  :
  :
Setting up python (2.7.9-1) ...

# apt-get install lighttpd
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following extra packages will be installed:
  libfam0 libterm-readkey-perl libterm-readline-perl-perl
Suggested packages:
  fam rrdtool apache2-utils
Recommended packages:
  spawn-fcgi
The following NEW packages will be installed:
  libfam0 libterm-readkey-perl libterm-readline-perl-perl
  lighttpd
0 upgraded, 4 newly installed, 0 to remove and 0 not upgraded.
Need to get 396 kB of archives.
After this operation, 1,170 kB of disk space will be used.
Do you want to continue [Y/n]? Y
  :
  :
  :
[ ok ] Starting web server: lighttpd.
#

Remember to configure the firewall to allow incoming connections to port 80.

# ufw allow from 192.168.0.0/24 to any port 80 proto tcp
Rule added

Configure the root folder

By default your web pages will be located in /var/www but I prefer to use a separate folder in the home partition instead. This allows me to backup any web pages and scripts with the rest of my user data on the home partition.

# mkdir /home/httpd
# cp -Rv /var/www/* /home/httpd
`/var/www/index.lighttpd.html' -> `/home/httpd/index.lighttpd.html'
# chown -R www-data /home/httpd
# chgrp -R www-data /home/httpd
# vi /etc/lighttpd/lighttpd.conf

# server.document-root        = "/var/www"
server.document-root        = "/home/httpd"

You will need restart the web server for the changes to the configuration file to take effect.

# service lighttpd restart
[ ok ] Stopping web server: lighttpd.
[ ok ] Starting web server: lighttpd.
#

You should now be able to view the default home page on your server using a web browser.

Welcome Page

Enable CGI scripting

To be able to write CGI scripts we need get lighttpd to load the appropriate module by editing the configuration file and adding the extra line shown below.

# vi /etc/lighttpd/lighttpd.conf

#server.modules = (
    "mod_access",
    "mod_alias",
    "mod_compress",
    "mod_redirect",
    "mod_cgi",
    "mod_rewrite",
)

Then to get lighttpd to recognze python scripts we need to add the following new section at the end of the file.

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

Since we specified that all python scripts should go in the cgi-bin folder above we need to create that folder and set the correct permissions.

# mkdir /home/httpd/cgi-bin
# chown www-data /home/httpd/cgi-bin
# chgrp www-data /home/httpd/cgi-bin


Finally we need restart the web server for the changes we made to the configuration file to take effect.

# service lighttpd restart
[ ok ] Stopping web server: lighttpd.
[ ok ] Starting web server: lighttpd.
#

Testing

To test it works create a small test script in the cgi-bin folder.

# vi /home/httpd/cgi-bin/hello.py


#! /usr/bin/python
#
print "Content-Type: text/html\n\n"
print '<html><head><meta content="text/html; charset=UTF-8" />'
print '<title>Raspberry Pi</title><p>'
for count in range(1,100): 
  print 'Hello&nbsp;World... '
print "</p></body></html>"

Browsing to this page should result in the following output.
Welcome Page


Raspberry Pi is a trademark of the Raspberry Pi Foundation

Advertisements
This entry was posted in Debian, Linux, Raspbian, Ubuntu and tagged , , . Bookmark the permalink.

5 Responses to Installing Lighttpd with Python CGI support

  1. Rather than print hello World 100 times is just prints the script. What did I do wrong?
    Jim

    • mike632t says:

      I suspect that the script either isn’t in the cgi-bin folder or isn’t execuitable (chmod +x). When I get some time I’ll see if I can reproduce the problem..

    • mike632t says:

      I’ve managed to reproduced your problem, it looks like your hello.py script isn’t in the cgi-bin sub folder of the web root…

  2. When entering the
    $HTTP[“url”] =~ “^/cgi-bin/” {
    cgi.assign = (“.py” => “/usr/bin/python”)
    }
    command I keep receiving an error -bash: syntax error near unexpected token `(‘
    any ideas?

    • mike632t says:

      Sorry about the delay in looking at this but I think you are entering the text as a command – it should be entered at the end of the lighttpd.conf file

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s