Create and Deploy Python Django Application in AWS EC2 Instance under 5 mins

Jaira Encio - Jan 12 '22 - - Dev Community

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

3. Choose an Amazon Machine Image (AMI)

Image description

Image description

For this I would be selecting either Amazon Linux/Ubuntu (free tier eligible)

4. Choose an Instance Type

Image description
For this I would be selecting t2.micro (free tier eligible)

5. Skip and Proceed to Configure Security Groups

Image description
Image description

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

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

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

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

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

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

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

Image description

Additional: Configuration for a production ready App

  1. 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

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player