Deploy Django application to AWS

Are you worried of deploying a django application, In this blog you can see how to deploy django app to aws

Download as pdf


Note: For this demo

Create an aws instance

Installing mysql in the server

sudo apt-get update 
sudo apt-get upgrade

sudo apt install mysql-server
sudo mysql_secure_installation

sudo mysql
CREATE USER 'rahul'@'localhost' IDENTIFIED BY '123456';
GRANT ALL PRIVILEGES ON * . * TO 'rahul'@'localhost';

Lets install python virtual env

sudo apt-get install python3-venv

clone the repository of django project you created, here for demo

git clone

Create a virtual env and activate it:

python3 -m venv env
source env/bin/activate

Install all the requirements

pip install -r requirements.txt

If this shows any errorĀ 

sudo apt-get install python-dev python3-dev
sudo apt-get install libmysqlclient-dev  
pip install -r requirements.txt

Then create a database :

mysql -u rahul -p

# Lets create a database
CREATE DATABASE aws_demo_db;

mysql username, password, db name should match with the code that we cloned

then lets migrate the database

python makemigrations
python migrate

Setting up the server

pip install gunicorn
sudo apt-get install nginx

Then allow the http, https, 8000 traffic in the inbound rule

gunicorn --bind aws_demo_project.wsgi:application

When the terminal is closed the application also stops working

There the supervisor comes into play

sudo apt-get install supervisor

Lets configure the Supervisor

cd /etc/supervisor/conf.d/
sudo touch gunicorn.conf

copy this in gunicorn.conf file:

command=/home/ubuntu/env/bin/gunicorn --workers 3 --bind unix:/home/ubuntu/django_aws_demo/app.sock aws_demo_project.wsgi:application



sudo mkdir /var/log/gunicorn
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl status

Lets configure the nginx

cd /etc/nginx/sites-available/
sudo touch django.conf

copy this in django.conf

server {
        client_max_body_size 4G;

        location / {
                include proxy_params;
                proxy_pass http://unix:/home/ubuntu/django_aws_demo/app.sock;
        location /static/ {
                autoindex on;
                alias /home/ubuntu/django_aws_demo/static/;
        location /media/ {
                autoindex on;
                alias /home/ubuntu/django_aws_demo/media/;

sudo nginx -t
sudo ln django.conf /etc/nginx/sites-enabled/
sudo service nginx reload
sudo service supervisor reload

if you go to the ip adress you can see the application

  see all blogs

Go to Home