7 min read

How to Deploy a Django App on AWS EC2 with Gunicorn and Nginx (Step-by-Step Guide)

Django App on AWS EC2

So, you’ve built your Django app. Now it’s time to launch it online. That’s where AWS EC2 deployment comes in.

AWS lets you scale easily, control your setup, and keep costs low. In this guide, you’ll learn how to deploy a Django app on AWS EC2 with Gunicorn and Nginx.

This is a great way to create a real-world production environment. You’ll also target best practices like setting up a WSGI server (Gunicorn) and a reverse proxy (Nginx). If you’ve searched for information on how to deploy Django with Gunicorn and Nginx, this tutorial is what you need.

Don’t worry if you’re new. This tutorial is beginner-friendly. You’ll learn what each tool does and how they work together. Let’s get started!

What You Need Before You Start

Before we begin the Django EC2 deployment process, ensure you have the basics ready. Here’s what you’ll need to follow along:

  • A working Django project (any simple app is fine)
  • A free AWS account
  • Git is installed on your local machine
  • Terminal or command-line access
  • Some basic understanding of Python
  • A stable internet connection

We’ll also be using a Linux server (Ubuntu). You don’t need to be a Linux expert, but you should be comfortable using a few terminal commands.

Finally, you’ll need to create an SSH key pair in AWS. This is a safe way to log in to your cloud server. You’ll use the .pem file to connect to your EC2 instance.

Step 1: Launch an EC2 Instance on AWS

EC2 stands for Elastic Compute Cloud. It’s like renting a virtual computer in the cloud. It runs 24/7 and is perfect for hosting apps.

Here’s how to set a Django EC2 deployment:

  1. Log in to your AWS dashboard.
  2. Click EC2 under the “Compute” section.
  3. Select Launch Instance.
  4. Choose an OS. Pick Ubuntu 22.04 LTS.
  5. Choose a free-tier instance: t2.micro.
  6. Set up a new key pair (important!). Download the .pem file.
  7. In the network settings, allow the following ports:
    • Port 22 (for SSH access)
    • Port 80 (for web traffic)
    • Port 443 (for secure HTTPS)
  8. Click Launch Instance.

Once the server starts, go to your EC2 dashboard and copy the public IP address. This is how you’ll connect to it from your computer.

Step 2: Connect to Your EC2 Server

Now it’s time to log in to your EC2 instance.

  1. Set the Correct Permissions for Your Key File: Before you can use your key to SSH into your server, you need to set the proper permissions for the .pem file. This is crucial to secure the key and ensure it’s only readable by you.

Run the following command in your terminal:

chmod 400 your-key.pem

This will restrict access to the key so only you can read it.

  1. SSH into Your EC2 Instance: Now that the key file is ready, you can SSH into your EC2 instance. Use the following command to connect to your server:
ssh -i your-key.pem ubuntu@your-ec2-ip

Replace your-key.pem with the name of your key file, and your-ec2-ip with your EC2 instance’s public IP address.

Important Note: Ensure you run this command from the same directory stored in your key file. If you’re in a different directory, you can either navigate to the directory where your key file is located or provide the full or relative path to the key file.

For example, if your key file is in a different folder, you can use the full path:

ssh -i /path/to/your-key.pem ubuntu@your-ec2-ip

Or use a relative path if you’re one level up:

ssh -i ../your-key.pem ubuntu@your-ec2-ip
  1. You’re Now Logged In: Once the command is executed successfully, you will be logged in as the Ubuntu user on your EC2 instance. From here, you can manage your server and deploy your Django application.

Step 3: Set Up Your Server Environment

Your server is empty right now. Let’s prepare it for Django.

Start by updating the system packages:

sudo apt update && sudo apt upgrade -y

Then install the tools you’ll need:

sudo apt install python3-pip python3-dev python3-venv git -y

This installs Python, pip, virtual environments, and Git.

Now, move to the home directory and clone your Django project:

cd ~git clone https://github.com/your-username/your-django-app.gitcd your-django-app

Set up a virtual environment:

python3 -m venv venv
source venv/bin/activate

Install your Python packages:

pip install -r requirements.txt

