All posts All posts by this author Paper color Change page color Announcements

Django + Gunicorn + Nginx configuration on Ubuntu 12.10

What could be the easiest way to deploy Django project with nginx. One possible
choice could be Gunicorn. Gunicorn is a Python WSGI HTTP Server for UNIX.

Considering you have Django and Nginx installed.

Create a new project:

~$ django-admin.py startproject project_name
~$ cd project_name

Wodnload and extract Gunicorn:

~/project_name$ wget -c http://pypi.python.org/packages/source/g/gunicorn/gunicorn-0.17.2.tar.gz
~/project_name$ tar -zxvf gunicorn-0.17.2.tar.gz
~/project_name$ cp -r gunicorn-*/gunicorn .

Add gunicorn to settings.py

~/project_name$ vi project_name/settings.py

In the INSTALLED_APPS tuple add gunicorn so it looks like

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # Uncomment the next line to enable the admin:
    # 'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    # 'django.contrib.admindocs',
    'gunicorn'
)

Now write a shell script

~/project_name$ vi gunicorn-server.sh

Add folling to the file

#!/bin/sh

FILE=`readlink -f $0`
DIRNAME=`dirname $FILE`

PIDFILE=$DIRNAME/server.pid

echo $PIDFILE

ADDRESS=127.0.0.1:8080
LOGFILE=$DIRNAME/server.log
LOGDIR=$(dirname $LOGFILE)
NUM_WORKERS=3
case "$1" in
    "stop" )
        if [ -f $PIDFILE ]; then
            kill `cat -- $PIDFILE`
            rm -f -- $PIDFILE
        fi
    ;;
    "" | "fg" )
        if [ -f $PIDFILE ]; then
            kill `cat -- $PIDFILE`
            rm -f -- $PIDFILE
        fi

        if [ "$1" = "fg" ]; then
            $DIRNAME/manage.py run_gunicorn --bind=$ADDRESS \
                --workers=$NUM_WORKERS --log-level=debug \
                --log-file=$LOGFILE 2>>$LOGFILE
        else
            $DIRNAME/manage.py run_gunicorn --bind=$ADDRESS \
                --pid=$PIDFILE --workers=$NUM_WORKERS \
                --log-level=debug --log-file=$LOGFILE \
                2>>$LOGFILE --daemon
        fi
esac

Nginx configuration:

~/project_name$ sudo vi /etc/nginx/sites-available/default

Add following at the bottom of the file

upstream project_server {
    server localhost:8080 fail_timeout=0;
}
server {
    listen 80;
    server_name project_name;
    location / {
    #   try_files $uri $uri/ /index.html;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        if (!-f $request_filename) {
            proxy_pass http://project_server;
            break;
        }
    }
}

Edit host file:

~/project_name$ sudo vim /etc/hosts

Add the line

127.0.0.1 project_name

Restart Nginx

~/project_name$ sudo /etc/init.d/nginx restart

Run the shell script

~/project_name$ chmod 775 gunicorn-server.sh
~/project_name$ ./gunicorn-server.sh

On your browser address bar put

http://project_name

It Works !!

Software Solutions