Wednesday, August 7, 2013

Django - Using Upstart to run Django built-in dev server

Sometimes, You only want to have a small django server to run a not-important-app, and you dont want to install any extra stuff. So, my solution is just use the Django built-in development app and make it start as a service at startup with Upstart (in Ubuntu).

Here is how:



1. Create a bash script to start django dev server

/var/www/MyDjangoProject/django_startup.sh:

#!/bin/bash
. ~/.venv/myvirtualenv/bin/activate
cd /var/www/MyDjangoProject
./manage.py runserver 0.0.0.0:8000




2. Create a file name django.conf in /etc/init/


# django - start django server#

description     "start django server"

start on startup       
stop on shutdown    

respawn

exec /var/www/MyDjangoProject/django_startup.sh



3. Create a symlink of upstart-job in /etc/init.d/

ln -s /lib/init/upstart-job /etc/init.d/django


 4. Start the job

$sudo start django


By this way, we will not see the output messages of the Django dev server. That's cool!

Read the Upstart cookbook for more understanding: http://upstart.ubuntu.com/cookbook/