Install Django on Ubuntu 14.04 with virtualenv, Nginx, Gunicorn and postgres

Update the system first.

jitsejan@jjsvps:~$ sudo apt-get update
jitsejan@jjsvps:~$ sudo apt-get upgrade

Install the virtual environment for Python.

jitsejan@jjsvps:~$ sudo apt-get install python-virtualenv

Create a new environment in a folder of your choice.

jitsejan@jjsvps:~$ ls /opt
jitsejan@jjsvps:~$ sudo virtualenv /opt/env
jitsejan@jjsvps:~$ sudo chown jitsejan /opt/env/

Activate the environment.

jitsejan@jjsvps:~$ source /opt/env/bin/activate

Install Django inside the environment.

(env)jitsejan@jjsvps:~$ pip install django
(env)jitsejan@jjsvps:~$ deactivate env

Install Postgresql on the system.

jitsejan@jjsvps:~$ sudo apt-get install libpq-dev python-dev
jitsejan@jjsvps:~$ sudo apt-get install postgresql postgresql-contrib

Install the Nginx webserver on the system.

jitsejan@jjsvps:~$ sudo apt-get install nginx

Install Gunicorn in the environment.

jitsejan@jjsvps:~$ source /opt/env/bin/activate
(env)jitsejan@jjsvps:~$ sudo pip install gunicorn

Create a database and a user for the project.

jitsejan@jjsvps:~$ sudo -u postgres psql
postgres=# CREATE DATABASE django_db;
postgres=# CREATE USER django_user WITH PASSWORD 'django_pass';
postgres=# GRANT ALL PRIVILEGES ON DATABASE django_db TO django_user;

Create a new project in the environment.

(env)jitsejan@jjsvps:/opt/env$ startproject django_project

Install the Psycopg2 so PostgreSQL can be used in the application.

(env)jitsejan@jjsvps:~$ sudo pip install psycopg2

Add the database details to the

(env)jitsejan@jjsvps:/opt/env/django_project$ nano django_project/

Create the default entries for the application in the database,

(env)jitsejan@jjsvps:/opt/env/django_project$ sudo python syncdb
(env)jitsejan@jjsvps:/opt/env/django_project$ sudo python migrate
(env)jitsejan@jjsvps:/opt/env/django_project$ sudo python makemigrations

Start the Django server.

(env)jitsejan@jjsvps:/opt/env/django_project$ sudo python runserver

Now use Gunicorn to connect to the server.

(env)jitsejan@jjsvps:/opt/env/django_project$ gunicorn --bind django_project.wsgi:application

Create a configuration file for Gunicorn.

(env)jitsejan@jjsvps:/opt/env$ sudo nano

Add the following to the configuration file.

command = '/opt/env/bin/gunicorn'
pythonpath = '/opt/env/django_project'
bind = ''
workers = 3
user = 'jitsejan'

Use the configuration file for starting Gunicorn.

(env)jitsejan@jjsvps:/opt/env/django_project$ gunicorn -c /opt/env/ django_project/django_project.wsgi:application

Create a superuser for Django administration.

(env)jitsejan@jjsvps:/opt/env/django_project$ sudo ./ createsuperuser

Add the STATIC_URL to the

(env)jitsejan@jjsvps:/opt/env/django_project$ nano django_project/
STATIC_URL = '/static/'

Now collect the static data

(env)jitsejan@jjsvps:/opt/env/django_project$ sudo ./ collectstatic

Create a new site in Nginx for the Django project

jitsejan@jjsvps:~$ sudo nano /etc/nginx/sites-available/django_project

Add the following. Change the IP address, the folder for the static files and make sure the port is the same as configured for Gunicorn before.

server {
    server_name 123.456.123.456, *;

    access_log off;

    location /static/ {
        alias /opt/env/django_project/static/;

    location / {
            proxy_set_header X-Forwarded-Host $server_name;
            proxy_set_header X-Real-IP $remote_addr;
            add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';

Enable the site by adding a link in the enabled sites.

jitsejan@jjsvps:~$ sudo ln -s /etc/nginx/sites-available/django_project /etc/nginx/sites-enabled/

Stop Apache and start Nginx.

jitsejan@jjsvps:~$ sudo service apache2 stop
jitsejan@jjsvps:~$ sudo service nginx start

Now run Gunicorn and visit the page in your browser.

(env)jitsejan@jjsvps:/opt/env/django_project$ gunicorn -c /opt/env/ django_project/django_project.wsgi:application

Hopefully the default Django page is shown now.