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
- Create and Launch your own EC2 instance
- SSH on your EC2
- Deploy packages
- Run and Create App
Creating EC2 Instance
1. Login your AWS account and open EC2 Console
2. Click Launch Instance
3. Choose an Amazon Machine Image (AMI)
For this I would be selecting either Amazon Linux/Ubuntu (free tier eligible)
4. Choose an Instance Type
For this I would be selecting t2.micro (free tier eligible)
5. Skip and Proceed to Configure Security Groups
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
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
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.
Create and Run python Django App
- 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
Edit settings.py of your django app, set ALLOWED_HOSTS to = ["*"]
Navigate to project and run app
% python3 manage.py runserver 0:8000
Copy and paste the Public DNS(Ipv4):8000 of your instance in your browser
Additional: Configuration for a production ready App
- Configure and Integrate Gunicorn and Nginx Configuring both takes a whole lot of steps. I would recommend this documentations related to that:
Top comments (5)
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
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!
You're welcome! Thanks as well, Jaira!
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.
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.