🚀 Host a Website on AWS EC2 with NGINX – Complete Beginner Guide

If you're exploring AWS, Linux, or DevOps, learning to host a website using NGINX on an EC2 instance is a great starting point. In this post, I’ll walk you through all the key steps to get your first website live!

🔧 Prerequisites

Before we begin, make sure you have:

  • An AWS account

  • Basic Linux terminal knowledge

  • An EC2 instance (Amazon Linux 2 or Ubuntu)

  • Security group with port 80 (HTTP) open
    Optionally: open custom ports like 90 or 8080


📌 Step-by-Step Guide

1️⃣ Launch EC2 Instance

  • Choose Amazon Linux 2 or Ubuntu

  • Configure Security Group:

    • Allow port 80 (for HTTP)

    • Optionally allow port 90, 8080, etc.


2️⃣ Install NGINX

For Amazon Linux:

bashCopyEditsudo yum install nginx -y

For Ubuntu:

bashCopyEditsudo apt install nginx -y

3️⃣ Start the NGINX Service

bashCopyEditsudo service nginx start

Once NGINX is started, your EC2 instance can serve web pages!


🌐 Access the Website

  • Open your browser and visit:

      cppCopyEdithttp://<Your-EC2-Public-IP>
    
  • If you're using a custom port (e.g., 90), access like this:

      cppCopyEdithttp://<Your-EC2-Public-IP>:90
    

🛠 How NGINX Handles the Request

When a user sends a request:

  1. It reaches the NGINX server

  2. NGINX checks the server block in /etc/nginx/nginx.conf

     nginxCopyEditlisten 80;
     root /usr/share/nginx/html;
     index index.html;
    
  3. If no specific file is mentioned in the URL:

    • NGINX looks for index.html in the root folder

⚠ Common Errors

  • 403 Forbiddenindex.html is missing in the root directory

  • 404 Not Found – The requested file does not exist in the specified path


🔄 Changing the Default Port (e.g., to 90)

Step 1: Open NGINX Config File

bashCopyEditsudo nano /etc/nginx/nginx.conf

Step 2: Change the port inside the server block:

nginxCopyEditlisten 90;

Step 3: Update Security Group on EC2 to allow port 90

Step 4: Reload NGINX

bashCopyEditsudo service nginx reload

Step 5: Access the new port

cppCopyEdithttp://<Your-EC2-IP>:90

✅ Quick Summary Table

TaskCommand / Path
Install NGINXsudo yum install nginx -y
Start NGINXsudo service nginx start
Reload NGINXsudo service nginx reload
Config File/etc/nginx/nginx.conf
Web Root/usr/share/nginx/html
Access (default)http://<EC2-Public-IP>
Access (custom)http://<EC2-Public-IP>:<PORT>

💬 Final Thoughts

Deploying a simple website using NGINX on EC2 gives you a hands-on intro to:

  • How web servers work

  • Managing cloud infrastructure

  • Real-world DevOps basics

1
Subscribe to my newsletter

Read articles from Prashant Mahamuni directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Prashant Mahamuni
Prashant Mahamuni