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" ubuntu@ec2-100-25-141-73.compute-1.amazonaws.com

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
ALLOWED_HOSTS = ['100.25.141.73','0.0.0.0']
cd /etc/nginx/sites-enabled
sudo vi default
location /{   proxy_pass http://0.0.0.0:9090;  }
sudo systemctl start nginx
gunicorn --bind 0.0.0.0:9090 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  YOUR_RDS_ENDPOINT.rds.amazonaws.com
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 manage.py makemigrations
python3 manage.py migrate
pkill gunicorn
gunicorn --bind 0.0.0.0:9090 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