Blog available for sell
This blog is available for sale. Please 'contact us' if interested.
Advertise with us
hosting aws   0   31036
Hosting Django app for free on Amazon (AWS) EC2 with Gunicorn and Nginx

Recently I hosted a Django project on EC2 instance of AWS. Hosting on PythonAnyWhere is a cakewalk as compared to hosting on EC2 instance. Since I have gone through the process, I am sharing the step by step approach to host Django application on EC2 instance.


We will be hosting the Django App on Free Tier EC2 Instance (t2.micro) which gives us the 750 hours of CPU usage, which should be more than enough for a small scale website.

Django - 2.x
Python - 3.5
EC2 OS - Ubuntu

 

Content:

- EC2 Free Tier account creation and SSH
- Hello World Django App
- Gunicorn 
- Nginx


EC2 Free Tier account creation and SSH:

Amazon offers an EC2 instance with 750 hours of CPU usage for free for 12 months. You can start by creating an account. Instructions to create an account and starting the EC2 instance are pretty clear. 

I chose Ubuntu 16.04 AMI.

Once your instance is up and running, download the private key file and place the same in ~/.ssh/ directory of your personal system.

You can SSH into your EC2 instance from your terminal using the below command.

ssh -i ~/.ssh/aws_key.pem ubuntu@ec2-13-99-202-128.us-east-2.compute.amazonaws.com

Where  ec2-13-99-202-128.us-east-2.compute.amazonaws.com is your instance's public DNS. 

If you are not able to connect the instance using SSH, go to security groups and in the inbound tab, create a rule for SSH with port 22 enabled.

ssh rule in ec2 aws

If the problem persists, follow connection guidelines.



Hello World Django App:

Python3 is already installed on this instance. We need to install pip and other dev tools. Run below commands

sudo apt-get update && sudo apt-get upgrade
sudo apt-get install python3-pip


We will be working in a virtual environment hence we need to install the same.

pip install virtualenv


Create a virtual environment and activate it. Now create a Django project.

(venv) ubuntu:~$  django-admin startproject helloworld

Update the ALLOWED_HOSTS in settings.py file. For now, you can allow requests from all DNS or IPs but this is not safe. A much safer way would be to use the current public IP of the instance.

ALLOWED_HOSTS = ["13.99.202.218"]


Start the development server.

python manage.py runserver 0.0.0.0:8000


Now when you type IP your instance's public IP address with 8000 port in the browser, you should be able to see the Default home page of hello world Django project. 

But before that, you need to add a custom TCP rule to allow incoming traffic on port 8000.


Gunicorn:

The development server of the Django application is good for testing but is highly insecure and can not handle more than one request at a time. In short for the production environment, we have to move away from the development server.

Stop the development server running on port 8000.

In the virtual environment, install Gunicorn if not installed already.

Start the Gunicorn using the below command.

gunicorn --workers 3 --bind 0.0.0.0:8000 helloworld.wsgi:application

This will start the Gunicorn on the same interface on which the development server was started. You can again test the application from the browser using public IP and port.


Nginx:

To integrate Gunicorn with Nginx make below changes.

Create a file /etc/nginx/sites-available/helloworld and paste the below content in it.

server {
    listen 80;
    server_name YOUR-PUBLIC-IP;

    # to avoid any error while fetching fevicon
    location = /favicon.ico { access_log off; log_not_found off; }

    location /static/ {
        root /home/ubuntu/helloworld;
    }

    location / {
        include proxy_params;
        # communicate via socket file created by Gunicorn
        proxy_pass http://unix:/home/ubuntu/helloworld/helloworld.sock;
    }
}


Now create the soft link of this file in the sites-enabled directory.

sudo ln -s /etc/nginx/sites-available/helloworld /etc/nginx/sites-enabled


Restart the Gunicorn using the below command.

gunicorn --workers 3 --bind unix:/home/ubuntu/helloworld/helloworld.sock helloworld.wsgi:application


This will enable the Nginx and Gunicorn communication over a socket instead of a port, which is faster and more secure.


Now you can access the application by typing your public IP in the browser. Enjoy the free hosting for up to 12 months.

aws public ip access django app


In the next article, we discussed how to use a custom domain for your Django app on EC2 instance.


You can host your app for free (or for $5 with custom DNS) on Pythonanywhere


hosting aws   0   31036
0 comments on 'Hosting Django App For Free On Amazon (Aws) Ec2 With Gunicorn And Nginx'
Login to comment


Related Articles:
How to host Django application on DigitalOcean server using Gunicorn, Supervisor, and NGINX
hosting Django application on DigitalOcean Server with custom Domain. Using WSGI server to host the Django application. Hosting Django application with Gunicorn, Supervisor, NGINX. Service static and media files of Django application using NGINX. Using Godaddy Domain to server traffic of Django application. Production deployment of Django applications....
AWS EC2 Vs PythonAnyWhere Vs DigitalOcean for hosting Django application
This article list out the differences between three major options to deploy Django application, We are briefly comparing the digital ocean, pythonAnyWhere, and AWS EC2 to host the Django application here, AWS EC2 Vs PythonAnyWhere for hosting Django application, hosting Django application for free on AWS EC2, Hosting Django application for free on PythonAnyWhere, comparing EC2 and Pythonanywhere and DigitalOcean...
AWS EC2 Vs PythonAnyWhere for hosting Django application
AWS EC2 Vs PythonAnyWhere for hosting Django application, hosting Django application for free on AWS EC2, Hosting Django application for free on PythonAnyWhere, comparing ec2 and pythonanywhere...
Preventing cross-site scripting attack on your Django website
XSS attack in Django, preventing cross-site scripting attack in Django website, avoid XSS in Django application, Enabling SECURE_BROWSER_XSS_FILTER in Django website, blocking cross-site scripting attach on Django website...
DigitalOcean Referral Badge

© 2024-2025 Python Circle   Contact   Sponsor   Archive   Sitemap