DEV Community

Oluwatobi Solarin
Oluwatobi Solarin

Posted on

Deploying an NGINX Web Server on AWS EC2: HNG DevOps Stage 0

Introduction
As part of the DevOps task to ascend from Level 0 to Level 1 in the HNG Internship for DevOps (because apparently, this internship is structured like a video game), I had to tackle my first boss fight: setting up and configuring an NGINX web server on a fresh Ubuntu instance.

The goal? To prove that I could configure a basic web server and make it publicly accessible—because if a server runs in the cloud and no one can access it, does it even exist? 🤔

Challenge accepted! Now, let’s see if my server stands tall or crashes harder than my hopes after a failed deployment. 😆

Steps to Take

Log into AWS Management Console

Open your browser and head to the AWS Management Console.

Log in with your credentials (don’t worry, no one’s watching… probably).

Launch an EC2 Instance in the Default VPC

Navigate to the EC2 Dashboard:

In the AWS Console, click on Services and find EC2 under Compute. It’s like finding a needle in a haystack, but with a search bar!

Click on “Launch Instance”:

This is where the magic begins. It’s like ordering a pizza, but instead of toppings, you’re choosing servers.

Choose an Amazon Machine Image (AMI)

Pick an AMI (Amazon Machine Image). Think of it as choosing the operating system for your virtual pizza—uh, server.

For example, you can go with Ubuntu Server 24.04 (HVM), SSD Volume Type. It’s like choosing a fancy, pre-baked crust.

Click Select and pat yourself on the back for making a decision.

Choose an Instance Type

Now, pick an instance type. For beginners, the t2.micro is your best friend—it’s free (well, mostly) and perfect for testing.

Click Next: Configure Instance Details and pretend you’re a cloud architect for a moment.

Configure Security Group

Create a New Security Group

Give it a fancy name like Example MyHNGSecurityGroup. (Feel free to get creative—maybe FortKnoxButForCloud or NoHackersAllowed?)

Set the Rules (Because Rules Are Cool)

Type:

For Linux, choose SSH. It’s like giving your server a secret handshake.

For Windows, choose RDP. It’s like handing over the keys to your virtual castle.

Source:

If you’re feeling adventurous, set it to Anywhere (0.0.0.0/0). This is the cloud equivalent of shouting, “Come one, come all!”

If you’re paranoid (or just smart), restrict it to specific IPs. Think of it as a VIP list for your server party.

Open the Right Ports

For Linux, open Port 22 for SSH. It’s like leaving the backdoor open for your tech-savvy friends.

For Windows, open Port 3389 for RDP. It’s like rolling out the red carpet for your remote desktop.

Click Review then,

Click Launch

This is the moment of truth. Click that button like you’re launching a spaceship. (Okay, it’s just a server, but let’s pretend it’s epic.)

Choose or Create a Key Pair

Option 1: Create a New Key Pair

Name it something cool, like SecretKey.pem.

Download the .pem file and guard it like it’s the One Ring. Lose it, and you’ll be locked out of your own server. (Dramatic, but true.)

Option 2: Use an Existing Key Pair

Select your trusty old key pair and check the box like, “Yeah, I know what I’m doing.”

Click Launch Instances

And just like that, your instance is off to the races. Sit back, relax, and maybe grab a snack while AWS works its magic.

Access Your EC2 Instance

Wait for the Instance to Launch

It’ll take a few minutes. Use this time to practice your victory dance or stare at the screen impatiently.

View Your Instance

Once it’s up and running, it’s showtime!

Open Git Bash on your local PC (or your terminal of choice) and follow AWS’s instructions to SSH into your instance.

Setting Up NGINX

Once my EC2 instance was running, I followed these steps to install and configure Setting Up NGINX

1. Update and install NGINX

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

2. Verify NGINX is Running

sudo systemctl status nginx
Enter fullscreen mode Exit fullscreen mode

if it is not active type

sudo systemctl start nginx
Enter fullscreen mode Exit fullscreen mode

3. Create a html folder

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

4. Then, I added the following content:

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

save and exit using ctrl + X
click y - yes
then click enter to exit

5. Restart NGINX to Apply Changes

sudo systemctl restart NGINX
Enter fullscreen mode Exit fullscreen mode

*6. Test the Setup I accessed my public IP address
*

http://<your-ec2-public-ip>/
Enter fullscreen mode Exit fullscreen mode

Challenges Faced:

The Case of the Missing Index.html

My server was playing hide-and-seek with my index.html file. I could see the file, but the server acted like it didn’t exist.
it displayed some weird content (see the diagram below)

Image description

I Interrogated the Server with

I typed curl http://localhost to see what the server was serving. Spoiler: It wasn’t my index.html.

I Investigated the Configuration File

I opened the Nginx config file with sudo nano /etc/nginx/nginx.conf and went full Sherlock Holmes.

I found the root path and realized it was pointing to the wrong neighborhood. So, I redirected it to /var/www/html—the right address for my index.html.

I Saved and Exited Like a Pro

After making the changes, I saved the file and exited Nano like a boss. (Nano tip: Ctrl + X, then Y, then Enter. You’re welcome.)

I Tested the Configuration

I ran sudo nginx -t to test the configuration. It finally displayed the correct path: /var/www/html. Victory was within reach!

I Checked File Permissions

I used ls -l /var/www/html/index.html to check the file permissions. Turns out, the file was owned by someone else. Rude.

I fixed it with sudo chown nginx /var/www/html/index.html. Now, NGINX had the keys to the kingdom.

I Restarted NGINX

I restarted Nginx with sudo systemctl restart nginx. It’s like rebooting your computer, but fancier.

I Refreshed the Browser

I refreshed my browser, and voilà! My index.html file was finally served. Cue the confetti and celebratory dance.

Key Takeaways

This task taught me several important lessons:

✅ How to launch an EC2 instance and configure it for web hosting.

✅ Setting up a basic web server using NGINX.

✅ Managing AWS security settings to allow public access.

✅ Troubleshooting connectivity issues related to NGINX configurations

Deploying NGINX on AWS EC2 was a valuable hands-on experience it helped me in troubleshoot issues I had with my server displaying my web content as this will be a common issues I will experience in becoming a DevOps Engineer

References

Devops Engineers
Cloud Engineers
Site Reliability Engineers
Platform Engineers
Infrastructure Engineers
Kubernetes Specialists
AWS Solutions Architects
Azure DevOps Engineers
Google Cloud Engineers
CI/CD Pipeline Engineers
Monitoring/Observability Engineers
Automation Engineers
Docker Specialists
Linux Developers
PostgreSQL Developers

Top comments (1)

Collapse
 
ameh_mathias profile image
Ameh Mathias Ejeh

Hahaa. So hilarious and fun to read. Weldon.