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

Update the system first.

[email protected]:~$ sudo apt-get update
[email protected]:~$ sudo apt-get upgrade

Install the virtual environment for Python.

[email protected]:~$ sudo apt-get install python-virtualenv

Create a new environment in a folder of your choice.

[email protected]:~$ ls /opt
[email protected]:~$ sudo virtualenv /opt/env
[email protected]:~$ sudo chown jitsejan /opt/env/

Activate the environment.

[email protected]:~$ source /opt/env/bin/activate

Install Django inside the environment.

(env)[email protected]:~$ pip install django
(env)[email protected]:~$ deactivate env

Install Postgresql on the system.

[email protected]:~$ sudo apt-get install libpq-dev python-dev
[email protected]:~$ sudo apt-get install postgresql postgresql-contrib

Install the Nginx webserver on the system.

[email protected]:~$ sudo apt-get install nginx

Install Gunicorn in the environment.

[email protected]:~$ source /opt/env/bin/activate
(env)[email protected]:~$ sudo pip install gunicorn

Create a database and a user for the project.

[email protected]:~$ 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)[email protected]:/opt/env$ startproject django_project

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

(env)[email protected]:~$ sudo pip install psycopg2

Add the database details to the

(env)[email protected]:/opt/env/django_project$ nano django_project/

Create the default entries for the application in the database,

(env)[email protected]:/opt/env/django_project$ sudo python syncdb
(env)[email protected]:/opt/env/django_project$ sudo python migrate
(env)[email protected]:/opt/env/django_project$ sudo python makemigrations

Start the Django server.

(env)[email protected]:/opt/env/django_project$ sudo python runserver

Now use Gunicorn to connect to the server.

(env)[email protected]:/opt/env/django_project$ gunicorn --bind django_project.wsgi:application

Create a configuration file for Gunicorn.

(env)[email protected]:/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)[email protected]:/opt/env/django_project$ gunicorn -c /opt/env/ django_project/django_project.wsgi:application

Create a superuser for Django administration.

(env)[email protected]:/opt/env/django_project$ sudo ./ createsuperuser

Add the STATIC_URL to the

(env)[email protected]:/opt/env/django_project$ nano django_project/
STATIC_URL = '/static/'

Now collect the static data

(env)[email protected]:/opt/env/django_project$ sudo ./ collectstatic

Create a new site in Nginx for the Django project

[email protected]:~$ 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.

[email protected]:~$ sudo ln -s /etc/nginx/sites-available/django_project /etc/nginx/sites-enabled/

Stop Apache and start Nginx.

[email protected]:~$ sudo service apache2 stop
[email protected]:~$ sudo service nginx start

Now run Gunicorn and visit the page in your browser.

(env)[email protected]:/opt/env/django_project$ gunicorn -c /opt/env/ django_project/django_project.wsgi:application

Hopefully the default Django page is shown now.