This guide provides a detailed walkthrough for deploying a Django application on an AWS EC2 instance running Ubuntu 22.04 LTS, using Gunicorn as the application server and Nginx as the reverse proxy.
- Cloud Provider: AWS EC2
- Instance Type: t2.medium (2 vCPUs, 4 GB RAM)
- Operating System: Ubuntu 22.04 LTS
- Programming Language: Python (Django Framework)
- Web Server: Nginx
- Application Server: Gunicorn
- Database: SQLite (can be replaced with PostgreSQL/MySQL in production)
- Process Manager: systemd
- Firewall Management: UFW
- Version Control: Git
- 🚀 Launch EC2 Instance
- 🛠️ Install Required Packages
- 📂 Clone Project & Set Up Virtual Environment
- 🔗 Configure Gunicorn
- ⚡ Set Up Nginx as Reverse Proxy
- 🔒 Secure the Application with SSL
- 🚀 Final Checks & Production Tips
- Log in to your AWS Management Console.
- Navigate to EC2 Dashboard and click on Launch Instance.
- Choose the Ubuntu 22.04 LTS AMI (Amazon Machine Image).
- Select t2.medium as the instance type (2 vCPUs, 4 GB RAM).
- Configure security groups to allow inbound traffic on:
- Port
22
(SSH) - Port
80
(HTTP) - Port
443
(HTTPS)
- Port
- Launch the instance and download the .pem key file for SSH access.
Open your terminal and SSH into the instance:
ssh -i /path/to/your-key.pem ubuntu@your-ec2-public-ip
- Update system packages and install required dependencies:
sudo apt update && sudo apt upgrade -y
sudo apt install -y python3 python3-pip python3-venv nginx gunicorn git
- Allow firewall rules for SSH, HTTP, and HTTPS:
sudo ufw allow OpenSSH
sudo ufw allow 'Nginx Full'
sudo ufw enable
- Create a project directory:
sudo mkdir /dj
cd /dj
- Clone your Django project repository:
git clone https://github.com/sunilkumar0633/social_media.git
cd social_media
- Set up a virtual environment and install dependencies:
python3 -m venv venv
source venv/bin/activate
pip install --upgrade pip
pip install -r requirements.txt
- Run database migrations:
python manage.py migrate
- Collect static files for production:
python manage.py collectstatic --noinput
- Test the application:
python manage.py runserver 0.0.0.0:8000
Check by visiting:
http://your-ec2-public-ip:8000
- Install Gunicorn:
pip install gunicorn
- Test Gunicorn by running:
gunicorn --bind 0.0.0.0:8000 social.wsgi
- Create a systemd service for Gunicorn:
sudo vim /etc/systemd/system/gunicorn.service
- Add the following configuration:
[Unit]
Description=Gunicorn daemon for Django app
After=network.target
[Service]
User=root
Group=root
WorkingDirectory=/dj/social_media
ExecStart=/dj/social_media/venv/bin/gunicorn --workers 3 --bind unix:/dj/social_media/social.sock social.wsgi:application
[Install]
WantedBy=multi-user.target
- Start and enable Gunicorn:
sudo systemctl daemon-reload
sudo systemctl enable gunicorn
sudo systemctl start gunicorn
sudo systemctl status gunicorn
- Create a new Nginx configuration:
sudo vim /etc/nginx/sites-available/social_media
- Add the following configuration:
server {
listen 80;
server_name your-domain.com; # Replace with your domain or public IP
location / {
include proxy_params;
proxy_pass http://unix:/dj/social_media/social.sock;
}
location /static/ {
root /dj/social_media;
}
location /media/ {
root /dj/social_media;
}
}
- Create a symbolic link to enable the configuration:
sudo ln -s /etc/nginx/sites-available/social_media /etc/nginx/sites-enabled/
- Test and restart Nginx:
sudo nginx -t
sudo systemctl restart nginx
sudo systemctl enable nginx
- Install Certbot for SSL configuration:
sudo apt install certbot python3-certbot-nginx
- Obtain and configure an SSL certificate:
sudo certbot --nginx -d your-domain.com
- Verify SSL and reload services:
sudo systemctl restart gunicorn
sudo systemctl restart nginx
✅ Check application status using:
sudo systemctl status gunicorn
sudo systemctl status nginx
✅ Visit your domain or public IP:
https://your-domain.com
- ✅ Successful deployment of the Django application on AWS EC2 with Gunicorn and Nginx.
- ✅ Properly configured SSL for secure HTTPS communication using Certbot.
- ✅ Application accessible via the domain name or public IP with HTTP and HTTPS.
- ✅ Hands-on experience in managing and deploying production-ready applications.
- ✅ Done! Your Django application is now successfully deployed on AWS EC2 with Gunicorn and Nginx!
- Stay updated on LinkedIn for more DevOps projects and insights.
- Follow along as I explore Cloud Infrastructure, Ansible Automation, and DevOps practices.
- Let's collaborate and build scalable solutions together!