Configure Django with PostgreSQL + Gunicorn + Nginx

I’ve just finish to setup my private web portal (currently under maintenance) on DigitalOcean, and below are step by step to setup VPS until my django app go live.

FYI, I’m using ubuntu 14.04 on my VPS because I experienced only with ubuntu. 😀

In this post I assumed that you already know how to setup your python environment (setup python, virtualenv & pip). Here we go:

Initial Server Setup:

$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get install libpq-dev python-dev
$ sudo apt-get install postgresql postgresql-contrib
$ sudo apt-get install nginx

Installing Python Dependency:

$ pip install django
$ pip install gunicorn
$ pip install psycopg2

Configure PostgreSQL:

$ sudo -iu postgres
$ createdb your_database_name
$ psql
$ CREATE USER your_username WITH PASSWORD 'your_password'
$ GRANT ALL PRIVILEGES ON DATABASE "your_database_name" to your_username

Configure Gunicorn:
What you need to do is create gunicorn config file, you can put anywhere,
but i put it within my virtualenv or your python installation path.
For example “~/.virtualenvs/my-virtualenv/”
Here is the configuration:

command = '/your_python_env/bin/gunicorn'    # Gunicorn path within python installation
pythonpath = '/your_project_path/myproject'  # Your project path.
bind = ''                      # If you have multiple project, just increment the port.
workers = 3                                  # adjust workers with what you need.
user = 'your_user'                           # adjust with your VPS user.

Then you can run your gunicorn with this command within your project directory.

$ /your_python_env/bin/gunicorn -c /your_python_env/ myproject.wsgi

Configure Nginx:
Create nginx config within “/etc/nginx/sites-available/” for instance “/etc/nginx/sites-available/mywebsite” then add the following to configuration file.

server {

    access_log off;

    location /static/ {
        alias /opt/myenv/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"';

Now, change directory to “/etc/nginx/sites-enabled

$ cd /etc/nginx/sites-enabled

Then create symbolic link to config file that created before.

$ sudo ln -s /etc/nginx/sites-available/mywebsite

Last step, what you need todo just restart your nginx service.

$ sudo service nginx restart

Below is how to run gunicorn at startup and it will run automatically instead of you run it manually. There are many options to run gunicorn using such us supervisor, upstart and etc. But I’m using the most basic RC.
Usually I create file “” within my project, then I put run gunicorn command on it.

/your_python_env/bin/gunicorn -c /your_python_env/ myproject.wsgi

Then edit this file “/etc/rc.local” and add the following.

/your_project_path/myproject/ || exit 1   # Your gunicorn run command.
exit 0

This post just my private notes, because this my first time I deployed django app to a live server. Please leave a comment if you have any suggestion for better way or the right way to deploy django app. 😀


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.