Deploying Django Apps

Django with Nginx and Gunicorn on AWS Cloud.

SHARON ZACHARIA
Analytics Vidhya
4 min readJan 8, 2021

--

Introduction

Once we have completed the development part of a web app it should be hosted so that the public can access it from anywhere. We will see how to deploy and host a django application on AWS EC2 instance using Nginx as the webserver and Gunicorn as WSGI.

AWS EC2

Amazon Elastic Compute Cloud (Amazon EC2) is a web service that provides resizable compute capacity in the cloud. We deploy and host our web apps on EC2 instance after choosing the AMI (OS) of our choice. We will see more on this in upcoming sections. More on EC2 here.

NGINX

Nginx

Nginx is an open-source web server that, since its success as a web server, is now also used as a reverse proxy , HTTP cache, and load balancer. We will use Nginx to server our webpages as needed . More on Nginx here

GUNICORN

Gunicorn is a Web Server Gateway Interface (WSGI) server implementation that is commonly used to run Python web applications.

WSGI — used to forward request from a web server to a python backend.

We won’t use the server that comes with django by default in production.

Deploying the Application

We will start an EC2 instance on AWS, For that login to aws console

  • Select EC2 from all services
  • Select launch New instance and we will choose Ubuntu from list.
  • Select any of the instances ,each has different configurations , we will choose the one with free-tier available.
  • Now configure security groups and open ports 8000 and 9000 since we will be using those ports . Review and Launch your instance , it may take some time for the instance to start running.

Connecting to Instance

We can connect to an instance using 'connect' option in console (or using putty or any other similar tool ).Once connected run the following

Install python , pip and django

Now that we have installed our dependencies we can create a folder in which we will copy our django app

Now we will place our code in the following path /home/ubuntu/Project/ProjectName

GitHub

Make sure that you have your code in a repository so that we can pull the code into our ec2 instance easily.

  • Go to the newly created folder ( /home/ubuntu/Project/ProjectName/ )
  • git clone <repository-url>

This will clone the repository to the folder and next time we can just pull the changes using git pull

Settings.py File

We have to make some changes to the settings.py in our project

  • Put your secret keys and passwords in environment variables
  • Set Debug =False
  • Set Allowed hosts
  • Add static file folder

Run the following so that the model migrations take place and all the static files will be collected to a common folder ( path given in STATIC_ROOT )

Installing Nginx

To install Nginx run the command

There is a config file named default in /etc/nginx/sites-enabled/ that has a basic setup for NGINX, we will edit this file.

The file will look like this after adding the necessary configuration and keep the rest of file as same.

default file for nginx

we will add proxy_pass http://0.0.0.0:9000 and we will provide path to our static folder by adding the path inside location /static/ as above. Make sure you have collected all the static files to a common folder by running

Now start nginx server

Installing Gunicorn

make sure you are in project folder eg: /home/ubuntu/Project and run the following command to start gunicorn

Now that we have installed and configured nginx and gunicorn , our application can be accessed by DNS of the ec2 instance.

--

--