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
-
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).
-
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
- 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)
- 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
- Deploy Push to main, and your site will be deployed to:
http://<EC2_PUBLIC_IP>/