Deploying Django REST API on AWS EC2: A Complete Step-by-Step Guide
Deploying a Django REST API on AWS EC2 is essential for businesses and developers looking to scale applications efficiently. This guide covers the entire deployment process, from local development to a fully operational cloud-based solution using AWS EC2.
Why Deploy Django REST API on AWS EC2?
Amazon Web Services (AWS) EC2 offers a powerful, scalable, and cost-effective environment for hosting Django REST applications. With flexible compute capacity and integrated security features, EC2 ensures high availability and seamless performance.
Prerequisites
An AWS account
Basic knowledge of Django and REST framework
Installed Python and Django on the local machine
SSH client (e.g., PuTTY or Terminal)
Step 1: Setting Up an AWS EC2 Instance
Login to AWS – Navigate to the AWS EC2 dashboard.
Launch a New Instance – Select an Amazon Machine Image (AMI) such as Ubuntu.
Choose an Instance Type – t2.micro is ideal for small applications.
Configure Security Group – Allow inbound HTTP, HTTPS, and SSH connections.
Generate and Download Key Pair – Save the .pem file securely for SSH access.
Launch the Instance – Start the server and obtain its public IP address.
Step 2: Connecting to the EC2 Instance
Using SSH, connect to the instance with the following command:
ssh -i your-key.pem ubuntu@your-ec2-public-ip
Once connected, update and install required packages:
sudo apt update && sudo apt upgrade -y
sudo apt install python3-pip python3-venv nginx -y
Step 3: Deploying the Django REST API
1. Transfer Project Files to EC2
Use scp or Git to transfer Django project files to the server.
scp -i your-key.pem -r /local/path/to/django ubuntu@your-ec2-public-ip:/home/ubuntu/
Alternatively, clone the repository:
git clone https://github.com/your-repo.git
2. Setting Up a Virtual Environment
cd your-django-project
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
3. Configure Django Settings
Edit settings.py to allow external access:
ALLOWED_HOSTS = ['your-ec2-public-ip', 'your-domain.com']
Run database migrations:
python manage.py migrate
Step 4: Setting Up Gunicorn and Supervisor
Install Gunicorn:
pip install gunicorn
Test the application with:
gunicorn --bind 0.0.0.0:8000 your_project.wsgi:application
Configure Supervisor to manage Gunicorn:
sudo apt install supervisor -y
sudo nano /etc/supervisor/conf.d/django.conf
Add the following:
[program:django]
command=/home/ubuntu/your-django-project/venv/bin/gunicorn --workers 3 --bind unix:/home/ubuntu/your-django-project/django.sock your_project.wsgi:application
directory=/home/ubuntu/your-django-project
autostart=true
autorestart=true
stderr_logfile=/var/log/django.err.log
stdout_logfile=/var/log/django.out.log
Restart Supervisor:
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start django
Step 5: Configuring Nginx as a Reverse Proxy
Create an Nginx configuration file:
sudo nano /etc/nginx/sites-available/django
Add the following:
server {
listen 80;
server_name your-ec2-public-ip;
location / {
include proxy_params;
proxy_pass http://unix:/home/ubuntu/your-django-project/django.sock;
}
}
Enable the Nginx configuration:
sudo ln -s /etc/nginx/sites-available/django /etc/nginx/sites-enabled
sudo systemctl restart nginx
Allow HTTP traffic:
sudo ufw allow 'Nginx Full'
Step 6: Setting Up a Domain and SSL (Optional but Recommended)
For a custom domain, update DNS records to point to the EC2 IP address. To enable HTTPS using Let’s Encrypt:
sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
Final Testing and Deployment
Access the Django REST API from the browser using the EC2 public IP or domain:
http://your-ec2-public-ip
Check for errors using logs:
sudo journalctl -u nginx --no-pager -n 50
sudo journalctl -u supervisor --no-pager -n 50
Conclusion
Deploying a Django REST API on AWS EC2 provides a scalable and reliable hosting solution. With proper configuration of Gunicorn, Supervisor, and Nginx, applications can run securely and efficiently. Implementing HTTPS further enhances security and trustworthiness.

Comments
Post a Comment