DEV Community

Cover image for Setting Up NGINX to Serve a Simple HTML Page
Anne Usang
Anne Usang

Posted on

Setting Up NGINX to Serve a Simple HTML Page

In this blog post, I will share my experience configuring NGINX on an EC2 instance running Ubuntu to serve a custom HTML page. The goal was to display the message:

"Welcome to DevOps Stage 0 - [Your Name]/[SlackName]"

I will walk through the approach I took, challenges I faced, and how this task contributes to my professional growth.

GIF

To begin, I launched an EC2 instance on AWS with Ubuntu as the operating system. After ensuring that my instance was up and running, I followed these steps:

1) Connect to the EC2 Instance: I connected to my EC2 instance using the browser-based terminal, EC2 instance connect.

Ec2-Instance-Connect

2) Update the System: Once connected, I updated the package list to ensure I had the latest versions. By the way, this is a good practice:

sudo apt update -y
Enter fullscreen mode Exit fullscreen mode

3) Install NGINX: I installed NGINX on my server using the package manager:

sudo apt install nginx -y
Enter fullscreen mode Exit fullscreen mode

4) Creating the simple HTML page: I navigated to the web root directory (/var/www/html/) and created an index.html file:

sudo nano /var/www/html/index.html
Enter fullscreen mode Exit fullscreen mode

Then, I added the following content into the index.html file:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>DevOps Stage 0</title>
</head>
<body>
    <h1>Welcome to DevOps Stage 0 - [Replace with Your Name]/[Replace with your SlackName]</h1>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

5) Next, I edited the NGINX default configuration file:

sudo nano /etc/nginx/sites-available/default
Enter fullscreen mode Exit fullscreen mode

I modified the server block as follows:

server {
    listen 80;
    server_name _;
    root /var/www/html;
    index index.html;

    location / {
        try_files $uri $uri/ =404;
    }
}
Enter fullscreen mode Exit fullscreen mode

6) After saving the configuration, I restarted NGINX to apply the changes:

sudo systemctl restart nginx
Enter fullscreen mode Exit fullscreen mode

Voila! Here's how my site looked

Deployed site

Challenges Faced & Solutions

While the task was straightforward, I encountered a challenge:

Permission Issues: Initially, I faced permission issues when trying to create the HTML file in the /var/www/html directory. To resolve this, I used sudo to create and edit the file, ensuring I had the necessary permissions.

How This Task Contributes to My Learning & Professional Goals

This task provided me with hands-on experience in configuring a web server, which is a crucial skill for any Cloud Engineer. Understanding how to serve static content using NGINX is foundational knowledge that will aid me in more complex deployments in the future.

Moreover, this experience reinforced my problem-solving skills and my ability to troubleshoot issues in real-time, which are essential traits in the fast-paced world of DevOps.

Conclusion

Configuring NGINX to serve a simple HTML page was a rewarding experience that enhanced my technical skills and prepared me for future challenges in my career. For those interested in pursuing a career in this field, I recommend exploring opportunities as a DevOps Engineer or Cloud Engineer, as these roles are in high demand and offer exciting career paths. For more information, check out the following HNG resources: DevOps Engineers and Cloud Engineers.

Top comments (0)