DEV Community

alok-38
alok-38

Posted on

How to get NginX on AWS serve a static webpage to the web?

How to Configure Nginx and Reverse Proxy to Serve a Static Webpage

1️⃣ Launch EC2 Free Tier Instance

  • Chose default VPC and default subnet
  • Enabled Auto-assign Public IP (required for SSH & HTTP access)
  • Created a security group with inbound rules:
    • SSH (port 22) → My IP only
    • HTTP (port 80) → Anywhere (0.0.0.0/0)
  • Outbound rules: allow all (default, fine for updates)

2️⃣ SSH into EC2 Instance

  • Set proper permissions for the .pem key:
chmod 400 ~/DevOps.pem
Enter fullscreen mode Exit fullscreen mode
  • Connected using the correct username (depends on AMI):
    • Amazon Linux → ec2-user
    • Ubuntu → ubuntu
ssh -i ~/DevOps.pem ec2-user@YOUR_PUBLIC_IP
Enter fullscreen mode Exit fullscreen mode
  • If you get Permission denied, check:
    • Correct username for your AMI
    • .pem file permissions

3️⃣ Update the EC2 Instance

sudo yum update -y
Enter fullscreen mode Exit fullscreen mode

4️⃣ Install Nginx

sudo yum install nginx -y
Enter fullscreen mode Exit fullscreen mode
  • Start & enable Nginx:
sudo systemctl start nginx
sudo systemctl enable nginx
Enter fullscreen mode Exit fullscreen mode

5️⃣ Copy Local Files to EC2

  • From WSL, copied a folder or file to EC2:
scp -i ~/DevOps.pem -r ~/DevOps ec2-user@YOUR_PUBLIC_IP:/home/ec2-user/
Enter fullscreen mode Exit fullscreen mode
  • -r → recursive, copies entire folder including subfolders

6️⃣ Move Files to Nginx Root

  • Nginx serves files from its root directory:

  • Move folder into Nginx root:

sudo mv ~/DevOps /usr/share/nginx/html/
Enter fullscreen mode Exit fullscreen mode
  • Folder structure after move:
/usr/share/nginx/html/DevOps/index.html
Enter fullscreen mode Exit fullscreen mode

7️⃣ Fix Permissions

sudo chmod -R 755 /usr/share/nginx/html/DevOps
Enter fullscreen mode Exit fullscreen mode

8️⃣ Test Your Site

  • From EC2 itself:
curl http://localhost/DevOps/index.html
Enter fullscreen mode Exit fullscreen mode

✅ Outcome

  • EC2 instance is updated and running

  • Nginx is installed, started, and enabled on boot

  • Your folder DevOps is served by Nginx publicly

  • Permissions are correct — files are accessible without exposing the server unnecessarily

Top comments (0)