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/gunicorn_config.py”
Here is the configuration:

command = '/your_python_env/bin/gunicorn'    # Gunicorn path within python installation
pythonpath = '/your_project_path/myproject'  # Your project path.
bind = '127.0.0.1:8001'                      # 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/gunicorn_config.py 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 {
    server_name yourdomainorip.com;

    access_log off;

    location /static/ {
        alias /opt/myenv/static/;
    }

    location / {
        proxy_pass http://127.0.0.1:8001;
        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

Bonus:
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 “start.sh” within my project, then I put run gunicorn command on it.

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

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

/your_project_path/myproject/start.sh || 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. 😀

Reference:
https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-django-with-postgres-nginx-and-gunicorn
http://askubuntu.com/questions/228304/how-do-i-run-a-script-at-start-up

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s