DEV Community

Cover image for Deploying Django Application To AWS EC2 Istance

Deploying Django Application To AWS EC2 Istance

subhamuralikrishna profile image N.V Murali Krishna Updated on ・5 min read

1.Create instance:
Go-to AWS services
(1) Launch instance

  (a)select Your Operating system (Choose AMI)

  (b) Choose Instance type (max)
      ex:Type (t2 micro) or what your required 

  (c)Configure security group
      click Add rule,select type- Http, source- anywhere

  (d)Review and launch 
      click launch,
      select create new key pair,muralikrishna or what you want and keep remember it(key pair name),
      Click Download, 
      select your instance (click connect),
 open an SSH client:(in terminal):
 GO to your key pair location
      cd /Home/Downloads/
 Next Connect your remote instance run this commands
    $ chmod 400 muralikrishna.pem
    $ SSH -i "muralikrishna.pem" ubuntu @
    We are successfully connected AWS SSH
    if you want exit the connection type (exit) click enter

2.Deploying Django Application to AWS EC2 Instance:

(a)Run this commands one-by-one:
    $ sudo apt-get update
    $ sudo apt-get upgrade

(b)Next Check the python version :
    $ python3 --version

(c)Next install python environment:
    $ sudo apt-get install python3-venv
    $ python3 -m venv env

(d)Now activate the environment:
    $ source env/bin/activate 

(e)Please push ur Django project in GITHUB
    $ git clone
  Note: Here "Student-Assessment-Portal-windows" this is my repositories name

(f)Install nginx service:
   $ sudo apt-get install -y nginx

(g)Next install gunicorn:
   $ pip3 install gunicorn

(h)Install the requirements what you want i mean your project requirements, GO to your requirements.txt file path and run this command 
  $ pip3 install -r requirements.txt

(i)Install supervisor
  $ sudo apt-get install supervisor

(j)Go-to supervisor location 
  $ cd /etc/supervisor/conf.d/

(k)Now we can create and edit gunicorn configuration file using this commands with in the $ cd /etc/supervisor/conf.d/
  $ sudo touch gunicorn.conf
  $ sudo nano gunicorn.conf

