DEV Community

Cover image for Deploy The Django web application on AWS EC2 Instance
N. V. Murali Krishna
N. V. Murali Krishna

Posted on • Updated on

Deploy The Django web application on AWS EC2 Instance

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
 ex:
      cd /Home/Downloads/
 Next Connect your remote instance run this commands
 ex:
    $ chmod 400 muralikrishna.pem
    $ SSH -i "muralikrishna.pem" ubuntu @ ec2-52-66-238-206.ap-south-1.compute.amazonaws.com
    We are successfully connected AWS SSH
    if you want exit the connection type (exit) click enter
Enter fullscreen mode Exit fullscreen mode

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 https://github.com/SUBHAMURALIKRISHNA/Student-Assessment-Portal-windows
  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 
  ex:
    [program:gunicorn]
directory=/home/ubuntu/Student-Assessment-Portal-windows
command=/home/ubuntu/env/bin/gunicorn --workers 3 --bind unix:/home/ubuntu/Student-Assessment-Portal-windows/app.sock svportal.wsgi:application
autostart=true
autorestart=true
stderr_logfile=/var/log/gunicorn/gunicorn.err.log
stdout_logfile=/var/log/gunicorn/gunicorn.out.log
[group:guni]
programs:gunicorn

  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;
        server_name 3.16.31.127;
        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
Enter fullscreen mode Exit fullscreen mode

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;
         server_name 18.222.109.192;
          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 
Enter fullscreen mode Exit fullscreen mode

4.Creating a RDS Instance In AWS:

 (a) open AWS
    services
    Database
    RDS
    create database
    select standard create

 (b) engine type:
    mysql

 (c) Templates:
    Free tire 

 (d) settings:
    Identifier -krishnadb 

 (e) credentials settings:
    krishna- username
    Murali123465-Password 

 (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/rdS-amazonaws.com-  copy this end point

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

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

 (m)here we can add the database
   ex: 
      DATABASES ={
      'default':{
        'ENGINE' : 'django.db.backends.mysql',
        'NAME' : 'krishdb',
        'USER' : 'krish',
        'PASSWORD' : 'Murali14',
        'HOST' : 'krishnadb.citovt0dmixr.us-east-2.rds.amazonaws.com',
        'PORT' : '3306',
        }
       }

 (n)$ python3 manage.py 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 \
python-pip

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

 (r)$ python3 manage.py makemigrations
    $ python3 manage.py migrate
    $ python3 createsuperuser

 (s)Here reload the supervisorctl 
    $ sudo supervisorctl reload
    $ sudo service nginx restart
Enter fullscreen mode Exit fullscreen mode

5.Configuring Route 53 with external:

 (a)connect your ssh: (note if you already connected no need)
   $ SSH -i "muralikrishna.pem" ubuntu@ec2-52-66-238-206.ap.south-1.comute.amazonaws.com

  (b)$ source env/bin/activate

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

  (d)$ sudo nano djnago.conf
     change your domin name 
     ex:
       server{
         listen 80;
         server_name studentassessment.xyz www.studentassessment.xyz;
         }

  (e)$ sudo nginx -t

  (f)$ sudo service nginx restart 

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

  (h)services
     search->Route 53

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

  (j)click create record set 
     name- www
     value- Ipaddress-52.66.238.206
     click create
     click again create recordset
     alias target -www.smarttuts.com
     click create
  (k)goo-to godaddy.com
  (l)my account visit
  (m)my products 
     create ur website go-to dns
     DNS
     name servers -form aws values
   ex:
    ns-527.awsdns-01.net
    ns-1455.awsdns-53.org
    ns-1899.awsns-45-co-uk
    ns-470awsdns-58.com

click save
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Top comments (4)

Collapse
 
shyam1107 profile image
shyam1107

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

Collapse
 
nv_murali_krishna profile image
N. V. Murali Krishna

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

Collapse
 
asdflkjh profile image
sadasdkjoiwck

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 ?

Collapse
 
nv_murali_krishna profile image
N. V. Murali Krishna • Edited

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