Deploy and Terminate Nginx on Amazon Linux EC2

Poonam VetalPoonam Vetal
3 min read

๐Ÿ‘‹ Introduction

In this guide, Iโ€™ll show you how to set up a basic web server using Nginx on an Amazon EC2 instance. Youโ€™ll install Nginx, create your own HTML page, and access it from any browser using your EC2โ€™s public IP address.

This tutorial is for beginners who want to learn how static websites are hosted in the cloud using Linux and Nginx โ€” no Docker involved.


โ˜๏ธ Step 1: Launch EC2 Instance on AWS

  1. Go to AWS Console โ†’ EC2 โ†’ Launch Instance

  2. Choose Amazon Linux 2023 (Free Tier eligible)

  3. Select t2.micro instance type

  4. Select or create a key pair (.pem file)

  5. In Security Group, add:

    • Type: HTTP, Port: 80, Source: Anywhere (0.0.0.0/0)

    • Type: SSH, Port: 22, Source: Your IP

  6. Launch your instance ๐Ÿš€


๐Ÿ”‘ Step 2: Connect to EC2 via SSH

From your terminal, connect like this:

ssh -i your-key.pem ec2-user@<your-ec2-public-ip>
  • Replace your-key.pem with your actual key file name

  • Replace <your-ec2-public-ip> with the IP shown in the EC2 dashboard

Once connected, youโ€™ll see:

[ec2-user@ip-... ~]$

โœ… You're now logged into your EC2 Linux machine.


๐ŸŒ Step 3: Install and Start Nginx

  1. Install Nginx:
sudo dnf install nginx -y
  1. Start Nginx:
sudo systemctl start nginx
  1. Check if itโ€™s running:
sudo systemctl status nginx

You should see: active (running) โœ…


๐ŸŒ Step 4: Access Default Nginx Page in Browser

In your browser, enter:

http://<your-ec2-public-ip>

๐ŸŽ‰ Youโ€™ll see the default Nginx Welcome Page โ€” it means the web server is working correctly!


๐Ÿ“ Step 5: Add Your Own HTML Page (Static Website)

Letโ€™s replace the default page with your own:

  1. Go to Nginx web root:
cd /usr/share/nginx/html
  1. Create your own index.html:
sudo nano index.html
  1. Paste this sample content:
<!DOCTYPE html>
<html>
<head>
  <title>My Static Site</title>
</head>
<body>
  <h1>Welcome to My Website!</h1>
  <p>This page is hosted on AWS EC2 using Nginx.</p>
</body>
</html>
  1. Save and Exit:
  • Press Ctrl + O, then Enter to save

  • Press Ctrl + X to exit nano

  1. Refresh the Browser Page:
http://<your-ec2-public-ip>

๐Ÿ”ฅ Youโ€™ll now see your custom HTML page live on the internet!


๐Ÿ” Step 6: Check Services and Ports (Optional)

  1. See all running services:
systemctl list-units --type=service --state=running
  1. Check which service is using which port:
sudo ss -tulpn

Nginx will be shown using port 80.


๐Ÿ›‘ Step 7: Stop or Disable Nginx (Optional)

To stop the server:

sudo systemctl stop nginx

To prevent it from auto-starting on reboot:

sudo systemctl disable nginx

Check status again:

sudo systemctl status nginx

๐Ÿ’ก Tips and Learnings

  • EC2 is your virtual machine in the cloud ๐Ÿ–ฅ๏ธ

  • Nginx is a fast, lightweight web server ๐ŸŒ

  • You host a site by replacing the index.html file

  • Always ensure port 80 is open in the security group

  • Use systemctl to manage services and ss to inspect network ports


๐Ÿง  Bonus: Serve Your Full Website

You can upload your entire website (HTML, CSS, JS files) into /usr/share/nginx/html using:

scp -i your-key.pem index.html ec2-user@<your-ec2-ip>:/usr/share/nginx/html

Or use Git or S3 to download files directly inside EC2.



๐Ÿ‘ Conclusion

Thatโ€™s it! You just launched your first static website using Nginx on AWS EC2 โ€” no Docker or complicated tools. This method is fast, beginner-friendly, and powerful.

0
Subscribe to my newsletter

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

Written by

Poonam Vetal
Poonam Vetal

I am student from Pune institute of computer technology !๐ŸŽ“