Using Django fastcgi with Upstart

We look after quite a few servers running Django apps, and latterly we’ve been running these on Ubuntu 10.04 LTS, using nginx + fastcgi.

On other systems we’ve used supervisor to launch the Django fastcgi process, but on Ubuntu 10.04, and now on Red Hat/CentOS 6, we have the wonderful upstart system built in which provides everything we need.

Below, you can see the upstart config I’ve written to start fastcgi. It assumes you have python-flup installed. We have one of these per Django project hosted on the machine - you’ll need to vary the port number specified for each project you have. All you’ll need to alter otherwise is the path to run in, and the path to manage.py. If you’re running under EL6, you’ll probably want to change www-data to apache, but beware that I haven’t actually tested this config in such an environment. Save the following in “/etc/init/djangoproject.conf”, where djangoproject is the name of your django project.

description "Basic Django fcgi Upstart config"
author "Sam Bashton"

start on (net-device-up
          and local-filesystems
          and runlevel [2345])
stop on runlevel [016]

respawn
chdir /var/www/djangoproject
exec su -s /bin/sh -c 'exec "$0" "$@"' www-data -- \
  /var/www/djangoproject/manage.py runfcgi \
  method=prefork host=127.0.0.1 port=32769 minspare=4 maxspare=8 \
  maxchildren=128 maxrequests=65535 daemonize=false

Once you’ve added the file, you can start it with the command “start djangoproject”, stop it with “stop djangoproject”, and restart it (for example, if you’ve updated the code) with “restart djangoproject”, replacing djangoproject with the name you gave the config of course. Should the fastcgi process die for some reason it will be automatically be restarted.

  • 2011-08-12 10:20:23 +0100
  • djangoubuntuupstart
comments powered by Disqus