DEV Community

etiifiok udofiah
etiifiok udofiah

Posted on

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

To progress from level 0 to level 1 in the HNG Internship for devops, we were require to set up and configure an NGINX web server on a fresh Ubuntu instance. This task is to test my ability to configure basic web server and ensuring it was publicly accessible.

STEP TO TAKE
Log into AWS Management Console
Go to AWS Management Console.
Log in using your credentials.
Launch an EC2 Instance in the Default VPC
Navigate to EC2 Dashboard:

In the AWS Management Console, go to Services and select EC2 under Compute.
Select “Launch Instance”:

Click on Launch Instance to start the EC2 instance creation process.
Choose an Amazon Machine Image (AMI):

Select an AMI (Amazon Machine Image). For example, you can choose:
Ubuntu Server 24.04 (HVM), SSD Volume Type for a Linux-based instance.
Click Select for your desired AMI.
Choose an Instance Type:

Select an instance type that fits your needs. For testing purposes, you can start with the t2.micro instance type (eligible for the Free Tier).
Click Next: Configure Instance Details.
Configure Instance Details
Network: Select default for the VPC (this is the default VPC created for you when your AWS account was set up).
Subnet: Select an available subnet (this can be any default subnet in the VPC).
Auto-assign Public IP: Ensure this is set to Enable to get a public IP address for your instance.
Leave other settings as is, unless you need specific configurations.
Click Next:

Configure Security Group.
Create a new security group:
Security Group Name: Example MyEC2SecurityGroup.
Type: Select SSH for Linux instances or RDP for Windows instances.
Source: Select Anywhere (0.0.0.0/0) for public access (or restrict to specific IPs for security).
For SSH, port 22 should be open for Linux instances.
For RDP, port 3389 should be open for Windows instances.
Click Review and Launch.

Review and Launch
Review all the details.
Click Launch.
When prompted to select a key pair, either create a new key pair or choose an existing one:
If creating a new key pair, download the .pem file and keep it safe. This file is required to SSH into your EC2 instance.
If using an existing key pair, select it and acknowledge the key pair checkbox.
Click Launch Instances.
Access Your EC2 Instance
Wait for the instance to launch: It will take a few minutes to launch the instance.

View Instance:

Once you have successfully SSH into the instance using the instructions provided by AWS open git bash on your local pc

install nginx
sudo apt update && sudo apt upgrade -y
sudo apt install nginx -y

check for nginx status if it is active
sudo systemctl status nginx
if it is not active type
sudo systemctl start nginx
then confirm if it is still active

create a html folder by typing sudo mkdir -p /var/www/html
grant permission by typing sudo chown -R $root:$root /var/www/html
add your html content
sudo nano /var/www/html/index.html

   <!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

verify the index.html file exit by typing
ls -l /var/www/html/index.html
or
cat /var/www/html/index.html

restart nginx to apply changes
sudo systemctl restart nginx

test your setup with
http://<your-ec2-public-ip>/

Challenges faced and solution
My server wasn't serving my index.html file, but i could see the file. It was displaying this content shown in the diagram below

Image description

I had to check if my server was serving my index.html file by typing curl http://localhost
then i check the configuration file
sudo nano /etc/nginx/nginx.conf
I looked out for the path to the root configuration and i made the necessary changes to /var/www/html
Saved and exited.
Proceeded to test the nginx configuration with
sudo nginx -t
and i noticed it was corrected and displayed the /var/www/html path
I checked for file permission with ls -l /var/www/html/index.html
Adjusted the file permission using sudo chown nginx /var/www/html/index.html
Then restart nginx sudo systemctl restart nginx
refreshed my web page from my browser and it worked.

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 ihad 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

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay