How to use Lighttpd to serve static media files for Django CMS

Prerequisities

  • lighttpd with fastcgi
  • python (2.5 tested)
  • django
  • django-cms
  • flup(?)

How it's set up

The fcgi is running through python and flup on server 127.0.0.1 and port 7777 and is served by lighttpd.

You can customize it to your needs, this is just an example of a config that works.

The /static and /media directories are also served by lighttpd, in order to speed things up.

The required /etc/lighttpd/lighttpd.conf snippet

#in server.modules, the "mod_fastcgi" must be enabled

$HTTP["host"] =~ "^example\.com$" {server.document-root = "/var/www/django-cms/"

fastcgi.server =  ("/mysite.fcgi" =>  ("main" => ("host" =>
"127.0.0.1", "port" => 7777, "check-local" => "disable",)),)
alias.url = ("/media/" => "/var/www/django-cms/media/admin/",
		"/static/" => "/var/www/django-cms/static/",)
url.rewrite-once = (
		"^(/media.*)$" => "$1",
		"^(/static.*)$" => "$1",
		"^/favicon\.ico$" => "/media/favicon.ico",
		"^(/.*)$" => "/mysite.fcgi$1",)
}

Example starting daemon script (for Fedora Linux)

This needs to be placed be placed into

/etc/init.d/d-cms-daemon

to have it all start up at boot time.

Place this at the start of /etc/init.d/d-cms-daemon

# Replace these settings.
PROJDIR="/var/www/django-cms/"
PIDFILE="/var/run/d-cms-daemon.pid"
HOST=127.0.0.1
PORT=7777

#
# d-cms-daemon   This scripts turns d-cms-daemon on
#
# Author:       A.M. <nospam@mosi.sk>
#
# description:  d-cms-daemon is a django-cms enabled site with a fcgi
daemon, this script serves the starting/stopping the daemon
# processname:  d-cms-daemon
# pidfile: /var/run/d-cms-daemon.pid
#

# source function library
. /etc/rc.d/init.d/functions

RETVAL=0

start() {
	echo -n $"Starting d-cms-daemon: "
	# this script starts the daemon
	exec /bin/env python $PROJDIR/manage.py runfcgi protocol=fcgi host=
$HOST port=$PORT method=prefork maxspare=2 maxchildren=3 pidfile=
$PIDFILE
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/d-cms-daemon
}

stop() {
	echo -n $"Stopping d-cms-daemon: "
	cd $PROJDIR
	if [ -f $PIDFILE ]; then
			kill `cat -- $PIDFILE`
			rm -f -- $PIDFILE
	fi
	echo
	[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/d-cms-daemon
}

restart() {
	stop
	start
}

case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  restart|force-reload|reload)
	restart
	;;
  condrestart)
	[ -f /var/lock/subsys/d-cms-daemon ] && restart
	;;
  status)
	status d-cms-daemon
	RETVAL=$?
	;;
  *)
	echo $"Usage: $0 {start|stop|status|restart|reload|force-reload|
condrestart}"
	exit 1
esac

exit $RETVAL

supervisord

There is also a nice utility - supervisord - for supervising if a program is running and restarting the program if it crashes.