AWS + Linux Combo — Part 5: Install Nginx Web Server on EC2

By Suraj Ahir October 17, 2025 6 min read

AWS + Linux — EBS & File Systems
AWS + Linux — EBS & File Systems
← Part 4 AWS + Linux Combo · Part 5 of 12 Part 6 →

In this part, we take a big step — we turn our EC2 Linux instance into a web server. By the end of this lesson, you will have Nginx running on your cloud server, and anyone with the IP address will be able to access a web page hosted on it. This is how the internet works, and you are about to experience it firsthand.

What is Nginx?

Nginx (pronounced "engine-x") is one of the most popular web servers in the world. It is lightweight, fast, and extremely efficient at handling many simultaneous connections. Nginx powers millions of websites and is used by companies like Netflix, Cloudflare, and GitHub. It can serve static files, act as a reverse proxy in front of application servers, handle SSL/TLS termination, load-balance traffic, and much more. For our purposes, we will start by using it to serve a simple HTML page.

Step 1: Open Port 80 in Security Group

Before installing Nginx, we need to allow web traffic to reach our server. By default, your EC2 Security Group only allows SSH (port 22). We need to open port 80 (HTTP) and optionally port 443 (HTTPS).

Go to AWS Console → EC2 → Instances → click your instance → scroll to Security → click on the Security Group link. Click "Edit inbound rules" → "Add rule". Set Type to HTTP, Source to "Anywhere-IPv4" (0.0.0.0/0). Click Save rules. Now the world can reach port 80 on your server.

Step 2: Install Nginx

SSH into your EC2 instance and run these commands:

Install Nginx on Amazon Linux 2023
# Update packages first
sudo dnf update -y

# Install nginx
sudo dnf install nginx -y

# Start nginx
sudo systemctl start nginx

# Enable nginx to start automatically on reboot
sudo systemctl enable nginx

# Check status
sudo systemctl status nginx

If the status shows "active (running)" in green, Nginx is working.

Step 3: Test in Your Browser

Open a browser and go to your EC2 instance's public IP address — just http://YOUR_IP. You should see the Nginx welcome page. Congratulations — your cloud server is serving web content!

Where Nginx Serves Files From

By default, Nginx serves files from /usr/share/nginx/html/. The main configuration file is at /etc/nginx/nginx.conf. Let us put our own HTML page there:

Create Custom Web Page
# Navigate to web root
cd /usr/share/nginx/html/

# Create a custom index page
sudo nano index.html

Add this content:

Custom HTML
<!DOCTYPE html>
<html>
<head><title>My EC2 Server</title></head>
<body>
  <h1>Hello from AWS EC2!</h1>
  <p>This page is served by Nginx running on Amazon Linux in the cloud.</p>
</body>
</html>

Save the file and refresh your browser. You should see your custom page.

Basic Nginx Configuration

Understanding the Nginx config file structure helps you customize behavior:

View Nginx Config
cat /etc/nginx/nginx.conf

# The main sections are:
# worker_processes - how many worker threads
# events - connection handling settings
# http - all web server settings
#   server - defines a virtual server (website)
#     listen - which port to listen on
#     root - which directory to serve files from
#     index - default file to serve

Nginx Logs

Every request to your server is logged. This is crucial for debugging and monitoring:

View Nginx Logs
# Access log - shows all requests
sudo tail -f /var/log/nginx/access.log

# Error log - shows errors
sudo tail -f /var/log/nginx/error.log

The tail -f command follows the log file in real time. Open the log in one terminal window, then visit your website in a browser — you will see the request appear in the log instantly.

Managing Nginx Service

Nginx Service Commands
# Start / stop / restart
sudo systemctl start nginx
sudo systemctl stop nginx
sudo systemctl restart nginx

# Reload config without downtime
sudo systemctl reload nginx

# Check if nginx is running
sudo systemctl is-active nginx

What We Have Built

You now have a fully functional web server running on a Linux instance in AWS. It is serving HTTP traffic to the public internet. You created a custom HTML page that people can access by visiting your server's IP address. This is the foundation of web hosting in the cloud.

In Part 6, we will explore AWS S3 — Amazon's storage service — and learn how to interact with it from the Linux command line. S3 is used for storing images, backups, static websites, and much more.

Nginx Configuration Deep Dive

Nginx's configuration structure is hierarchical: a main context contains events and http contexts. The http context contains server blocks (virtual hosts), which contain location blocks defining how different URL paths are handled. Understanding this hierarchy enables complex configurations:

Nginx Reverse Proxy Configuration
server {
    listen 80;
    server_name myapp.example.com;

    # Serve static files directly
    location /static/ {
        alias /var/www/myapp/static/;
        expires 30d;
        add_header Cache-Control "public, immutable";
    }

    # Proxy dynamic requests to application
    location / {
        proxy_pass http://127.0.0.1:8000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_connect_timeout 30s;
        proxy_read_timeout 60s;
    }
}

SSL/TLS with Let's Encrypt

Serving your application over HTTPS is mandatory for production. Let's Encrypt provides free SSL certificates, and Certbot automates the installation and renewal process. Install Certbot: sudo apt install certbot python3-certbot-nginx. Obtain and install a certificate: sudo certbot --nginx -d yourdomain.com. Certbot automatically modifies your nginx configuration to handle HTTPS and sets up automatic renewal. Verify auto-renewal with sudo certbot renew --dry-run. Certificates are valid for 90 days and automatically renewed when less than 30 days remain.

Practice Exercise

On your EC2 instance, configure nginx as a reverse proxy for a simple application. First, run a simple Python HTTP server on port 8000: python3 -m http.server 8000 &. Then configure nginx to proxy requests from port 80 to port 8000. Update the EC2 security group to allow HTTP (port 80) from anywhere. Test from your browser using the EC2 public IP. Examine the nginx access logs at /var/log/nginx/access.log to see the requests being logged.

Disclaimer: This content is for educational purposes only. SRJahir Tech does not guarantee any specific outcome, job placement, or exam result. Learning requires consistent effort and practical application.