Django production deployment using Ubuntu, Nginx, Gunicorn, Supervisor and Fabfile


Different developers follow different ways to deploy. I would like to explain some of the ways.

This post explains one way to deploy using Ubuntu, supervisor, gunicorn, nginx and fabfile and it assumes that your local operating system is Ubuntu. I will provide tutorial for windows users a little later.

Here are the two parts of this tutorial:

  1. Basic Server Setup
  2. Deployment using fabfile

Basic Server Setup

Let us assume that you have created a new Ubuntu 14 instance. Generally, you will be given

  Username - root

You can login to the server using:

  ssh root@ip #Enter the password when asked

SSH key-based authentication:

Lets create(if not exists) a key in your system.

Open a new terminal in your local system and enter


This will create a directory .ssh in your home directory if not exists already and will create two files inside it.

id_rsa  #Private file which you need to keep secure #File containing public key. 
#You need to copy the contents of this file to the file ~/.ssh/authorized_keys in the server

Lets copy the public key


This will display the contents of on the screen so you can select with your mouse and copy it.

Now login to the server

ssh root@ip

Paste the copied public key to the authorized_keys file. With this step, you can login to the server for user root without password. Repeat the same process for every user to enable ssh key authentication.

vim ~/.ssh/authorized_keys #Vim is a terminal editor.

Disable password authentication for security

Password authentication allows hackers to brute force and login to your server. So, its secure to disable password authentication(you need to enable key based authentication before this).

Login to server and disable password authentication

ssh root@ip

vim /etc/ssh/sshd_config

#Add new line

PasswordAuthentication no

#Restart ssh

restart ssh

Adding user

Lets assume we are working on a project cowhite, we can follow this

adduser cowhite

This will create a new user cowhite and add a directory cowhite in the home directory It will prompt for password, enter a secure password and remember it.

Giving sudo access to the user

We can give sudo access to any user

adduser cowhite sudo

Logging in to the newly created user

Since we disabled password authentication for all users and since we didn't add any key to the list of authorized_keys for the newly created user 'cowhite', we can't login to that user directly from our system.

So login to root user first and then login to new user

ssh root@ip
su cowhite #This wont even ask password as you are logged in as root

Now, you are logged in as the new user 'cowhite' and you can add your local public key to the list of authorized_keys of this new user the same way we did for root user(please check above).

Deployment Using Fabfile

You need and deploy directory for the deployment. Please use the files from our github repo.

This fabfile is a slightly modified version of the one provided by Mezzanine, a popular CMS framework for Django. As of now, it will work for projects with or without mezzanine with django1.8. I will modify it to work for more versions and I will update here.

At line 36 of, set the value of PROJ_APP to the directory name containing the file

You need to add ALLOWED_HOSTS and FABRIC dictionary to your or

    "DEPLOY_TOOL": "rsync",  # Deploy with "git", "hg", or "rsync"
    "SSH_USER": "",  # VPS SSH username
    "HOSTS": [""],  # The IP address of your VPS
    "REQUIREMENTS_PATH": "requirements.txt",  # Project's pip requirements
    "LOCALE": "en_US.UTF-8",  # Should end with ".UTF-8"
    "DB_PASS": "",  # Live database password
    "ADMIN_PASS": "",  # Live admin user password

Here are the commands for deployment to production server:

fab all

The above command will do the deployment along with the initial server installation necessary for the deployment.

For further deployments, you can use

fab deploy

This will only deploy the latest changes to the production server.


Recent Posts






RSS / Atom