Running web2py with Lighttpd on Arch Linux

by Sam Burnett on Aug 21, 2011

First, locate your lighttpd documents directory; mine is /srv/http. Extract web2py into that directory, so that it’s located at /srv/http/web2py, and chown everything to whatever user is running lighttpd (http in my case). All scripts and data for web2py will be in this directory.

In /etc/lighttpd/lighttpd.conf, enable the mod_rewrite and mod_fastcgi modules and add the following lines somewhere:

fastcgi.server = (
  "/fcgihandler.py" => (
      "handler_web2py" => ( #name for logs
         "check-local" => "disable",
         "socket" => "/tmp/fcgi.sock",
      )
   ),
)
url.rewrite-once = (
  "^/web2py(/.+?/static/.+)$" => "/web2py/applications$1",
  "^/web2py(/.*)$" => "/fcgihandler.py$1",
)

This tells lighttpd about a FastCGI socket to use for talking to web2py, and directs traffic from /web2py to FastCGI. (/tmp/fcgi.sock is the FastCGI, which we’ll populate later.) Notice that we run web2py from a subdirectory instead of requiring a dedicated server or hostname. To make web2py run properly from this subdirectory, put the following in /srv/http/web2py/routes.py:

routes_in = ((r'/web2py/(?P<a>.*)', r'/\g<a>'),)
routes_out = ((r'/(?P<a>.*)', r'/web2py/\g<a>'),)

Next, create an init script called /etc/rc.d/web2py:

#!/bin/bash

. /etc/rc.conf
. /etc/rc.d/functions

DAEMON=web2py
DAEMON_PATH=/srv/http/web2py/fcgihandler.py
DAEMON_USER=http

PID=$(pgrep -f "^python2 $DAEMON_PATH\$")

case "$1" in
start)
stat_busy "Starting $DAEMON"
[ -z "$PID" ] && sudo -u $DAEMON_USER nohup python2 $DAEMON_PATH >/dev/null 2>&1 &
if [ $? = 0 ]; then
add_daemon $DAEMON
stat_done
else
stat_fail
exit 1
fi
;;
stop)
stat_busy "Stopping $DAEMON"
[ -n "$PID" ] && kill $PID &>/dev/null
if [ $? = 0 ]; then
rm_daemon $DAEMON
stat_done
else
stat_fail
exit 1
fi
;;
restart)
$0 stop
sleep 1
$0 start
;;
*)
echo "usage: $0 (start|stop|restart)"
esac

Add web2py to the DAEMONS line in /etc/rc.conf. This init script will start a FastCGI server to handle web2py requests.

Reload the init scripts:

/etc/rc.d/web2py start
/etc/rc.d/lighttpd reload

Web2py should now be accessible at http://<yourhostname>/web2py.

Adminstration

Web2py doesn’t let you run the adminstration interface over HTTP. As a workaround, you can run a second, localhost-only copy:

sudo -u http python2 /srv/http/web2py/web2py.py

You can the access the administration interface at http://localhost:8000/admin.

Send Questions and Comments to Sam Burnett.