Running Django sites through Upstart

djangoupstart

I host a few Django sites on one of my Ubuntu servers. Up until recently I've been using the Cherokee HTTPd which has the option to launch back-end applications like Django sites but I've just switched out to nginx.

Under cherokee, I just "run" Django sites (sites are stored in /web/):

cd /web/mywebsite/; python manage.py runfcgi workdir=/web/mywebsite method=threaded socket=/web/mywebsite/sock pidfile=/web/mywebsite/pid maxspare=3 maxrequests=500

And then I'd connect to the socket at /web/mywebsite/sock. Cherokee also runs this as a user of my choice, in my case www-data.

This approach has worked well for me under Cherokee but now I'm moving to nginx, I don't have something there holding my hand for process management.

Looking around, there are literally hundreds of different ways of managing this. Init scripts, cron checks, daemontools. But as I'm on Ubuntu (and probably always will be) Upstart seems to make sense… But where do I start?

Could somebody give me an example of a upstart script that runs the above and will respawn it if it dies?

I have several django sites sitting in /web/ (and they're the only directories in that directory) so if there's a cheaty way that I can get this one upstart script to launch them all (with the same settings) and monitor them, that would be super-extra-awesome.

Best Answer

I use same config, /etc/init/django.conf:

description "Django FastCGI /web site"

start on runlevel [2345]
stop on runlevel [!2345]

respawn
exec python /web/manage.py runfcgi socket=//web/mywebsite/sock pidfile=/var/run/django.pid

control:

start: start django or initctl start django
stop: stop django or initctl stop django
restart: restart django or initctl restart django

I recently recorded a video about configure nginx and django(fastcgi and upstart) - http://linux.dyndns-work.com/video/ubuntu_server10.04_install_django_nginx_fastcgi.ogv/

But I prefer nginx and uwsgi(also upstart init script) - no link, i can post a maximum of one hyperlink.