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

chmod 400 Your_serverkey_name.pem
ssh -i "serverkey.pem"

Setting up your newly created instance for your Django Application.

sudo apt install nginx
mkdir aditya && cd aditya
Sudo apt update
sudo apt install python3-pip
sudo apt install python3-virtualenv
virtualenv venv
source venv/bin/activate
git clone https://github/your_project_link
pip install -r requirements.txt
sudo apt install gunicorn
cd /etc/nginx/sites-enabled
sudo vi default
location /{   proxy_pass;  }
sudo systemctl start nginx
gunicorn --bind wsgi

Connecting Django Application to RDS (MySQL)

sudo apt-get install python3-dev default-libmysqlclient-dev build-essentialpip3 install mysqlclientsudo apt install mysql-server
mysql -u YOUR USERNAME -p -h
mysql> create database mydb;
mysql> quit;
DATABASES = {              'default':{   'ENGINE': 'django.db.backends.mysql',                            'NAME': 'mydb',                            'USER': 'YOUR_MASTER_USERNAME',                            'PASSWORD': 'YOUR_MASTER_PASSWORD',                            'HOST':'YOUR_RDS_ENDPOINT',                            'PORT': 3306,                            'OPTIONS': {                                         'sql_mode': 'traditional',                                       }                         }}
python3 makemigrations
python3 migrate
pkill gunicorn
gunicorn --bind project_name.wsgi



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store