Deploy a Django App to AWS EC2 with Nginx and Gunicorn
Deploying a production-grade Django application requires moving away from the built-in development server (python manage.py runserver) to a robust, fault-tolerant stack. In production architectures, Gunicorn serves as the WSGI application server to handle Python worker processes, while Nginx acts as a high-performance reverse proxy to manage incoming HTTP traffic, SSL termination, and static file serving.
This comprehensive guide walks through provisioning an AWS EC2 instance, configuring a virtual environment, setting up Gunicorn as a systemd service, and configuring Nginx.
1. EC2 Provisioning and Server Dependencies
Launch an Ubuntu 24.04 LTS instance on AWS EC2, configure your security group to allow inbound traffic on ports 22 (SSH), 80 (HTTP), and 443 (HTTPS), and SSH into your server to install system dependencies.
# Update system packages and install Python, pip, and Nginx
sudo apt update && sudo apt upgrade -y
sudo apt install python3-pip python3-dev python3-venv nginx git curl -y
2. Setting Up the Django Project and Virtual Environment
Clone your Django repository into /var/www/ or your home directory, create a Python virtual environment, and install your project dependencies.
# Navigate to web directory and clone project
cd /var/www
sudo git clone https://github.com/your-username/your-django-app.git myproject
cd myproject
# Create and activate virtual environment
python3 -m venv venv
source venv/bin/activate
# Install requirements
pip install --upgrade pip
pip install -r requirements.txt
pip install gunicorn psycopg2-binary # If using PostgreSQL
3. Configuring Gunicorn as a systemd Service
To ensure Gunicorn starts automatically on system boot and restarts if it crashes, create a systemd socket and service file for Gunicorn.
# Create Gunicorn systemd service file
sudo nano /etc/systemd/system/gunicorn.service
# Paste the following configuration:
[Unit]
Description=gunicorn daemon for Django project
After=network.target
[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/var/www/myproject
ExecStart=/var/www/myproject/venv/bin/gunicorn \
--access-logfile - \
--workers 3 \
--bind unix:/var/www/myproject/myproject.sock \
myproject.wsgi:application
[Install]
WantedBy=multi-user.target
# Start and enable Gunicorn
sudo systemctl daemon-reload
sudo systemctl start gunicorn
sudo systemctl enable gunicorn;
4. Configuring Nginx as a Reverse Proxy
Configure Nginx to pass incoming client requests to the Gunicorn Unix socket while efficiently serving static and media assets directly.
# Create Nginx server block configuration
sudo nano /etc/nginx/sites-available/myproject
# Paste the following server block:
server {
listen 80;
server_name your_domain_or_ec2_ip;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
alias /var/www/myproject/static/;
}
location /media/ {
alias /var/www/myproject/media/;
}
location / {
include proxy_params;
proxy_pass http://unix:/var/www/myproject/myproject.sock;
}
}
# Enable site and test configuration
sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx;
Conclusion
By combining an AWS EC2 instance with Gunicorn for WSGI process management and Nginx as a high-performance reverse proxy, your Django application is fully primed for secure, scalable production traffic.