DEV Community

Cover image for Deploying an NGINX Web Server on AWS: A Practical Project Guide
Goodluck Ekeoma Adiole
Goodluck Ekeoma Adiole

Posted on

Deploying an NGINX Web Server on AWS: A Practical Project Guide

# Deploying an NGINX Web Server on AWS: A Practical Project Guide

Project Overview

In this project, we will deploy an NGINX web server on an AWS EC2 instance, demonstrating fundamental DevOps and cloud infrastructure skills. The setup will involve installing and configuring NGINX, serving a custom HTML page, and ensuring persistent service availability. This guide will specifically document the deployment of nginx-server-hng, launched in the us-east-1 region within the us-east-1c availability zone.

Project Objectives

  • Deploy an EC2 instance on AWS.
  • Install and configure NGINX.
  • Serve a custom HTML web page.
  • Automate the process using a User Data script.
  • Ensure high availability and accessibility.

Use Cases of NGINX

NGINX is a versatile, high-performance web server with several use cases, including:

  • Serving Static Content: Efficiently delivers HTML, CSS, and JavaScript files.
  • Reverse Proxying: Distributes client requests to backend servers.
  • Load Balancing: Enhances application performance and scalability.
  • SSL Termination: Implements HTTPS security.
  • Microservices Architecture: Facilitates communication between containerized services.

Step-by-Step Implementation

Step 1: Launch an AWS EC2 Instance

  1. Log in to the AWS Management Console.
  2. Navigate to EC2 > Launch Instance.
  3. Choose Ubuntu 22.04 LTS as the Amazon Machine Image (AMI).
  4. Select an instance type (t2.micro for free tier eligibility).
  5. Configure the instance:
    • Name: nginx-server-hng
    • Region: us-east-1
    • Availability Zone: us-east-1c
  6. Set up security group rules:
    • Allow SSH (port 22) for remote access.
    • Allow HTTP (port 80) for web traffic.
  7. Create or use an existing key pair.
  8. Click Launch Instance.

Step 2: Connect to the EC2 Instance

Once the instance is running, establish an SSH connection:

ssh -i your-key.pem ubuntu@your-instance-public-ip
Enter fullscreen mode Exit fullscreen mode

Step 3: Install and Configure NGINX

Update the package list and install NGINX:

sudo apt update && sudo apt upgrade -y
sudo apt install nginx -y
Enter fullscreen mode Exit fullscreen mode

Start and enable the NGINX service:

sudo systemctl start nginx
sudo systemctl enable nginx
Enter fullscreen mode Exit fullscreen mode

Step 4: Configure NGINX to Serve a Custom Web Page

Navigate to the web root directory:

cd /var/www/html
Enter fullscreen mode Exit fullscreen mode

Create a new index.html file with a styled welcome message:

echo '<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>DevOps Stage 0</title>
    <style>
        body { background-color: #282c34; color: white; text-align: center; font-family: Arial; padding: 50px; }
        h1 { color: #61dafb; }
    </style>
</head>
<body>
    <h1>Welcome to DevOps Stage 0</h1>
    <p>AdioleGoodluck/DevOpsEngineer</p>
</body>
</html>' | sudo tee /var/www/html/index.html
Enter fullscreen mode Exit fullscreen mode

Adjust file permissions:

sudo chmod 644 /var/www/html/index.html
Enter fullscreen mode Exit fullscreen mode

Step 5: Restart NGINX and Verify Deployment

Restart NGINX to apply changes:

sudo systemctl restart nginx
Enter fullscreen mode Exit fullscreen mode

Open a web browser and navigate to:

http://your-instance-public-ip
Enter fullscreen mode Exit fullscreen mode

You should see the custom-styled webpage displaying the welcome message.

Image description

Challenges Faced and Solutions

1. NGINX Not Starting Automatically After Reboot

  • Issue: After restarting the EC2 instance, NGINX did not start automatically.
  • Solution: Used the following command to enable the service on boot:
sudo systemctl enable nginx
Enter fullscreen mode Exit fullscreen mode

2. Permission Issues with HTML File

  • Issue: NGINX could not read the index.html file.
  • Solution: Modified file permissions using:
sudo chmod 644 /var/www/html/index.html
Enter fullscreen mode Exit fullscreen mode

Automating Deployment with User Data

To automate the entire setup, add the following User Data script when launching the instance:

#!/bin/bash
apt update -y
apt install nginx -y
systemctl start nginx
systemctl enable nginx
echo '<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>DevOps Stage 0</title>
    <style>
        body { background-color: #282c34; color: white; text-align: center; font-family: Arial; padding: 50px; }
        h1 { color: #61dafb; }
    </style>
</head>
<body>
    <h1>Welcome to DevOps Stage 0</h1>
    <p>AdioleGoodluck/DevOpsEngineer</p>
</body>
</html>' > /var/www/html/index.html
systemctl restart nginx
Enter fullscreen mode Exit fullscreen mode

How This Task Contributes to My Professional Goals

This project enhanced my cloud engineering and DevOps skills by giving hands-on experience with:

  • Infrastructure as a Service (IaaS): Deploying and managing cloud-based servers on AWS.
  • Automation: Using shell scripts and User Data to streamline deployments.
  • Web Server Management: Configuring and securing NGINX for production-ready applications.

By mastering these foundational concepts, I am better equipped for roles in DevOps Engineering (Hire DevOps Engineers) and Cloud Engineering (Hire Cloud Engineers).

Image description

Project Conclusion

Deploying an NGINX web server on AWS is a fundamental skill for DevOps engineers. This project covered launching an EC2 instance, installing and configuring NGINX, and automating the setup using User Data scripts. The resulting server is capable of handling HTTP requests efficiently, making it an excellent starting point for more complex web applications.

However, future improvements can include SSL encryption with Let’s Encrypt, load balancing with AWS ELB, and containerizing NGINX using Docker.
Additionally, the infrastructure deployment can be automated using Terraform.
**_

Would you like to explore these enhancements in a follow-up project?
_** πŸš€

Top comments (0)