Skip to content

aboderinsamuel/aws-ec2

Repository files navigation

AWS EC2 Setup Guide - From Zero to Hosted Website

Step 1: Install AWS CLI (Ubuntu/WSL Terminal)

sudo apt update
sudo apt install awscli -y

Step 2: AWS Configure

Run this command and enter 4 things:

aws configure

You'll be prompted for:

Prompt Where to find it
AWS Access Key ID AWS Console → Account name (top right) → Security credentials → Access keys → Create access key
AWS Secret Access Key Shown ONCE when you create the key — download the .csv file immediately
Default region e.g. us-east-2 (check your AWS console URL for your region)
Default output format Just press Enter (defaults to json)

IMPORTANT: Never share your keys or push them to GitHub.


Step 3: Key Pair Setup

When creating an EC2 instance, AWS gives you a .pem file (your SSH key). Move it to a safe location:

# Copy from Windows Downloads to WSL SSH folder
cp /mnt/c/Users/YOUR_USERNAME/Downloads/your-key.pem ~/.ssh/

# Lock down permissions (SSH requires this)
chmod 400 ~/.ssh/your-key.pem

Step 4: SSH Into Your Instance

ssh -i ~/.ssh/your-key.pem ubuntu@YOUR_PUBLIC_IP

Username depends on AMI:

  • Ubuntu → ubuntu
  • Amazon Linux → ec2-user
  • Debian → admin

Step 5: Security Group - Open Port 80

Your website won't load without this. In the AWS Console:

  1. EC2 → your instance → Security tab
  2. Click the security group
  3. Edit inbound rules
  4. Add rule: Type = HTTP, Port = 80, Source = 0.0.0.0/0

Step 6: User Data Scripts

Paste these into the User Data field under Advanced Details when launching an EC2 instance.

Which script to use:

  • Next.js / Node.js app from GitHub → see scripts/nextjs-ubuntu.sh
  • Static HTML website (no repo needed) → see scripts/static-nginx-ubuntu.sh
  • Any public repo (React/Vite) → see scripts/react-ubuntu.sh

Useful AWS CLI Commands

# List all instances with ID, status, and IP
aws ec2 describe-instances --region us-east-2 \
  --query "Reservations[*].Instances[*].[InstanceId,State.Name,PublicIpAddress]" \
  --output table

# Stop an instance
aws ec2 stop-instances --instance-ids i-XXXXX --region us-east-2

# Start an instance
aws ec2 start-instances --instance-ids i-XXXXX --region us-east-2

# Terminate (permanently delete) an instance
aws ec2 terminate-instances --instance-ids i-XXXXX --region us-east-2

SSH Config Shortcut (Optional)

Edit ~/.ssh/config to avoid typing the full SSH command every time:

Host myserver
    HostName YOUR_PUBLIC_IP
    User ubuntu
    IdentityFile ~/.ssh/your-key.pem

Then just type:

ssh myserver

Quick Checklist Before Launching

  • AMI selected (Ubuntu or Amazon Linux)
  • Key pair selected (existing or new)
  • User Data script pasted in Advanced Details
  • Security Group allows HTTP (port 80) from 0.0.0.0/0
  • After launch, wait 2-3 minutes for script to finish
  • Visit http://YOUR_PUBLIC_IP (not https)

Troubleshooting

# Check if the startup script ran successfully
cat /var/log/cloud-init-output.log

# Check if app is running (Node.js apps)
pm2 list

# Check if build folder exists (Next.js)
ls /var/www/mysite/.next

# Check available memory
free -m

# If build fails due to low memory, add swap:
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages