Setting Up Nginx on Ubuntu AWS EC2

A Step-by-Step Guide

As an aspiring DevOps Engineer, I am committed to learning the fundamental skills required to automate and manage cloud-based infrastructure. This project involves setting up Nginx on an AWS EC2 instance, a crucial step in understanding web server deployment, cloud networking, and system administration. By working through this process, I am gaining hands-on experience with AWS, Linux, SSH, and Nginx, all of which are essential tools in a DevOps workflow. This is a task provided by HNG Tech internship for stage 0.

Setting up Nginx on an AWS EC2 instance running Ubuntu is a crucial step for deploying web applications. This guide provides a step-by-step approach to installing and configuring Nginx, ensuring it serves a custom HTML page as the default page.

Prerequisites

  • An AWS account

  • A running Ubuntu EC2 instance

  • SSH access to the instance

  • Basic knowledge of Linux commands

Step 1: Launch an EC2 Instance

  1. Log in to AWS Management Console.

  2. Navigate to EC2 and launch a new instance.

  3. Choose an Ubuntu AMI (Amazon Machine Image).

    I used the “Ubuntu Server 22.04 LTS”

  4. Select an instance type (e.g., t2.micro for free-tier users).

  5. Configure instance details and storage as needed.

    I kept this in their default setting

  6. Configure security group:

    • Allow HTTP (80) and SSH (22) access.

      💡Use caution on the inbound rules used as publicly exposing your instance puts it in a security risk. Use security best practices

    • The security group acts as a virtual firewall to control inbound and outbound traffic to your instance. For this task, you need to allow:

      • SSH traffic (port 22): To connect to your instance via SSH.

      • HTTP traffic (port 80): To access your NGINX web server via a browser.

    • Save and launch the instance.

  7. Download the private key (.pem -For Linux & Mac users / ppk file for Windows users) and set the correct permissions:

    ~ chmod 400 gives read access to the owner of a file, while removing all permissions for the group and other users

chmod 400 your-key.pem
  1. Connect to your instance via SSH:
ssh -i your-key.pem ubuntu@your-ec2-public-ip

Step 2: Install Nginx

Once connected to your EC2 instance, run the following commands to install Nginx:

sudo apt update
sudo apt install nginx -y

Enable and start the Nginx service:

sudo systemctl enable nginx
sudo systemctl start nginx

Verify the status of Nginx:

sudo systemctl status nginx

Nginx Status Screenshot:

Step 3: Configure Nginx to Serve a Custom HTML Page

Note: Use “q” the letter to exit the running process and get back to command line, incase you get stuck

  1. Navigate to the default web directory:

     cd /var/www/html
    

  2. Create or edit the index.html file:

    I used Vim Editor thus, use “i“ the letter to enter insert mode where you can edit and

     sudo vi index.html
    
  3. Add the following HTML content, replacing [Your Name] and [SlackName] with your actual details:

     <html>
         <head>
             <title>HNG DevOps Stage 0</title>
         </head>
         <body>
             <h1>Welcome to DevOps Stage 0 - Your Name / SlackName</h1>
         </body>
     </html>
    

Custom HTML File Screenshot:

  1. Save the file (Esc (to exit Insert mode) + :wq (to save and exit VI editor)).

  2. Restart Nginx to apply the changes:

     sudo systemctl restart nginx
    

Step 4: Verify the Web Page

  1. Open a browser and visit:

     http://your-ec2-public-ip
    
  2. You should see the custom welcome message displayed on the page.

Final Web Page Screenshot:

Challenges faced and How I overcame them

  • While the process of setting up an NGINX web server on AWS was straightforward, I did encounter a couple of challenges that taught me valuable lessons:

    1. Security Group Misconfiguration: Initially, I forgot to allow HTTP traffic (port 80) in the security group. As a result, when I tried accessing the server’s public IP in my browser, the page wouldn’t load. I revisited the security group settings in the AWS console and added the necessary rule, which resolved the issue. This experience reinforced the importance of carefully configuring firewalls to ensure proper access to resources.

    2. Troubleshooting SSH Connection Issues with Public Key: During the setup, I encountered an issue connecting to the EC2 instance using the provided public key. The error was due to incorrect permissions on the .pem file. This was resolved by ensuring the key had the correct permissions

How This Task Contributes to My Learning and Professional Goals

Completing this task has strengthened my understanding of cloud infrastructure, particularly in setting up and configuring web servers on AWS. As an aspiring DevOps Engineer, mastering these skills is crucial for deploying and managing scalable web applications. This experience provides hands-on exposure to Linux system administration, web server configuration, security best practices, and cloud computing fundamentals.

References

Conclusion

🎉You have successfully set up Nginx on an AWS EC2 instance, configured it to serve a custom HTML page, and verified that it works. This setup is the foundation for deploying more complex web applications on AWS.