DEV Community

Rukoyahsanni
Rukoyahsanni

Posted on

Installing and Configuring NGINX on an AWS Ubuntu 24.04 Server

## Introduction
Setting up a web server is an essential step for hosting websites, APIs, and applications. A keyskill required as a DevOps engineer. In this blog post, I'll walk you through the process of installing and configuring NGINX on an AWS Ubuntu 24.04 instance. I'll also share insights, challenges encountered, and how they were resolved.

## AWS as a Cloud Platform
AWS (Amazon Web Services) is one of the leading cloud service providers, offering a range of infrastructure solutions for hosting, computing, and networking. Learning to configure and deploy services on AWS is a crucial skill for anyone aspiring to be a Cloud Infrastructure Engineer.

## Launching the AWS Ubuntu Server
To begin, an AWS EC2 instance was launched with the following specifications:

  • AMI: Ubuntu 24.04 LTS
  • Instance Type: t2.micro
  • Key Pair: SSH key pair for secure access
  • Security Group Configuration:
    • Inbound Rule: Port 22 (SSH) allowed for remote access
    • Additional configurations added later for HTTP access

After launching the instance, I connected to it via SSH using:

ssh -i my-key.pem ubuntu@<public-ip>
Enter fullscreen mode Exit fullscreen mode

## Updating the Ubuntu System
Before proceeding with any software installation, it is good practice to update the system package list and upgrade existing packages:

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

This ensures that all installed packages are up to date, minimizing potential conflicts.

## Installing NGINX
NGINX is a powerful web server that also functions as a load balancer and reverse proxy. To install it, I executed the following command:

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

Once installed, NGINX automatically starts running. To verify its status, I ran:

sudo systemctl status nginx
Enter fullscreen mode Exit fullscreen mode

A successful installation and running status indicated that the service was active and operational.

## Editing the Default Web Page
NGINX serves an HTML file from /var/www/html/index.html by default. To customize the default page, I attempted to edit the file:

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

However, I encountered a Permission Denied error because the file is owned by the root user.

### Challenge: Permission Issues
Since the /var/www/html/index.html file belongs to the root user, the default ubuntu user did not have write access. Best practices suggest using appropriate user permissions instead of switching to the root user.

### Solution: Editing with Elevated Privileges
For this project, I temporarily switched to the root user to modify the file:

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

After editing and saving the changes, I exited the root user mode:

exit
Enter fullscreen mode Exit fullscreen mode

Alternatively, a safer approach would be to change the file ownership:

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

This method allows the ubuntu user to edit the file without switching to root.

## Restarting NGINX and Allowing HTTP Traffic
After modifying the HTML file, I restarted the NGINX service to apply the changes:

sudo systemctl restart nginx
Enter fullscreen mode Exit fullscreen mode

By default, NGINX listens on port 80 (HTTP). To allow public access, I updated the AWS security group settings to include an inbound rule for HTTP:

  • Protocol: TCP
  • Port Range: 80
  • Source: 0.0.0.0/0 (Allows public access)

After updating the security group, I accessed the NGINX welcome page through my public IP address in a web browser:

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

The modified page successfully loaded, confirming that NGINX was serving my customized HTML content.

## Conclusion
This experience provided valuable insights into setting up and configuring NGINX on an AWS Ubuntu 24.04 server. The key takeaways include:

  1. Updating the system before installing new packages.
  2. Understanding user permissions when modifying system files.
  3. Properly configuring the security group to allow external HTTP access.
  4. Recognizing AWS as a powerful cloud platform for hosting and infrastructure management, an essential skill for Cloud Infrastructure Engineers.

By following these steps, you can deploy your own NGINX-powered web server on AWS with minimal hassle!

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

Top comments (0)