How to Deploy a Django Application on AWS
There are several ways to deploy Django on AWS, but the most common and flexible method is:
EC2 (server) + Gunicorn (app server) + Nginx (reverse proxy) + RDS (database) + S3 (static/media files)
This guide walks you through everything you need.
✅ 1. Prepare Your Django Project
A. Install required packages
pip install gunicorn psycopg2-binary boto3
B. Set allowed hosts
# settings.py
ALLOWED_HOSTS = ['your-ec2-public-ip', 'your-domain.com']
C. Collect static files
python manage.py collectstatic
D. Production environment variables
Use environment variables for:
SECRET_KEY
DEBUG=False
DATABASE_URL or individual DB settings
AWS credentials (if using S3)
✅ 2. Create an EC2 Instance (Linux Server)
Steps
Go to AWS EC2 → Launch instance
Choose Ubuntu or Amazon Linux 2
Choose a small instance (t2.micro for testing)
Add storage (default is enough)
Configure security group:
Allow SSH (22) from your IP
Allow HTTP (80) from anywhere
Allow HTTPS (443) if using SSL
Create / download SSH key pair
After launch, connect via SSH:
ssh -i yourkey.pem ubuntu@your-ec2-ip
✅ 3. Install Server Dependencies on EC2
Update packages
sudo apt update && sudo apt upgrade -y
Install Python & virtual environment
sudo apt install python3-pip python3-venv -y
Create app directory
mkdir ~/django-app
cd ~/django-app
✅ 4. Clone Your Django Project
From GitHub or GitLab:
git clone https://github.com/username/yourproject.git .
✅ 5. Create and Activate Virtual Environment
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
✅ 6. Set Up Gunicorn (Django App Server)
Test Gunicorn:
gunicorn --bind 0.0.0.0:8000 yourproject.wsgi
Visit:
http://EC2-public-ip:8000
→ You should see Django running.
✅ 7. Create a Systemd Service for Gunicorn
Create service file:
sudo nano /etc/systemd/system/gunicorn.service
Paste:
[Unit]
Description=Gunicorn daemon
After=network.target
[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/home/ubuntu/django-app
ExecStart=/home/ubuntu/django-app/venv/bin/gunicorn --access-logfile - --workers 3 --bind unix:/home/ubuntu/django-app/gunicorn.sock yourproject.wsgi:application
[Install]
WantedBy=multi-user.target
Enable and start:
sudo systemctl start gunicorn
sudo systemctl enable gunicorn
✅ 8. Install & Configure Nginx
Install:
sudo apt install nginx -y
Create config:
sudo nano /etc/nginx/sites-available/django
Paste:
server {
listen 80;
server_name your-ec2-ip your-domain.com;
location / {
proxy_pass http://unix:/home/ubuntu/django-app/gunicorn.sock;
}
location /static/ {
alias /home/ubuntu/django-app/static/;
}
location /media/ {
alias /home/ubuntu/django-app/media/;
}
}
Enable config:
sudo ln -s /etc/nginx/sites-available/django /etc/nginx/sites-enabled
sudo nginx -t
sudo systemctl restart nginx
Now visit:
http://your-ec2-ip
→ Django app should load.
✅ 9. Use RDS for Your Database (Optional but Recommended)
Steps:
Open AWS RDS
Create PostgreSQL or MySQL instance
Set public access = "No"
Put EC2 and RDS in the same VPC
Copy connection details and configure Django:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'yourdbname',
'USER': 'youruser',
'PASSWORD': 'yourpassword',
'HOST': 'your-rds-endpoint',
'PORT': '5432',
}
}
Run migrations:
python manage.py migrate
✅ 10. Store Static & Media Files in S3 (Recommended)
Install packages:
pip install django-storages boto3
Configure:
INSTALLED_APPS += ['storages']
AWS_STORAGE_BUCKET_NAME = 'your-bucket'
AWS_S3_REGION_NAME = 'your-region'
AWS_S3_CUSTOM_DOMAIN = f'{AWS_STORAGE_BUCKET_NAME}.s3.amazonaws.com'
STATIC_URL = f'https://{AWS_S3_CUSTOM_DOMAIN}/static/'
MEDIA_URL = f'https://{AWS_S3_CUSTOM_DOMAIN}/media/'
Run:
python manage.py collectstatic
✅ 11. Add SSL with AWS Certificate Manager (ACM)
Steps:
Register a domain (Route53 or any provider)
Request SSL cert in AWS Certificate Manager
Add DNS validation records
Use AWS Load Balancer OR use Certbot on EC2
Certbot for EC2 (quick method):
sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d your-domain.com -d www.your-domain.com
✅ 12. Final Checklist
Item Status
Django running with Gunicorn ✅
Nginx reverse proxy ✅
RDS database connection Optional but recommended
S3 static/media Optional but recommended
SSL certificate Highly recommended
Security groups configured Essential
Backup & monitoring enabled Recommended
Summary
To deploy a Django app on AWS:
Prepare Django for production
Launch EC2 server
Install Python, clone your project
Run app with Gunicorn
Use Nginx as reverse proxy
Connect RDS for database
Store static/media on S3
Add SSL (HTTPS)
Secure everything with firewalls & permissions
This setup is reliable, scalable, and production-ready.
Learn Fullstack Python Training in Hyderabad
Read More
Setting Up Continuous Deployment (CD) for Full Stack Python Projects
Using Docker to Containerize Your Full Stack Python Application
Continuous Integration (CI) in Full Stack Python Development
Deploying a Python Web Application to Heroku
At Our Quality Thought Training Institute in Hyderabad
Subscribe by Email
Follow Updates Articles from This Blog via Email
No Comments