Deploying a Django Project on AWS EC2: A Three-Part Guide

Adedolapo AtibaAdedolapo Atiba
5 min read

Deploying a Django project on AWS EC2 can be a challenging process, especially for first-time deployers. I won’t sugarcoat it — I found it frustrating. As a junior developer handling deployment to AWS for the first time, I faced several roadblocks. But as someone once told me, “Always write about things that frustrate you.” So here I am, sharing my experience to help others navigate the same challenges.

This guide is divided into three parts:

  • Part 1: Deploying Your Django Project on AWS EC2

  • Part 2: Redirecting Your Namecheap Domain to AWS

  • Part 3: Securing Your Website with Load Balancers & SSL Certification

In this article, we’ll focus on setting up and deploying a Django project on AWS EC2, covering everything from instance setup to running your application successfully. Let’s get right into it.

STEP 1: LAUNCHING AN EC2 INSTANCE

After creating your AWS account, the first step is to launch an EC2 instance. Navigate to your EC2 Dashboard, and in the top-right corner, you’ll see the “Launch Instances” button. Click on it, and you’ll be prompted to configure and create your instance.

AWS EC2 Dashboard

STEP 2: CONFIGURING YOUR INSTANCE

Here, you get to name your instance and choose your AMI (Amazon Machine Image). Since you’ll be using Amazon Linux, select Amazon Linux 2023 AMI.

Next, choose an instance type. For Django projects, a t2.micro (free-tier eligible) is sufficient.

Instance Type

The next step is to configure your Key Pair, which is used for SSH access. You can either create a new key pair or use an existing one.

Creating Key pair

Setting Security Groups

Now, configure Security Groups to allow inbound traffic for the necessary ports:

  • SSH (port 22) — For remote access.

  • HTTP (port 80) — For web access.

  • HTTPS (port 443) — For secure connections.

  • Any other required ports (e.g., port 8000 for Django development).

Security Settings

After configuring everything, you can now launch your instance.

STEP 3: VERIFYING YOUR INBOUND RULES

After launching the instance, navigate to your instance’s security group settings.

Edit the inbound rules to ensure they include the necessary ports and set them to “Anywhere (IPv4)”. The required ports are:

  • SSH (22) — For remote access.

  • HTTP (80) — For web traffic.

  • HTTPS (443) — For secure web traffic.

  • Development Port (8000) — For running Django during development.

Updated Security Group Inbound Rules

STEP 4: CONNECTING YOUR EC2 INSTANCE

Once the instance is successfully launched. Click on the newly created instance to open the Instance Dashboard.

In the top-right corner, you’ll see the “Connect” button. Click on it.

Instance Dashboard

You will be prompted to the EC2 Instance Connect page, click the Connect that down part of the page.

INSTANCE CONNECT

STEP 5: UPDATING AND INSTALLING DEPENDENCIES

Instantly, after contecting you will be taken to a terminal, there you will have to update and install dependencies. Run the these command to update and install your dependencies:

sudo yum update -y
sudo yum install -y python3 python3-pip python3-virtualenv git

This installs:

  • Python 3

  • pip (Python package manager)

  • virtualenv (For creating isolated Python environments)

  • git (For version control)

Verify installation by running:

python3 --version
pip3 --version

STEP 6: CLONING YOUR GITHUB REPO

Run:

git clone <your-github-link-repo>

This will clone your project from your github. Replace <your-github-link-repo> with your actual repo link.

Next is to enter your repo directory by running:

cd <your project directory name>

Replace your project directory name with your django proejct repo name. You can also list the contents to verify:

ls

This will list all your github repo content.

After that you will create and activate virtual environment by running:

python3 -m venv venv
source venv/bin/activate

Upon activating virtual environment, you will install your project’s requirements by running this command:

pip install -r requirements.txt

STEP 7: INSTALL AND SET UP POSTGRESQL(Optional)

If you’re using Postgresql, install it:

sudo yum install -y postgresql postgresql-server postgresql-devel

Start and enable Postgresql:

sudo systemctl start postgresql
sudo systemctl enable postgresql

Then create a .env file:

nano .env

Add your database credentials and other sensitive information. Save and exit (CTRL+X, then Y, then Enter).

STEP 8: SET UP GUNICORN

If you haven’t installed gunicorn before, run:

pip install gunicorn

After installing, run:

gunicorn --bind 0.0.0.0:8000 <your-project-name>.wsgi

With that your django app is now accessible on http://your-ec2-public-ip:8000/

STEP 9: INSTALL AND CONFIGURE NGINX

Install nginx by:

sudo amazon-linux-extras enable nginx1
sudo yum install -y nginx

Configure nginx for django by creating a Nginx configuration file for your project:

sudo nano /etc/nginx/conf.d/<your-project-name>.conf

And then paste this in it:

server {
    listen 80;
    server_name your-ec2-public-ip;

    location / {
        proxy_pass http://127.0.0.1:8000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    location /static/ {
        alias /home/ec2-user/<your-project-name>/staticfiles/;
    }

    location /media/ {
        alias /home/ec2-user/<your-project-name/media/;
    }
}

Save and exit (CTRL+X, then Y and Enter).

Test, Start and Enable Nginx:

sudo nginx -t
sudo systemctl start nginx
sudo systemctl enable nginx

STEP 10: CONFIGURE GUNICORN TO RUN AS A SERVICE

Create a systemd service file for Gunicorn:

sudo nano /etc/systemd/system/gunicorn.service

Paste the following:

[Unit]
Description=Gunicorn daemon for Django project
After=network.target

[Service]
User=ec2-user
Group=ec2-user
WorkingDirectory=/home/ec2-user/<your_project_name>
ExecStart=/home/ec2-user/<your-project-name>/venv/bin/gunicorn --workers 3 --bind unix:/home/ec2-user/<your-project-name>/gunicorn.sock <your-project-name>.wsgi:application

[Install]
WantedBy=multi-user.target

Save it and exit.

Now start and enable gunicorn.

sudo systemctl daemon-reload
sudo systemctl start gunicorn
sudo systemctl enable gunicorn

Finally, restart Nginx with:

sudo systemctl restart nginx

You can now visit on http://your-ec2-public-ip/ to see your Django.

Conclusion

You have now successfully deployed your Django project on AWS EC2. Your application is running behind Gunicorn and Nginx, making it more robust for production. In the next part of this series, we will explain how to:

  • Redirect your Namecheap domain to AWS

  • Secure your website with load balancers and SSL certification

Final Tips:

  • Security: Review your security groups and firewall settings regularly.

  • Troubleshooting: Check logs (e.g., Nginx and Gunicorn logs) if you encounter issues.

  • Further Reading: Look into AWS best practices for scaling and monitoring your deployed application.

23
Subscribe to my newsletter

Read articles from Adedolapo Atiba directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Adedolapo Atiba
Adedolapo Atiba

Passionate backend developer with expertise in building scalable solutions using Django and Python. Focused on creating innovative software with attention to detail with critical thinking and strong problem-solving skills.