django python nginx gunicorn

How to Deploy a Django Project on AWS EC2 with Ubuntu, Gunicorn, and Nginx

Deploying your Django project to a live server might seem hard at first, but don’t worry! In this blog, I’ll guide you step-by-step to deploy your Django app on an AWS EC2 instance using Ubuntu, Gunicorn, and Nginx.

Step 1: Install Required Packages

Update the package and the Linux

sudo apt update && sudo apt upgrade -y

Install Python Nginx dependencies:

sudo apt install python3-pip python3-venv python3-dev libpq-dev nginx curl git -y

Step 2: Create a Virtual Environment and Activate

python3 -m venv myenv
source myenv/bin/activate

Step 3: Clone or Upload Your Django Project

Clone your project to AWS. Generally, the AWS user name is Ubuntu

git clone https://github.com/ubuntu/yourproject.git
cd yourproject

Install all the project’s libraries:

pip install -r requirements.txt

Step 4: Configure Django Settings

Add your EC2 IP or domain in ALLOWED_HOSTS like this

ALLOWED_HOSTS = [‘aws-ec2-ip’, ‘yourdomain.com’]

Step 5: Install PostgreSQL on Ubuntu

sudo apt update
sudo apt install postgresql postgresql-contrib -y

Check PostgreSQL Status and Start PostgreSQL

sudo systemctl status postgresql
sudo systemctl start postgresql

Switch to the postgres User and create a Database

sudo -i -u postgres
psql
\l

create database

CREATE DATABASE ensdb;
--set password
ALTER USER postgres WITH PASSWORD 'new_secure_password';

Exit psql and the user:

\q
exit

Step 4: Migrate and collect static files

python3 manage.py makemigrations
python3 manage.py migrate

#now collaect all static files
python3 manage.py collectstatic

Create a superuser for the django admin

python3 manage.py createsuperuser

Step 5: Install & Run Gunicorn

pip install gunicorn

Run Gunicorn and verify it’s working :

gunicorn --workers 3 myproject.wsgi:application

Test it by accessing http://your-ec2-ip:8000 if Gunicorn runs.

Step 6: Run Gunicorn in the Background with Systemd

Create a socket file

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

Please copy and paste the code below into the socket file and save it

[Unit]
Description=gunicorn socket

[Socket]
ListenStream=/run/gunicorn.sock

[Install]
WantedBy=sockets.target

Now, create a service file

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

Be careful with the socket file because your project depends on this configuration. Make sure your path to the project is correct

[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target

[Service]
User=root
Group=www-data
WorkingDirectory=/home/ubuntu/project-dir/myproject
ExecStart=/home/ubuntu/project-dir/env/bin/gunicorn \
          --access-logfile - \
          --workers 3 \
          --bind unix:/run/gunicorn.sock \
          myproject.wsgi:application

[Install]
WantedBy=multi-user.target

myproject is inside the project dir, that’s why my env is inside it. For any error, comment me, and I will help you out

Enable and start it:

sudo systemctl start gunicorn.socket
sudo systemctl enable gunicorn.socke

Restart Gunicorn and check the status:

sudo systemctl daemon-reload
sudo systemctl restart gunicorn
sudo systemctl status gunicorn

Step 7: Set up Nginx

We have already installed nginx, now it is time to configure it

sudo nano /etc/nginx/sites-available/myproject
server {
    listen 80;
    server_name mysite.com;

    location = /favicon.ico { access_log off; log_not_found off; }

    location /static/ {
        alias /home/ubuntu/project-dir/myproject/myproject/static/;
    }

    location /media/ {
        alias /home/ubuntu/project-dir/myproject/media/;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }
}

Run the commands to create a symlink and allow nginx to fully

sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled
sudo nginx -t
sudo systemctl restart nginx
sudo ufw allow 'Nginx Full'

Open the Nginx configuration file

sudo nano /etc/nginx/nginx.conf

Edit the user directive, Find the top line that looks like:

user www-data;

Change it to your desired user (e.g., ubuntu):

user ubuntu;

Check if your user has permission

Ensure the user you set has permission to access:

  • Nginx log directories (/var/log/nginx)
  • Static/media file directories
  • Gunicorn socket file (if using socket)

Test Nginx configuration

sudo nginx -t

Comment below if you have an issue with deployment, and I will help you

Get Lifetime Free SSL:

To get a free SSL certificate, create a Cloudflare account and configure your DNS records there

1 thought on “How to Deploy a Django Project on AWS EC2 with Ubuntu, Gunicorn, and Nginx”

Leave a Comment

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