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
chmod 400 Your_serverkey_name.pemStep3: 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
ssh -i "serverkey.pem" ubuntu@ec2-100-25-141-73.compute-1.amazonaws.comSetting up your newly created instance for your Django Application.
Step 1: Install Nginx server on your instance
sudo apt install nginxStep 2: Create a new directory give it name whatever want
mkdir aditya && cd adityaStep 3: To install pip and virtualenv run following commands
Sudo apt update
sudo apt install python3-pip
sudo apt install python3-virtualenvStep 4: Create a virtual environment and activate it.
virtualenv venv
source venv/bin/activateStep 5: Clone your project from GitHub
git clone https://github/your_project_linkStep 6: Install your requirements if any
pip install -r requirements.txtStep 7: Install Gunicorn
sudo apt install gunicornStep 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 settings.py file and in allowed hosts list append your public IP address as below
ALLOWED_HOSTS = ['100.25.141.73','0.0.0.0']Step 11: Open new terminal and SSH into your instance using your serverkey.pem and enter below commands
cd /etc/nginx/sites-enabled
sudo vi defaultStep 12: Now replace location /{ …. } as below and save and exit form vi editor
location /{ proxy_pass http://0.0.0.0:9090; }Step 13: Now start nginx server using below command
sudo systemctl start nginxStep14: Return to your previous terminal and cd into your project directory where your wsgi file is located and run the following command.
gunicorn --bind 0.0.0.0:9090 wsgiYou 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
sudo apt-get install python3-dev default-libmysqlclient-dev build-essentialpip3 install mysqlclientsudo apt install mysql-server
Step 5:Now executed following command to connect to your RDS and enter your password.
mysql -u YOUR USERNAME -p -h YOUR_RDS_ENDPOINT.rds.amazonaws.com
Step 6: Create a new database and exit
mysql> create database mydb;
mysql> quit;Step 7: Edit your database settings in your projects settings.py file as below and replace with your credentials
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', } }}
Step 8: Now run makemigrations and migrate
python3 manage.py makemigrations
python3 manage.py migrateStep 9: Restart your gunicorn server
pkill gunicorn
gunicorn --bind 0.0.0.0:9090 project_name.wsgi