Skip to content

A bash script for automated Django project deployment on AWS EC2. Uses AWS CLI to create EC2 instances, handles system setup, Nginx configuration, Gunicorn service, and security settings in one go.

Notifications You must be signed in to change notification settings

centaurusgod/bash_django_server_deployment

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 

Repository files navigation

Django Server Deployment Automation Script

⚠️ Note: This script is a work in progress and needs several improvements

⚠️ Compatibility Note: This script is specifically designed and tested for Ubuntu Server 24.04 LTS and has not been tested with other distributions.

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.

Deployment Methods

Method 1: Using AWS CLI & Git Bash (Windows)

Prerequisites

  1. 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
  2. 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)
  3. 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
  4. SSH Key Setup:

    • Create a key pair in AWS Console
    • Download the .pem file
    • Place it in your working directory

Deployment Steps

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
  1. Create a file named deployment.sh with 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/
  2. Open Git Bash in your working directory and run:

    # In windows
    bash deployment.sh
    
    # In linux
    chmod +x deployment.sh
    ./deployment.sh

Method 2: (Slightly Manual) Using SSH with .pem file

# 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

What the Script Does

  1. Fetch the Update So the necessary package urls can be reached
  2. Installs required dependencies (virtualenv, nginx, gunicorn)
  3. Clones the Django project repository
  4. Sets up a Python virtual environment
  5. Installs project dependencies
  6. Configures Django settings (SECRET_KEY, DEBUG, ALLOWED_HOSTS)
  7. Sets up Nginx configuration
  8. Configures Gunicorn service
  9. Sets appropriate file permissions
  10. Starts and enables required services

Script Components

  • 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

Important Improvements Needed

  1. Error Handling: Add proper error checking for each command execution
  2. Logging: Implement comprehensive logging for better debugging
  3. Command Success Verification: Add checks to verify if each step completed successfully
  4. Rollback Mechanism: Implement rollback functionality for failed deployments
  5. AWS CLI Integration: Future improvement to automatically fetch EC2 public IP using AWS CLI
  6. Environment Variables: Move hardcoded values to environment variables
  7. Backup Mechanism: Add backup functionality before making changes
  8. Security Enhancements: Implement additional security checks and validations

About

A bash script for automated Django project deployment on AWS EC2. Uses AWS CLI to create EC2 instances, handles system setup, Nginx configuration, Gunicorn service, and security settings in one go.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages