Deploy Django Application on AWS EC2 instance with Nginx and Gunicorn and connect to RDS (Ubuntu Server 20.04 LTS)

Creating a new EC2 instance

After creating the account on AWS as you log into your console you will find EC2 under All services in compute section, so select it.

Now you will be able to see instances(running) under Resources it will state as 0 if you have not launched any instance yet.

So select launch instance, and then select Ubuntu Server 20.04 LTS

Choose an Instance Type and click on Review and Launch now click on launch and create a new key pair and keep it safe as the private key allows you to securely SSH into your instance.

Wait for a few minutes your instance will be live and running.

To connect to your instance follow following steps

Step 1: Open a new terminal, cd into where you have stored your serverkey.pem file.

Step 2: Run this command, if necessary, to ensure your key is not publicly viewable

Step3: Click on your instance, then click on connect, and select SSH client you will find the command as below copy it and paste in your terminal

Setting up your newly created instance for your Django Application.

Step 1: Install Nginx server on your instance

Step 2: Create a new directory give it name whatever want

Step 3: To install pip and virtualenv run following commands

Step 4: Create a virtual environment and activate it.

Step 5: Clone your project from GitHub

Step 6: Install your requirements if any

Step 7: Install Gunicorn

Step 8: Click on the link in security groups from the security tab

Step 9: Now in Inbound rules click on Edit inbound rules and Add rule now add port 80 and port 9090 as below

and then click on the save rule.

Step 10: Now cd into your project directory and open file and in allowed hosts list append your public IP address as below

Step 11: Open new terminal and SSH into your instance using your serverkey.pem and enter below commands

Step 12: Now replace location /{ …. } as below and save and exit form vi editor

Step 13: Now start nginx server using below command

Step14: Return to your previous terminal and cd into your project directory where your wsgi file is located and run the following command.

You have deployed your Django application on Amazon EC2 instance enter your on your public IP adress in browser and you web app will be live and running

Connecting Django Application to RDS (MySQL)

Step1: Select RDS from All Services under Database section and then click on create database button

Step 2: Select MySQL then Enter your credentials and remember them and click on create database

Step 3: Your database will be created now click on your database and copy your and endpoint from Connectivity & security

Step 4: Now Open new terminal SSH into your EC2 instance activate your virtual environment and enter the following commands

Step 5:Now executed following command to connect to your RDS and enter your password.

Step 6: Create a new database and exit

Step 7: Edit your database settings in your projects file as below and replace with your credentials

Step 8: Now run makemigrations and migrate

Step 9: Restart your gunicorn server