This repository contains an automation script for deploying a Django REST API project on an EC2 instance with Nginx and Gunicorn. This script is specifically designed as an example for deploying the project from django_rest_project_management_api.
Note: The Nginx and Gunicorn configurations in this script are tailored for the example repository mentioned above. You should modify these configurations according to your specific project requirements if using with a different Django project.
-
AWS Account Setup:
- Create an AWS account if you haven't already
- Create an IAM user and generate Access Key ID and Secret Access Key
- Note down these credentials for AWS CLI configuration
-
AWS CLI Setup:
# Create a virtual environment python -m venv awsvenv # Activate the virtual environment .\awsvenv\Scripts\activate # Install AWS CLI pip install awscli # Configure AWS CLI aws configure # Enter your AWS Access Key ID # Enter your AWS Secret Access Key # Enter your preferred region (e.g., ap-south-1) # Enter output format (json)
-
Security Group Setup:
- Create a security group in AWS Console
- Add inbound rule for HTTP (Port 80) from anywhere (0.0.0.0/0)
- Add inbound rule for SSH (Port 22) from your IP
- Note down the security group ID
-
SSH Key Setup:
- Create a key pair in AWS Console
- Download the .pem file
- Place it in your working directory
Note: Replace the following placeholders in the script:
<YOUR-AMI-ID>: Your Ubuntu Server 24.04 LTS AMI ID | (by_default-> "ami-053b12d3152c0cc71" )<YOUR-KEY-PAIR-NAME>: Name of your key pair<YOUR-SECURITY-GROUP-ID>: Your security group ID<YOUR-PEM-FILE>: Name of your .pem file<YOUR-SERVER-NAME>: Name tag for your EC2 instance
-
Create a file named
deployment.shwith the following content:#!/bin/bash # Step 1: Launch EC2 Instance INSTANCE_ID=$(aws ec2 run-instances \ --image-id "<YOUR-AMI-ID>" \ --instance-type "t2.micro" \ --key-name "<YOUR-KEY-PAIR-NAME>" \ --block-device-mappings '[{"DeviceName":"/dev/sda1","Ebs":{"VolumeSize":8,"VolumeType":"gp3"}}]' \ --network-interfaces '{"AssociatePublicIpAddress":true,"DeviceIndex":0,"Groups":["<YOUR-SECURITY-GROUP-ID>"]}' \ --tag-specifications '{"ResourceType":"instance","Tags":[{"Key":"Name","Value":"<YOUR-SERVER-NAME>"}]}' \ --count "1" \ --query 'Instances[0].InstanceId' \ --output text) echo "Launched EC2 Instance ID: $INSTANCE_ID" # Wait for 10 seconds instance to be ready echo "Waiting for 10 seconds for the instance to be ready..." sleep 10 # Step 3: Fetch Public IP PUBLIC_IP=$(aws ec2 describe-instances \ --instance-ids "$INSTANCE_ID" \ --query "Reservations[*].Instances[?State.Name=='running'].[PublicIpAddress]" \ --output text) if [[ -z "$PUBLIC_IP" ]]; then echo "Failed to fetch public IP. Terminating instance." aws ec2 terminate-instances --instance-ids "$INSTANCE_ID" exit 1 fi echo "Public IP: $PUBLIC_IP" echo "Sleeping for 10 seconds to allow the ssh port to start properly in the instance..." sleep 10 # Step 4: Deploy application ssh -o StrictHostKeyChecking=no -i <YOUR-PEM-FILE>.pem ubuntu@$PUBLIC_IP "git clone https://github.com/centaurusgod/bash_django_server_deployment.git && cp bash_django_server_deployment/automate.sh /home/ubuntu/ && chmod +x /home/ubuntu/automate.sh && sed -i 's|your_ec2_instance_public_ip|$PUBLIC_IP|' /home/ubuntu/automate.sh && bash /home/ubuntu/automate.sh" # Step 5: Open browser msedge http://$PUBLIC_IP/api/docs/
-
Open Git Bash in your working directory and run:
# In windows bash deployment.sh # In linux chmod +x deployment.sh ./deployment.sh
# Connect to EC2 instance
ssh -i "your-key.pem" ubuntu@your-ec2-public-ip
# Create the automation script file in the home directory
nano automate.sh
# Copy the contents of the script into the file
# Save and exit
# Make the script executable
chmod +x automate.sh
# Run the script
./automate.sh- Fetch the Update So the necessary package urls can be reached
- Installs required dependencies (virtualenv, nginx, gunicorn)
- Clones the Django project repository
- Sets up a Python virtual environment
- Installs project dependencies
- Configures Django settings (SECRET_KEY, DEBUG, ALLOWED_HOSTS)
- Sets up Nginx configuration
- Configures Gunicorn service
- Sets appropriate file permissions
- Starts and enables required services
- System Updates: Initial system preparation
- Dependencies: Installation of required packages
- Project Setup: Django project configuration
- Nginx Configuration: Web server setup for the example project
- Gunicorn Setup: WSGI server configuration
- Security Settings: Basic security configurations
- Service Configuration: System service setup
- Error Handling: Add proper error checking for each command execution
- Logging: Implement comprehensive logging for better debugging
- Command Success Verification: Add checks to verify if each step completed successfully
- Rollback Mechanism: Implement rollback functionality for failed deployments
- AWS CLI Integration: Future improvement to automatically fetch EC2 public IP using AWS CLI
- Environment Variables: Move hardcoded values to environment variables
- Backup Mechanism: Add backup functionality before making changes
- Security Enhancements: Implement additional security checks and validations