DEV Community

Jaira Encio for AWS Community ASEAN

Posted on • Edited on

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

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

Top comments (5)

Collapse
 
raphael_jambalos profile image
Raphael Jambalos

Hi Jaira, Interesting article. For the setup with Django, do you advise adding web servers like Nginx or Apache? Or the Django App Server can stand on its own? I'm coming from Rails where it is needed and in Flask, where I just use unicorn

Collapse
 
jairaencio profile image
Jaira Encio

Hi @raphael_jambalos , that is correct. I do recommend users to install Gunicorn, Nginx, together with virtualenv for an actual production based app. Will update this, thanks!

Collapse
 
raphael_jambalos profile image
Raphael Jambalos

You're welcome! Thanks as well, Jaira!

Collapse
 
wade3944d46f9c3e5 profile image
Wade Wilson • Edited

In the step 'Connect to your Instance via SSH' when you say "n your terminal type the ff:"... is the n for "In your terminal type the ff"... but what is the ff?

There is missing info about what email do we use, security groups, I can't even get beyond much more as this tutorial coveres 91% of the process and only supplies like 62% complete details.

Collapse
 
jairaencio profile image
Jaira Encio • Edited

Hi , yes that was a typo to "in your terminal". Then regarding what to type, you will need to specify the path and file name of the private key or (.pem) file you have downloaded upon creating your instance (step 8). the ec2-user@ipaddress is also located on the ip address of your instance like "ec2-user@ec2-34-204-200-76.compute..."

As for the security groups, there are attached screenshots of the ports I haved used included http, https, ssh, etc on which you will need.