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

(Comments)

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

  Ipaddress 
  Username - root
  Password

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

ssh-keygen

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
id_rsa.pub #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

cat id_rsa.pub

This will display the contents of id_rsa.pub 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 fabfile.py 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 fabfile.py, set the value of PROJ_APP to the directory name containing the file settings.py.

You need to add ALLOWED_HOSTS and FABRIC dictionary to your local_settings.py or settings.py

FABRIC = {
    "DEPLOY_TOOL": "rsync",  # Deploy with "git", "hg", or "rsync"
    "SSH_USER": "",  # VPS SSH username
    "HOSTS": [""],  # The IP address of your VPS
    "DOMAINS": ALLOWED_HOSTS,  # Edit domains in ALLOWED_HOSTS
    "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
    "SECRET_KEY": SECRET_KEY,
    "NEVERCACHE_KEY": NEVERCACHE_KEY,
}

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.

Comments

Recent Posts

Archive

2022
2021
2020
2019
2018
2017
2016
2015
2014

Tags

Authors

Feeds

RSS / Atom