If you don’t yet have a requirements.txt, you must generate it on your local machine (not the server). Here’s how you do it:

  1. On your local machine, navigate to your Django project folder.
  2. Ensure your local environment is set up and the necessary packages are installed (pip install <package_name>).

Run the following command to create one using:

pip freeze > requirements.txt

This command generates a list of all installed Python packages and their versions. Now, upload the requirements.txt to your server, and you can install the dependencies as shown above.

Step 4: Configure Django for Production

Django is built for both development and production. In production, we need a few extra settings.

Open settings.py in your project. Find the line that says DEBUG = True. Change it to:

DEBUG = False

This turns off error messages from showing in the browser.

Then, set allowed hosts:

ALLOWED_HOSTS = ['your-ec2-ip']

This tells Django to allow access from your server’s IP.

Next, run this to collect static files:

python manage.py collectstatic

Static files are images, CSS, and JavaScript. Django gathers them in one folder for easy access.

It’s also smart to move your secret key and settings to a .env file for security. The python-dotenv package can securely load them.

Step 5: Install and Run Gunicorn

Gunicorn helps deploy Django with Gunicorn and Nginx by acting as the WSGI server.

Install Gunicorn:

pip install gunicorn

Then test it by running:

gunicorn --bind 0.0.0.0:8000 your_project_name.wsgi:application

Replace your_project_name with the actual folder name of your Django project.

Now, visit http://your-ec2-ip:8000. You should see your Django app running!

Step 6: Install and Configure Nginx

Gunicorn is good, but it’s not made to face the internet alone. Nginx is a web server that helps manage traffic and static files. It sits in front of Gunicorn and sends requests to it.

Install Nginx:

sudo apt install nginx -y

Create a new config file:

sudo nano /etc/nginx/sites-available/yourproject

