DEV Community

Cover image for Deploy a Dockerized Django App on AWS (EC2 + Nginx + Gunicorn + RDS + S3) — A Beginner-Friendly Guide
Techpointby
Techpointby

Posted on

Deploy a Dockerized Django App on AWS (EC2 + Nginx + Gunicorn + RDS + S3) — A Beginner-Friendly Guide

This process only for run project on IP Address without domain but it not support other device (like mobile,tablet etc.)

No prior DevOps experience needed—just follow the steps.

Step 0 — Prerequisites

EC2 Ubuntu 22.04 LTS instance with public IP.

Security group allowing:

SSH 22 → your IP

HTTP 80 → 0.0.0.0/0

HTTPS 443 → 0.0.0.0/0 (optional)

Key pair .pem file to SSH in.

Your Django project in GitHub (with Dockerfile).

RDS (PostgreSQL) and S3 bucket ready.

Step 1 — SSH into EC2

Open your Ubuntu terminal open .pem file path where you install

ssfsf@youdsfsdf-sdf-by-lef-Laptop-134-e0xxx:~$ cd Desktop
Enter fullscreen mode Exit fullscreen mode

Importent open path where you download key par .pem file

In EC2 > Instance > Your_intance (click connect button on top right)
Go SSH Client
copy Run this command

ssfsf@youdsfsdf-sdf-by-lef-Laptop-134-e0xxx:~/Desktop$ 
Enter fullscreen mode Exit fullscreen mode

Past .pem path location

ssfsf@youdsfsdf-sdf-by-lef-Laptop-134-e0xxx:~/Desktop$ chmod 400 "yourpairkey.pem"
Enter fullscreen mode Exit fullscreen mode

Press Enter , then copy Example code SSH Client

ssfsf@youdsfsdf-sdf-by-lef-Laptop-134-e0xxx:~/Desktop$ ssh -i "yourpairkey.pem" ubuntu@ec2-31-85-7123-234.compute-1.amazonaws.com
Enter fullscreen mode Exit fullscreen mode

Then Press Enter, wait few seconds then it ask yes/no type yes it setup ubuntu.

Now your ubuntu live

Step 2 — Update & Install Packages

sudo apt update && sudo apt upgrade -y

# Install required packages
sudo apt install -y git python3-pip python3-venv nginx curl ca-certificates gnupg lsb-release

Enter fullscreen mode Exit fullscreen mode

Step 3 — Install Docker CE & Compose

# Remove any old Docker
sudo apt remove -y docker docker.io docker-engine containerd runc
sudo apt autoremove -y

# Add Docker repo
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

# Install Docker
sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin

# Start Docker & allow user access
sudo systemctl enable --now docker
sudo usermod -aG docker $USER
newgrp docker

# Test Docker
docker run hello-world

Enter fullscreen mode Exit fullscreen mode

Step 4 — Create virtual environment

python3 -m venv venv
Enter fullscreen mode Exit fullscreen mode

This will create a folder venv/ inside your project.

source venv/bin/activate
Enter fullscreen mode Exit fullscreen mode

Your shell prompt should now show (venv) at the beginning.

Install project dependencies :

pip install --upgrade pip
pip install -r requirements.txt

Enter fullscreen mode Exit fullscreen mode

Step 5 — Clone your project


git clone <YOUR_REPO_URL>
Enter fullscreen mode Exit fullscreen mode

Step 6 — Create .env for secrets

nano .env
Enter fullscreen mode Exit fullscreen mode

In your .env file have allowhost set public IP :

ALLOWED_HOSTS=<EC2_PUBLIC_IP>,localhost,127.0.0.1
Enter fullscreen mode Exit fullscreen mode

Step 7 — Adjust Django settings

Access settings.py page use cmd :

nano settings.py

for save use ctrl + o (is not zero) then Enter , Ctrl+x exit , see changes use cmd:

cat settings.py

CSRF_TRUSTED_ORIGINS = [f"http://{h}" for h in ALLOWED_HOSTS if h not in ("localhost","127.0.0.1")]

# True to False 
CSRF_COOKIE_SECURE = False     
SESSION_COOKIE_SECURE = False

Enter fullscreen mode Exit fullscreen mode

Step 8 — Build Docker image

docker build -t mydjangoapp .
Enter fullscreen mode Exit fullscreen mode

Step 9 — Configure Nginx

Create config:

sudo nano /etc/nginx/sites-available/watchmart.conf
Enter fullscreen mode Exit fullscreen mode

Paste:

server {
    listen 80;
    server_name _;

    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;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

Enter fullscreen mode Exit fullscreen mode

Check which site Nginx is serving

ls -l /etc/nginx/sites-enabled/
Enter fullscreen mode Exit fullscreen mode
  • If you see default -> /etc/nginx/sites-available/default, Nginx is still using the default config.
  • Your Django config file must be enabled and default should be removed.

Enable your config & disable default :

sudo rm /etc/nginx/sites-enabled/default
sudo ln -s /etc/nginx/sites-available/watchmart.conf /etc/nginx/sites-enabled/

Enter fullscreen mode Exit fullscreen mode

Step 10 — Run Docker container

docker run -d \
  --name mydjangoapp \
  --env-file .env \
  -p 127.0.0.1:8000:8000 \
  --restart always \
  mydjangoapp

Enter fullscreen mode Exit fullscreen mode
  • Gunicorn runs inside container on port 8000.
  • Nginx will proxy traffic from port 80 → 8000.

Check logs:

docker logs -f mydjangoapp
Enter fullscreen mode Exit fullscreen mode

Enable and test:

sudo nginx -t
sudo systemctl restart nginx
Enter fullscreen mode Exit fullscreen mode
  • If you see syntax is ok and test is successful, Nginx is now pointing to your Django container.

Open in browser

Visit:

http://<EC2_PUBLIC_IP>
Enter fullscreen mode Exit fullscreen mode
  • You should now see your Django app instead of the Nginx default page.

You’re live! 🎉

Follow on :
Git Hub
Instagram
Youtube

Top comments (0)