Skip to content

KokBoakye/Static-website-deploy-to-EC2

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

14 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Deploying My Static HTML Website to AWS EC2 via GitHub Actions

This project is automatically deployed to an Ubuntu EC2 instance using GitHub Actions whenever I push to the main branch. The site is served using Nginx.

πŸ“ Project File Structure Resume-website/ β”œβ”€β”€ index.html β”œβ”€β”€ css/ β”‚ └── style.css β”œβ”€β”€ images/ β”‚ └── pic.jpg └── .github/ └── workflows/ └── deploy_static_website_to_ec2.yml

  1. Launch and Configure Your EC2 Instance Create an Ubuntu EC2 instance on AWS. Security Group: allow ports 22 (SSH), 80 (HTTP). Download your .pem key (for initial SSH setup).

  2. SSH into EC2 and Set Up Nginx From your machine:

chmod 400 your-key.pem ssh -i your-key.pem ubuntu@<EC2_PUBLIC_IP> On the EC2 instance:

sudo apt update sudo apt install nginx -y sudo systemctl enable nginx sudo systemctl start nginx Ensure Nginx is serving from /var/www/html: ls /var/www/html

  1. Add SSH Secrets to GitHub In your GitHub repo:

Go to Settings > Secrets and variables > Actions Add the following secrets: Secret Name Description EC2_HOST: Your EC2 public IP EC2_SSH_KEY: private key ( CAT you priavte key and Paste the contents into the EC2_SSH_KEY secret)

  1. GitHub Actions Workflow Add this to .github/workflows/static_website_to_ec2.yml:

name: Deploy Static Site to EC2

on: push: branches: - main

jobs: deploy: runs-on: ubuntu-latest

steps:
  - name: Checkout code
    uses: actions/checkout@v4

  - name: Set up SSH key
    run: |
      mkdir -p ~/.ssh
      echo "${{ secrets.EC2_SSH_KEY }}" | base64 -d > ~/.ssh/id_rsa
      chmod 600 ~/.ssh/id_rsa
      ssh-keyscan -H ${{ secrets.EC2_HOST }} >> ~/.ssh/known_hosts

  - name: Copy files to temporary folder
    run: |
      rsync -avz --exclude='.git' ./ ubuntu@${{ secrets.EC2_HOST }}:/home/ubuntu/temp-site

  - name: Move files to web root
    run: |
      ssh ubuntu@${{ secrets.EC2_HOST }} << 'EOF'
        sudo rm -rf /var/www/html/*
        sudo cp -r /home/ubuntu/temp-site/* /var/www/html/
      EOF
  1. Deploy Push to main, and your site will be deployed to:

http://<EC2_PUBLIC_IP>/

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages