AWS Builder Center
Deploying and Configuring NGINX on Amazon EC2: A Practical Guide to Web Server Deployment

Deploying and Configuring NGINX on Amazon EC2: A Practical Guide to Web Server Deployment

Explore how to provision an Amazon EC2 instance, configure network security, install NGINX, and deploy a web application in the AWS Cloud. This hands-on guide covers the complete deployment lifecycle, including server configuration, HTTP traffic flow, application hosting, and production readiness considerations.

AWS Student Builder | Cloud & DevOps Learner

Deploying and Configuring NGINX on Amazon EC2: A Practical Guide to Web Server Deployment

Introduction

Cloud computing has transformed how modern applications are deployed, managed, and scaled. Among the foundational services offered by Amazon Web Services (AWS), Amazon Elastic Compute Cloud (Amazon EC2) provides flexible, resizable compute capacity that enables organizations to run applications without managing physical infrastructure.
NGINX is a widely adopted, high-performance web server that also provides reverse proxy, load balancing, and HTTP caching capabilities.
In this technical walkthrough, we will deploy NGINX on an Ubuntu-based EC2 instance, configure the required network access, publish a sample web application, and examine how incoming requests travel through the AWS infrastructure.
By the end of this implementation, you will have a functional web server hosted on AWS and a clear understanding of its underlying architecture.

Architecture Overview

The deployment consists of the following components:
  • Amazon EC2: Provides the virtual compute environment.
  • Ubuntu Server: Acts as the operating system.
  • AWS Security Group: Controls inbound and outbound network traffic.
  • NGINX: Handles incoming HTTP requests and serves application content.
  • Web Application: Contains the website files delivered to users.

Prerequisites

Before proceeding, ensure that you have:
  • An active AWS account.
  • Basic familiarity with the AWS Management Console.
  • An Ubuntu EC2 instance.
  • An SSH key pair for secure access.
  • Fundamental Linux command-line knowledge.

Step 1: Provision an Amazon EC2 Instance

Navigate to the Amazon EC2 console and select Launch Instance.
Configure the instance with the following settings:
ConfigurationValue
Instance Namenginx-web-server
Operating SystemUbuntu Server LTS
Instance Typet3.micro or an appropriate alternative
AuthenticationSSH Key Pair
StorageBased on application requirements
Configure the Security Group with the following inbound rules:
ProtocolPortSource
SSH22Your public IP
HTTP800.0.0.0/0
HTTPS4430.0.0.0/0
Restrict SSH access to trusted IP addresses rather than exposing administrative access publicly.
Launch the instance and wait until it reaches the running state.

Step 2: Establish an SSH Connection

Connect to the instance using its public IPv4 address:
1
ssh -i your-key.pem ubuntu@YOUR_PUBLIC_IP
After connecting, update the operating system packages:
1
2
sudo apt update
sudo apt upgrade -y
This ensures the instance has the latest available package updates from its configured repositories.

Step 3: Install and Enable NGINX

Install NGINX:
1
sudo apt install nginx -y
Start the service:
1
sudo systemctl start nginx
Enable automatic startup:
1
sudo systemctl enable nginx
Verify the service:
1
sudo systemctl status nginx
If NGINX is running successfully, navigate to the EC2 public IP address in your browser:
1
http://YOUR_PUBLIC_IP
The default NGINX welcome page should appear, confirming that the web server is accessible.

Step 4: Deploy a Sample Web Application

NGINX commonly serves static website content from:
1
/var/www/html
Navigate to the directory:
1
cd /var/www/html
Create a sample HTML file:
1
sudo nano index.html
Add the following content:
1
2
3
4
5
6
7
8
9
10
11
12
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AWS EC2 Deployment</title>
</head>
<body>
<h1>Application Successfully Deployed!</h1>
<p>Hosted on Amazon EC2 and served by NGINX.</p>
</body>
</html>
Save the file and refresh the browser.
Your custom application should now be displayed instead of the default welcome page.

Step 5: Configure the NGINX Web Server

NGINX configuration files are located in:
1
/etc/nginx/
Open the default site configuration:
1
sudo nano /etc/nginx/sites-available/default
Configure the server block:
1
2
3
4
5
6
7
8
9
10
11
12
13
server {
listen 80;
listen [::]:80;

server_name _;

root /var/www/html;
index index.html index.htm;

location / {
try_files $uri $uri/ =404;
}
}
Validate the configuration:
1
sudo nginx -t
If the configuration test passes, reload the service:
1
sudo systemctl reload nginx
This applies the configuration without requiring a full server restart.

Step 6: Validate Application Availability

Verify the response locally:
1
curl -I http://localhost
You should receive an HTTP response indicating that the web server is responding.
Next, access the application externally:
1
http://YOUR_PUBLIC_IP
If the page loads successfully, the deployment is operational.

Understanding the Complete Request Lifecycle

When a user accesses the deployed application, the request follows this sequence:
  1. The user enters the website URL in a browser.
  2. The request travels through the internet toward the EC2 public endpoint.
  3. The AWS Security Group evaluates the inbound connection against its rules.
  4. The request reaches the EC2 instance.
  5. NGINX listens on the configured HTTP or HTTPS port.
  6. NGINX serves the requested content or forwards the request to a backend service.
  7. The response is returned to the user's browser.
This architecture demonstrates the interaction between AWS networking, compute infrastructure, and application delivery.

Production Readiness and Best Practices

Although this deployment is suitable for learning and testing, production environments require additional considerations.
Security
  • Restrict SSH access to trusted networks.
  • Configure HTTPS using a valid TLS certificate.
  • Apply operating system security updates.
  • Follow the principle of least privilege.
Availability and Scalability
  • Consider an Application Load Balancer for distributing traffic.
  • Use EC2 Auto Scaling when application demand requires multiple instances.
  • Implement health checks and monitoring.
Observability
  • Monitor instance metrics through Amazon CloudWatch.
  • Review NGINX access and error logs.
  • Establish appropriate alerting and recovery procedures.

Conclusion

Deploying NGINX on Amazon EC2 provides a practical foundation for understanding cloud-based web hosting and infrastructure management.
Through this implementation, we provisioned a virtual server, established secure administrative access, configured network permissions, installed NGINX, and deployed a functional web application.
More importantly, this exercise demonstrates how AWS compute, networking, and web server technologies work together to deliver application content.
As a next step, this architecture can be extended with HTTPS, a custom domain, an Application Load Balancer, and automated deployment pipelines.
Keep building, keep experimenting, and keep contributing to the AWS Community.
#AWS #AmazonEC2 #NGINX #AWSCommunity #CloudComputing #DevOps #Linux #WebServer
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article