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$ django-admin.py 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 settings.py

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

Create the default entries for the application in the database,

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

Start the Django server.

(env)[email protected]:/opt/env/django_project$ sudo python manage.py runserver 0.0.0.0:8080

Now use Gunicorn to connect to the server.

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

Create a configuration file for Gunicorn.

(env)[email protected]:/opt/env$ sudo nano gunicorn_config.py

Add the following to the configuration file.

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

Use the configuration file for starting Gunicorn.

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

Create a superuser for Django administration.

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

Add the STATIC_URL to the settings.py.

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

Now collect the static data

(env)[email protected]:/opt/env/django_project$ sudo ./manage.py 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, *.domain.com;

    access_log off;

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

    location / {
            proxy_pass http://127.0.0.1:8088;
            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/gunicorn_config.py django_project/django_project.wsgi:application

Hopefully the default Django page is shown now.