Create and Deploy Python Django Application in AWS EC2 Instance under 5 mins
Published: 2022-01-12 10:19:31


Hello guys! Here is a simple step by step on how to create and deploy your own Python Django Application in AWS EC2 Instance. The goal of this is to deploy your app in the fastest way possible


# Overview
1. Create and Launch your own EC2 instance
2. SSH on your EC2
3. Deploy packages
4. Run and Create App

## Creating EC2 Instance

### 1. Login your AWS account and open EC2 Console
### 2. Click Launch Instance
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/62xh92a5om3pl8v4albl.png)
### 3. Choose an Amazon Machine Image (AMI)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4ytera4zav25kowp6egy.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ypxu428pxz4wpjbj8tdf.png)

For this I would be selecting either Amazon Linux/Ubuntu (free tier eligible)
### 4. Choose an Instance Type
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5yo8bw3nanutz68gkosk.png)
For this I would be selecting t2.micro (free tier eligible)
### 5. Skip and Proceed to Configure Security Groups

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2uadf1g7jbxpkd5e97ug.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wcd89wg16q0hq8c00iwz.png)

You can simply use default or configure your own security groups even later after launching your instance.  You must open HTTP port 80 if you wish to check that the Nginx is successfully set up while visiting index.html. **Note:** You can add additional port for postgresql. this sample is just for default django. 
### 6. Click Review and Launch
### 7. Review Instance Launch then click Launch
### 8. Create new key pair then download
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qvqn8vo5zih02pl9w22c.png)

Save this file as you would be needing this later.
### 9. Click Launch Instance
### 10. Navigate to Instances then check status of running instance


## Connect to your Instance via SSH
### 1. SSH on your EC2 instance
In your terminal type the ff: `% ssh -i ~/Desktop/name_of_your_keypair.pem ec2-user@ipaddress`
Upon entering this, you will encounter a **Warning unprotected file error**. With that you need to **CHMOD** your pem file

### 2. CHMOD pem file
On your terminal type the ff: `% chmod 400 ~/Desktop/umr-example-keypair.pem` 
then retry connecting again via ssh and you should be able to connect to your ec2 instance now.

## Install Packages
Since linux ami has its own python pre installed, I did not include it in my installations. I only added pip and django
`sudo apt update
sudo apt-get install python3-pip
sudo pip3 install gunicorn
sudo apt-get install supervisor
sudo pip3 install django
sudo yum install nginx
`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ys3nhhix4qoocziowmf2.png)

## Start and check status of nginx
`sudo systemctl start nginx
sudo systemctl status nginx
`

You can test this by copying the Public DNS(Ipv4) of your instance in the browser. See the index.html page of Nginx via public ip address ie http://ipadress...
You should see default page for Nginx.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h335jjv68w5kpatdgll5.png)

## Create and Run python Django App
1. Create project
`% django-admin startproject mysite`

Creating project from and ec2 instance is really **optional**, you can always **push your app in a repo then clone** it inside

2. Edit settings.py of your django app, set **ALLOWED_HOSTS to = ["*"]**

3. Navigate to project and run app
`% python3 manage.py runserver 0:8000`

4. Copy and paste the Public DNS(Ipv4):8000 of your instance in your browser


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/syobqlshrk7cbo71cd72.png)

### Additional: Configuration for a production ready App

3. Configure and Integrate Gunicorn and Nginx
Configuring both takes a whole lot of steps. I would recommend this documentations related to that:

https://pythoncircle.com/post/697/hosting-django-app-for-free-on-amazon-aws-ec2-with-gunicorn-and-nginx/

https://www.alibabacloud.com/blog/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-16-04_594319

https://dev.to/rmiyazaki6499/deploying-a-production-ready-django-app-on-aws-1pk3#installing-dependencies