Skip to main content

Prerequisites

  • AWS Account with EC2 access
  • Local SSH client (or use AWS Console)
  • GitHub repository with your application
  • Docker and Docker Compose knowledge (basics)
  • .env file with environment variables

Step 1: Launch an EC2 Instance

AWS EC2 Home Screen

1.1 Create Instance

  1. Go to AWS Console → EC2 Dashboard
  2. Click “Launch Instances”
  3. Select an AMI (Amazon Machine Image)
    • Recommended: Ubuntu 22.04 LTS (free tier eligible)
  4. Choose instance type: t2.micro (free tier) or t3.small for better performance
AWS EC2 Home Screen

1.2 Configure Security Group

Configure security group to allow: Network Security Group Configuration

1.3 Connect to Instance

Option A: SSH from Local Terminal
Option B: AWS Console EC2 Instance Connect
  • Click instance → “Connect” tab“EC2 Instance Connect”
  • Browser-based terminal opens directly
SSH Connection to EC2

Step 2: Update System & Install Dependencies

Once connected to your instance:

2.1 Logout & Login


Step 3: Clone Repository & Setup SSH Keys

3.1 Generate SSH Key on EC2

3.2 Add Public Key to GitHub

  1. Go to GitHub → Settings → SSH and GPG keys
  2. Click “New SSH key”
  3. Paste the output from cat ~/.ssh/id_ed25519.pub
  4. Give it a name (e.g., “EC2 Server”)
  5. Click “Add SSH key”

3.3 Clone Your Repository

Why SSH? HTTPS requires entering credentials each time. SSH uses keys for automatic authentication.

Step 4: Environment Variables & Docker Setup

4.1 Create .env File

Create a .env file in your project root with your configuration:
⚠️ Important: Never commit .env files to git. Add .env to your .gitignore

4.2 Load Environment Variables in Docker Compose

Update your docker-compose.yml to load from .env file:

4.3 Verify Docker Files

Make sure your project has:
  • Dockerfile - Instructions to build your app image
  • docker-compose.yml - Orchestrate multiple containers

4.4 Build & Run


Step 5: Nginx Configuration (Reverse Proxy)

5.1 Install Nginx

5.2 Configure Nginx

Replace content with:

5.3 Test & Reload

✅ Now access your app via http://YOUR_PUBLIC_IP (Nginx will route to your containers)

Step 6: Setup CI/CD with GitHub Actions

6.1 Create GitHub Actions Workflow

Create .github/workflows/deploy.yml in your repository:

6.2 Add GitHub Secrets

  1. Go to GitHub → Settings → Secrets and variables → Actions
  2. Add these secrets:
    • EC2_PRIVATE_KEY: Content of your my-key.pem file
    • EC2_HOST: Your EC2 public IP address

6.3 Test Deployment

Push to main branch:
GitHub Actions will automatically:
  1. Pull latest code
  2. SSH into EC2
  3. Stop old containers
  4. Build and start new containers

Troubleshooting

Environment Variables Not Loading

Problem: Environment variables are undefined in containers

Can’t Clone Repository

Problem: “Permission denied (publickey)“

Docker Permission Denied

Port Already in Use

Nginx Not Routing Correctly


Deployment Summary


Next Steps

  • Set up SSL/TLS with Let’s Encrypt for HTTPS
  • Configure monitoring (CloudWatch, Prometheus)
  • Setup database backups and replication
  • Implement health checks and auto-recovery
  • Learn Kubernetes for scaling
🎉 Your application is now live at http://YOUR_PUBLIC_IP