Deploying Django Apps
By: Sharon Zacharia
Tags: Python Programming, Django, AWS, Web App Development, Nginx

Deploying Django Apps

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

sudo apt-get update

Install python , pip and django

sudo apt install python sudo apt install python3-pip pip3 install django

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

cd /home/ubuntu/

mkdir Project

cd Project

mkdir ProjectName

cd ProjectName

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

DEBUG = False

Set Allowed hosts

ALLOWED_HOSTS = [‘your domain name’]

Add static file folder

BASE_DIR=os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

STATIC_ROOT = os.path.join(BASE_DIR, “static”)

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 )

manage.py makemigrations manage.py migrate manage.py collectstatic

Installing Nginx

To install Nginx run the command

sudo apt install nginx

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

sudo vi default

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

manage.py collectstatic

Now start nginx server

sudo service nginx start #to start nginx

sudo service nginx stop #to stop nginx

sudo service nginx restart #to restart nginx

Installing Gunicorn

pip install gunicorn

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

gunicorn ProjectName.wsgi:application- -bind 0.0.0.0:9000

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