1. Cloud (AWS / Azure / Google)

How to Deploy a Simple Django App Using AWS EC2

So you have written your first Django App and want to install the app to your Free AWS Ec2 Instance. Here is the steps you can follow to do the same.

Connect your EC2 Instance using Putty

sudo apt-get update
Then you need your webserver (nginx):

sudo apt-get install nginx
Since we installed Ubuntu 14.04, Nginx starts up automatically. You should be able to visit your public IP address and see a screen that says Welcome to nginx!

Great, nginx was downloaded correctly and is all booted up. Let’s get your app on there!

Since this is a Django project, you’ll need to install Django on your server.

sudo apt-get install python-pip
sudo pip install virtualenv
sudo pip install git

Pull your project down from github:

git clone my-git-hub-url
In your project’s root directory make sure you have at a minimum a requirements.txt file with the following:

django
gunicorn

Side note: gunicorn is a Python WSGI HTTP Server for UNIX. You can find out more here.

Make a virtualenv and install your pip requirements using:

pip install -r requirements.txt
Now you should have django and gunicorn installed.

Since nginx starts automatically you’ll want to shut it down.

sudo service nginx stop
Now you’ll turn on gunicorn by running:

gunicorn app-name.wsgi
Now that gunicorn is up and running it’s time to turn on nginx:

cd ~/etc/nginx
sudo vi nginx.conf

Within the http block either at the top or the bottom, you’ll want to insert this block:

server {

listen 80;
server_name public-ip-address;

access_log /var/log/nginx-access.log;
error_log /var/log/nginx-error.log;
root /home/ubuntu/project-root;

location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Now start up nginx again:

sudo service nginx start
Go to your public IP address and you should see your lovely app on the Internet.

 

Note: There are many different ways to set up your servers, this is just one way. You can and should experiment to see what works best for you.

Comments to: How to Deploy a Simple Django App Using AWS EC2

Your email address will not be published. Required fields are marked *

Nishant Kumar Verma

Full Stack Developer, Experience in enterprise application development. Providing solution for scalable SaaS application and developing product to automate part of development efforts.

#CleanCodeArchitect #.Net #C# #NodeJS #TypeScript #React #JavaScript #DesignPattern #SOLID #WebAPI #HTML5 #Bootstrap #CSS

#AWS #Docker #Kubernetes #GIT #GitHubAction #ECS #LoadBalancer #S3 #Lambda #Azure #CI/CD Pipeline

#MongoDB #MSSQL #MySQL #JSON