(l)Here we can write the gunicorn program 
command=/home/ubuntu/env/bin/gunicorn --workers 3 --bind unix:/home/ubuntu/Student-Assessment-Portal-windows/app.sock svportal.wsgi:application

  Note: above configuration file "Student-Assessment-Portal-windows" enter your project directory name and "svportal" project name

  (m)Now create gunicorn file and check status:
    $ sudo mkdir /var/log/gunicorn
    $ sudo supervisorctl reread
      output: guni:avaliable 
    $ sudo supervisorctl update
      output: guni:added process group
    $ sudo supervisorctl status
      output: guni:gunicorn  Running pid 11219, uptime 0:00:01

  (n)Go-to nginx sites location
    $ cd /etc/nginx/sites-available/    

  (o)create django configuration file edit the file 
    $ sudo touch django.conf
    $ sudo nano django.conf
     ex: server{
        listen 80;
        location / {
        include proxy_params;
        proxy_pass http://unix:/home/ubuntu/Student-Assessment-Portal-windows/app.sock;
 Note: Here server_name is aws instance ip address check your instance and copy paste over here

 (p)Next test the nginx service
   $ sudo nginx -t
   $ sudo ln django.conf /etc/nginx/sites-enabled/

 (q)Finally restart the nginx service:
   $ sudo service nginx restart

   Note: Here we can deploy django application successfully but we can't run static files and media files

3.Connect to static files and media files:

 (a) we need to go configuration path
   $ cd /etc/nginx/sites-enabled/

 (b) open django configuration file
   $ sudo nano django.conf
    ex: server{
         listen 80;
          location / {
            include proxy_params;
            proxy_pass http://unix:/home/ubuntu/Student-Assessment-Portal-windows/app.sock;
         location /static {
            autoindex on;
            alias /home/ubuntu/Student-Assessment-Portal-windows/static;
         location /media {
            autoindex on;
            alias /home/ubuntu/Student-Assessment-Portal-windows/media;

 (c)test nginx service again
   $ sudo nginx -t
   $ sudo service nginx restart 

4.Creating a RDS Instance In AWS:

 (a) open AWS
    create database
    select standard create

 (b) engine type:

 (c) Templates:
    Free tire 

 (d) settings:
    Identifier -krishnadb 

 (e) credentials settings:
    krishna- username

 (f) Allocate storage:
    min(20gb) or max(16384gb)

 (g) additional connectivity configuration:
    Public Accessable -select

 (h) Additional configguration:
    Enter database name -krishnadb

 (i) create database- Click button

 (j) open database
   go-to connectivity and security:
   click endpoint:
     smarttus.cathhsbxzpu-ap-south/  copy this end point

 (k)open terminal and connect your ssh go-to
   $ source env/bin/active

 (l)next open with nano editor using this command
   $ sudo nano

 (m)here we can add the database
      DATABASES ={
        'ENGINE' : 'django.db.backends.mysql',
        'NAME' : 'krishdb',
        'USER' : 'krish',
        'PASSWORD' : 'Murali14',
        'HOST' : '',
        'PORT' : '3306',

 (n)$ python3 makemigrations
   note: if have an error mysql client

 (p)run this commands one-by-one
   $ sudo apt-get install python3 python-dev python3-dev \
build-essential libssl-dev libffi-dev \
libxml2-dev libxslt1-dev zlib1g-dev \

 (q)$ sudo apt-get install default-libmysqlclient-dev
$ pip install mysqlclient

 (r)$ python3 makemigrations
    $ python3 migrate
    $ python3 createsuperuser

 (s)Here reload the supervisorctl 
    $ sudo supervisorctl reload
    $ sudo service nginx restart

5.Configuring Route 53 with external:

 (a)connect your ssh: (note if you already connected no need)
   $ SSH -i "muralikrishna.pem"

  (b)$ source env/bin/activate

  (c)$ cd etc/nginx/sites-enabled/

  (d)$ sudo nano djnago.conf
     change your domin name 
         listen 80;

  (e)$ sudo nginx -t

  (f)$ sudo service nginx restart 

  (g)go-to Instance 
     ipv4 public ip-copy

     search->Route 53

  (i)Hosted Zone-selected
     click create hosted zones
     Domain name:
     click create

  (j)click create record set 
     name- www
     value- Ipaddress-
     click create
     click again create recordset
     alias target
     click create
  (l)my account visit
  (m)my products 
     create ur website go-to dns
     name servers -form aws values

click save

6.Securing website with ssl (HTTPS):

  (a)connect ur ssh and run the bellow commands
    $ sudo apt-get update
    $ sudo apt-get install software-properties-common
    $ sudo add-apt-repository universe
    $ sudo add-apt-repository ppa:certbot/certbot
    $ sudo apt-get update

 (b)Now execute the below command to install certbot
   $ sudo apt-get install certbot python3-certbot-nginx

   This command will ask you for permission to accept by entering y.

  (c)Now integrate SSh ith the nginx by the bellow command
     sudo certbot --nginx

  NOTE: This command will ask you for some confirmation.

1. Email: enter your email so that you will get updates about your certificate
2. Agree to terms of services: enter ‘a’ for accepting  
3. Share your email: You can ignore it by entering ‘n’.
4. Choose your domains. If you want all the domains then hit enter
5. Choose the redirect option: Enter 2 for enabling redirect.

(d)$ cd /etc/nginx/sites-enabled/
   $ sudo apt-get install cron
   $ systemctl status corn
   $ ctrl + X
   $ crontab -e

(5)Finally restart the server your site will secure 
  $ sudo supervisorctl reload  
  $ sudo service nginx restart


Editor guide
shyam1107 profile image

hey bro!!
i want to ask you that my project im using splite3
so its okay if i go with mysql in AWS because i can't find splite3 in AWS

subhamuralikrishna profile image
N.V Murali Krishna Author

Yeah I know in aws there no splite3... Don't worry about it please go through the MySQL database

hello_vatsal profile image

Also you're using ubuntu distribution for ec2 instance (because apt-get package manager). Can you share changes have to be done in gunicorn if using amazon linux 2 ?

subhamuralikrishna profile image
N.V Murali Krishna Author

Yes Vatsal, I'm using ubuntu 20.04 it is very flexible and secure for deploying our projects..
yeah i will try to change