Paste this inside:

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

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/ubuntu/your-django-app;<br>    }
    location / {
        include proxy_params;
       proxy_pass http://127.0.0.1:8000;    }}

Enable it:

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

Test the configuration:

sudo nginx -t

Then restart Nginx:

sudo systemctl restart nginx

Now, go to http://your-ec2-ip and you’ll see your Django with Gunicorn and Nginx!

Step 7: Open Server Ports

By default, your firewall might block traffic. Let’s open it up:

sudo ufw allow 'Nginx Full'
sudo ufw enable

This opens HTTP and HTTPS access. Now, anyone can reach your app.

Step 8: Keep the App Running with Supervisor

Gunicorn will stop if your server restarts. Let’s fix that with Supervisor. It keeps apps running even if one of them crashes.

Install Supervisor:

sudo apt install supervisor -y

Create a config:

sudo nano /etc/supervisor/conf.d/yourproject.conf

Paste this:

ini[program:yourproject]
directory=/home/ubuntu/your-django-app
command=/home/ubuntu/your-django-app/venv/bin/gunicorn --workers 3
 --bind 127.0.0.1:8000 your_project_name.wsgi: application
autostart=true
autorestart=true
stderr_logfile=/var/log/yourproject.err.log
stdout_logfile=/var/log/yourproject.out.log
user=ubuntu

Then restart Supervisor:

sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start yourproject

That’s it! Now your app will stay alive even after reboots.

Final Tips and Troubleshooting

✅ If your site doesn’t load, check your EC2 security group settings.
✅ Use sudo supervisorctl status yourproject to see if Gunicorn is running.
✅ Check logs for errors: tail -f /var/log/yourproject.err.log
✅ Always restart services after making changes.

To go further, consider:

  • Adding a custom domain
  • Setting up HTTPS with Let’s Encrypt
  • Using a database like PostgreSQL

These can improve speed, safety, and professionalism.

Conclusion: You’re Live!

Great work! You’ve just deployed a Django app on AWS EC2 using real tools like Gunicorn and Nginx, which are used in actual production websites.

Now your app is online and ready for users. You’ve taken a big step as a developer.

If you enjoyed this guide, check out more deployment tutorials at Seenode.com. We make backend tech simple and fun.

Want a guide on HTTPS or domain setup next? Just ask!

Previous Post
Unlock the Power of WebSockets with FastAPI: Real-Time Apps

Deploy Django, Flask, and FastAPI in seconds

Connect the git repository and deploy your app on the fully managed Kubernetes.

Share

How to Deploy a Django App on AWS EC2 with Gunicorn and Nginx (Step-by-Step Guide)

Django App on AWS EC2

So, you’ve built your Django app. Now it’s time to launch it online. That’s where AWS EC2 deployment comes in.

AWS lets you scale easily, control your setup, and keep costs low. In this guide, you’ll learn how to deploy a Django app on AWS EC2 with Gunicorn and Nginx.

This is a great way to create a real-world production environment. You’ll also target best practices like setting up a WSGI server (Gunicorn) and a reverse proxy (Nginx). If you’ve searched for information on how to deploy Django with Gunicorn and Nginx, this tutorial is what you need.

Don’t worry if you’re new. This tutorial is beginner-friendly. You’ll learn what each tool does and how they work together. Let’s get started!

What You Need Before You Start

Before we begin the Django EC2 deployment process, ensure you have the basics ready. Here’s what you’ll need to follow along:

  • A working Django project (any simple app is fine)
  • A free AWS account
  • Git is installed on your local machine
  • Terminal or command-line access
  • Some basic understanding of Python
  • A stable internet connection

We’ll also be using a Linux server (Ubuntu). You don’t need to be a Linux expert, but you should be comfortable using a few terminal commands.

Finally, you’ll need to create an SSH key pair in AWS. This is a safe way to log in to your cloud server. You’ll use the .pem file to connect to your EC2 instance.

Step 1: Launch an EC2 Instance on AWS

EC2 stands for Elastic Compute Cloud. It’s like renting a virtual computer in the cloud. It runs 24/7 and is perfect for hosting apps.

Here’s how to set a Django EC2 deployment:

  1. Log in to your AWS dashboard.
  2. Click EC2 under the “Compute” section.
  3. Select Launch Instance.
  4. Choose an OS. Pick Ubuntu 22.04 LTS.
  5. Choose a free-tier instance: t2.micro.
  6. Set up a new key pair (important!). Download the .pem file.
  7. In the network settings, allow the following ports:
    • Port 22 (for SSH access)
    • Port 80 (for web traffic)
    • Port 443 (for secure HTTPS)
  8. Click Launch Instance.

Once the server starts, go to your EC2 dashboard and copy the public IP address. This is how you’ll connect to it from your computer.

Step 2: Connect to Your EC2 Server

Now it’s time to log in to your EC2 instance.

  1. Set the Correct Permissions for Your Key File: Before you can use your key to SSH into your server, you need to set the proper permissions for the .pem file. This is crucial to secure the key and ensure it’s only readable by you.

Run the following command in your terminal:

chmod 400 your-key.pem

This will restrict access to the key so only you can read it.

  1. SSH into Your EC2 Instance: Now that the key file is ready, you can SSH into your EC2 instance. Use the following command to connect to your server:
ssh -i your-key.pem ubuntu@your-ec2-ip

Replace your-key.pem with the name of your key file, and your-ec2-ip with your EC2 instance’s public IP address.

Important Note: Ensure you run this command from the same directory stored in your key file. If you’re in a different directory, you can either navigate to the directory where your key file is located or provide the full or relative path to the key file.

For example, if your key file is in a different folder, you can use the full path:

ssh -i /path/to/your-key.pem ubuntu@your-ec2-ip

Or use a relative path if you’re one level up:

ssh -i ../your-key.pem ubuntu@your-ec2-ip
  1. You’re Now Logged In: Once the command is executed successfully, you will be logged in as the Ubuntu user on your EC2 instance. From here, you can manage your server and deploy your Django application.

Step 3: Set Up Your Server Environment

Your server is empty right now. Let’s prepare it for Django.

Start by updating the system packages:

sudo apt update && sudo apt upgrade -y

Then install the tools you’ll need:

sudo apt install python3-pip python3-dev python3-venv git -y

This installs Python, pip, virtual environments, and Git.

Now, move to the home directory and clone your Django project:

cd ~git clone https://github.com/your-username/your-django-app.gitcd your-django-app

Set up a virtual environment:

python3 -m venv venv
source venv/bin/activate

Install your Python packages:

pip install -r requirements.txt

If you don’t yet have a requirements.txt, you must generate it on your local machine (not the server). Here’s how you do it:

  1. On your local machine, navigate to your Django project folder.
  2. Ensure your local environment is set up and the necessary packages are installed (pip install <package_name>).

Run the following command to create one using:

pip freeze > requirements.txt

This command generates a list of all installed Python packages and their versions. Now, upload the requirements.txt to your server, and you can install the dependencies as shown above.

Step 4: Configure Django for Production

Django is built for both development and production. In production, we need a few extra settings.

Open settings.py in your project. Find the line that says DEBUG = True. Change it to:

DEBUG = False

This turns off error messages from showing in the browser.

Then, set allowed hosts:

ALLOWED_HOSTS = ['your-ec2-ip']

This tells Django to allow access from your server’s IP.

Next, run this to collect static files:

python manage.py collectstatic

Static files are images, CSS, and JavaScript. Django gathers them in one folder for easy access.

It’s also smart to move your secret key and settings to a .env file for security. The python-dotenv package can securely load them.

Step 5: Install and Run Gunicorn

Gunicorn helps deploy Django with Gunicorn and Nginx by acting as the WSGI server.

Install Gunicorn:

pip install gunicorn

Then test it by running:

gunicorn --bind 0.0.0.0:8000 your_project_name.wsgi:application

Replace your_project_name with the actual folder name of your Django project.

Now, visit http://your-ec2-ip:8000. You should see your Django app running!

Step 6: Install and Configure Nginx

Gunicorn is good, but it’s not made to face the internet alone. Nginx is a web server that helps manage traffic and static files. It sits in front of Gunicorn and sends requests to it.

Install Nginx:

sudo apt install nginx -y

Create a new config file:

sudo nano /etc/nginx/sites-available/yourproject

Paste this inside:

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

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/ubuntu/your-django-app;<br>    }
    location / {
        include proxy_params;
       proxy_pass http://127.0.0.1:8000;    }}

Enable it:

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

Test the configuration:

sudo nginx -t

Then restart Nginx:

sudo systemctl restart nginx

Now, go to http://your-ec2-ip and you’ll see your Django with Gunicorn and Nginx!

Step 7: Open Server Ports

By default, your firewall might block traffic. Let’s open it up:

sudo ufw allow 'Nginx Full'
sudo ufw enable

This opens HTTP and HTTPS access. Now, anyone can reach your app.

Step 8: Keep the App Running with Supervisor

Gunicorn will stop if your server restarts. Let’s fix that with Supervisor. It keeps apps running even if one of them crashes.

Install Supervisor:

sudo apt install supervisor -y

Create a config:

sudo nano /etc/supervisor/conf.d/yourproject.conf

Paste this:

ini[program:yourproject]
directory=/home/ubuntu/your-django-app
command=/home/ubuntu/your-django-app/venv/bin/gunicorn --workers 3
 --bind 127.0.0.1:8000 your_project_name.wsgi: application
autostart=true
autorestart=true
stderr_logfile=/var/log/yourproject.err.log
stdout_logfile=/var/log/yourproject.out.log
user=ubuntu

Then restart Supervisor:

sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start yourproject

That’s it! Now your app will stay alive even after reboots.

Final Tips and Troubleshooting

✅ If your site doesn’t load, check your EC2 security group settings.
✅ Use sudo supervisorctl status yourproject to see if Gunicorn is running.
✅ Check logs for errors: tail -f /var/log/yourproject.err.log
✅ Always restart services after making changes.

To go further, consider:

  • Adding a custom domain
  • Setting up HTTPS with Let’s Encrypt
  • Using a database like PostgreSQL

These can improve speed, safety, and professionalism.

Conclusion: You’re Live!

Great work! You’ve just deployed a Django app on AWS EC2 using real tools like Gunicorn and Nginx, which are used in actual production websites.

Now your app is online and ready for users. You’ve taken a big step as a developer.

If you enjoyed this guide, check out more deployment tutorials at Seenode.com. We make backend tech simple and fun.

Want a guide on HTTPS or domain setup next? Just ask!

Previous Post
Unlock the Power of WebSockets with FastAPI: Real-Time Apps

Share