How I Set Up and Configured NGINX on Ubuntu

I recently got accepted into the HNG12 internship programme and because of my interest on cloud infrastructure management, I joined the DevOps track.

As part of my DevOps internship, I was tasked with setting up and configuring NGINX on a fresh Ubuntu server. This task serves as an introduction to working with basic web server configurations and demonstrates my ability to deploy a functional web server.

In this blog, I document my step-by-step approach, the challenges I faced, and how this experience contributes to my professional growth.

Step-by-Step Procedure for NGINX Installation and Configuration

Here is the step-by-step procedure I followed to achieve this task

Set Up a Fresh Ubuntu Server

If you don’t already have a server, you can create one on a cloud platform like AWS, DigitalOcean, or Azure. Ensure the server is running Ubuntu (e.g., Ubuntu 22.04 LTS).

In my case, I utilized the AWS console by taking advantage of the Free-tier account. This was set up by launching an EC2 instance running a Ubuntu Server.

SSH into your server using the terminal or an SSH client like PuTTY or Mobaxterm:

I was able to connect to my virtual machine using Mobaxterm terminal, an open source ssh client, first by navigating to the keypair .pem file created on AWS console and which was downloaded on my computer.

I modified the permissions using

chmod 400 Keypairname.pem

Then I connected to my Linux server

Step 1: Update and Upgrade Ubuntu Packages

Before installing any software, updating and upgrading the package list is best practice to ensure the latest versions are installed. Run this command to achieve this:

sudo apt update && sudo apt upgrade -y

Step 2: Install NGINX

To install NGINX, run the following command:

sudo apt install nginx -y

Step 3: Start and Enable NGINX

Once installed, start the NGINX service and ensure it runs at startup:

sudo systemctl start nginx

Step 4: Verify NGINX is Running

To confirm that NGINX is active and running, run the command:

sudo systemctl status nginx

Alternatively, you can check if NGINX is serving content by opening your server’s public IP in a browser:

http://<your-server-ip>

You should see the default NGINX welcome page.

Step 5: Configure the Firewall

Allow HTTP traffic (port 80) through the firewall:

sudo ufw allow 'Nginx HTTP'

Step 6: Configure a Custom HTML Page

The next step is to replace the default NGINX page with a custom HTML page.

Create the HTML File

In this location /var/www/html/index.html by running this command:

sudo nano /var/www/html/index.html

This opens an editable file.

Add the following content to the file:

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Welcome to DevOps Stage 0</title>

</head>

<body>

<h1>Welcome to DevOps Stage 0 - [Aishat Oshileye]/[Indigotech]</h1>

</body>

</html>

Save and exit the file by using these keys ‘Ctrl + X’, then ‘Y’, then ‘Enter’).

Step 7: Restart NGINX

After making changes, restart the NGINX server:

sudo systemctl restart nginx

Then use the command curl ifconfig.me to verify the Setup

Find your server’s public IP (highlighted in blue)

Step 8: Test the Configuration

Open your web browser and navigate to:

http://<your-server-ip>

If configured correctly, you should see the custom message:

Welcome to DevOps Stage 0 - Aishat Oshileye/Indigotech

Challenges and How I Overcame Them

1. NGINX Not Starting

One issue I encountered was that NGINX failed to start after installation. Checking the status with:

sudo systemctl status nginx revealed a conflict with another service running on port 80. I resolved this by stopping the conflicting service and restarting NGINX.

sudo systemctl stop

sudo systemctl restart nginx

2. Server Not Accessible from Browser

Initially, my custom page wasn’t loading. I checked the firewall settings:

sudo ufw allow 'Nginx HTTP'

This ensured that HTTP traffic could reach the server.

Learning and Professional Growth

This task provided a foundational understanding of:

  • Installing and managing services on Ubuntu.

  • Basic NGINX configuration and troubleshooting.

  • The importance of security settings and permissions.

  • Deploying a basic web server, a critical skill for a DevOps role.

As I progress, I aim to explore advanced NGINX configurations, reverse proxy setups, and load balancing. I believe that this programme will further deepen my knowledge Cloud engineering, Infrastructure and then widen my scope in the DevOps field.

For those looking skilled and experienced talents in Cloud and DevOps Engineering, here are valuable resources:

https://hng.tech/hire/cloud-engineers

https://hng.tech/hire/azure-devops-engineers

Conclusion

Setting up NGINX was a great first step in my DevOps journey. The process helped me develop troubleshooting skills and confidence in managing web servers. I look forward to tackling more complex configurations and deployments in future tasks.