Setting Up a Ubuntu VM Instance (Compute Engine) on Google Cloud, Installing NGINX, and Configuring Firewalls


I’ve always been interested in cloud computing and DevOps, with hands-on experience using AWS and DigitalOcean. Recently, I joined an HNG internship to fine-tune my DevOps skills. The first tasks was setting up and configuring NGINX on a fresh Ubuntu server, sso I decided to explore Google Cloud Platform (GCP) by creating a virtual machine (VM). The whole process turned out to be easier than I expected. Here’s how I did it, what challenges I faced, and how this boosted my confidence in using GCP.



Task Overview

  • Create a VM instance on GCP.
  • Access the VM instance using gcloud/ssh.
  • Install and configure NGINX to serve a custom HTML page.
  • Configure firewalls to allow HTTP/HTTPS traffic.



Approach



1. Creating a VM on GCP

First, I created a GCP account and logged into the Google Cloud Console. It was a little unfamiliar at first, but I was able to locate and navigate to Compute Engine > VM Instances and created a new VM with these settings:

  • Name: simple-nginx-configuration
  • Region/Zone: europe-west1-d
  • Machine Type: e2-micro (free tier)
  • Boot Disk: Ubuntu 20.04 LTS
  • Firewall: Enabled HTTP and HTTPS traffic

This step was pretty straightforward.



2. SSH into the VM Instance

To SSH into the VM, I used the gcloud command-line tool:
First, I installed gcloud by following the official guide.
Then, I authenticated by running:

gcloud auth login
Enter fullscreen mode

Exit fullscreen mode

Set the project ID:

gcloud config set project `PROJECT_ID`
Enter fullscreen mode

Exit fullscreen mode

Replace PROJECT_ID with your actual project ID.

SSH into the VM instance with:

gcloud compute ssh INSTANCE_NAME --zone ZONE
Enter fullscreen mode

Exit fullscreen mode

(Replace INSTANCE_NAME with your VM’s name and ZONE with the zone where your instance is located.)
the command will look like this

gcloud compute ssh --zone "europe-west1-d" "simple-nginx-configuration" --project "hng-internship"
Enter fullscreen mode

Exit fullscreen mode

Alternatively, if you’re using the Google Cloud Console, you can simply click SSH > View gcloud sommand on your VM instance’s page. This will display the gcloud command to use, which you can copy and paste directly.

This worked seamlessly, and I was able to access the terminal of my VM instance.



3. Installing NGINX on the Vm instance

Step 1: Install NGINX
Update the Package List:

sudo apt update
Enter fullscreen mode

Exit fullscreen mode

Install NGINX:

sudo apt install nginx
Enter fullscreen mode

Exit fullscreen mode

Verify Installation:

Check the NGINX version to confirm installation:

nginx -v
Enter fullscreen mode

Exit fullscreen mode

Step 2: Start and Enable NGINX
Start NGINX:

sudo systemctl start nginx
Enter fullscreen mode

Exit fullscreen mode

Enable NGINX to Start on Boot:

sudo systemctl enable nginx
Enter fullscreen mode

Exit fullscreen mode

Check NGINX Status:
Ensure NGINX is running:

sudo systemctl status nginx
Enter fullscreen mode

Exit fullscreen mode

You should see active (running) in the output.

Step 3: Configure Firewall (if enabled)
If you have a firewall enabled (e.g., ufw), allow HTTP and HTTPS traffic:

Enable the Firewall:

sudo ufw enable
Enter fullscreen mode

Exit fullscreen mode

Allow HTTP:

sudo ufw allow 'Nginx HTTP'
Enter fullscreen mode

Exit fullscreen mode

Allow HTTPS (optional):

sudo ufw allow 'Nginx HTTPS'
Enter fullscreen mode

Exit fullscreen mode

Verify Firewall Rules:

Allow SSH:

sudo ufw allow 'OpenSSH'

sudo ufw status
Enter fullscreen mode

Exit fullscreen mode

Step 4: Create a Custom HTML Page
Navigate to the Web Root Directory:

cd /var/www/html
Enter fullscreen mode

Exit fullscreen mode

Create or Edit the index.html File:

Use a text editor (e.g., vim) to create or edit the file:

sudo vim index.html
Enter fullscreen mode

Exit fullscreen mode

Add the Custom HTML Content:

Replace the content with the following:

<!DOCTYPE html>
<html>
<head>
    <title>Welcome</title>
</head>
<body>
    <h1>Welcome</h1>
    <p>Welcome to DevOps Stage 0 - [Your Name]/[SlackName]</p>
</body>
</html>
Enter fullscreen mode

Exit fullscreen mode

Save and Exit:

:wq
Enter fullscreen mode

Exit fullscreen mode

Save the file and exit the editor

I ensured the file had the correct permissions:

sudo chown -R www-data:www-data /var/www/html
sudo chmod -R 755 /var/www/html
Enter fullscreen mode

Exit fullscreen mode

Step 5: Test the Configuration
Restart NGINX:

Restart NGINX to apply the changes:

sudo systemctl restart nginx
Enter fullscreen mode

Exit fullscreen mode

Open a web browser and navigate to your server’s IP address:

visit
http://YOUR_SERVER_IP

Alternatively, use curl to test:

curl http://localhost
Enter fullscreen mode

Exit fullscreen mode

or

curl http://YOUR_SERVER_IP
Enter fullscreen mode

Exit fullscreen mode



Challenges Faced and How I Overcame Them

  1. NGINX Not Serving the Custom Page
    Initially, when I tried to access the VM’s IP address in my browser, I saw the default NGINX page instead of my custom HTML page. I realized I hadn’t replaced the default index.html file in /var/www/html. After creating my custom file, the issue was resolved.

  2. HTTPS Connection Refused
    When I tried to access the site over HTTPS (https://34.22.161.37/), I got a “Connection refused” error. This was because NGINX wasn’t configured to serve content over HTTPS. To fix this, I would have used Certbot from Let’s Encrypt to obtain an SSL/TLS certificate but i didt because the task stated to use only port 80 specifically.



How This Task Contributes to My Learning and Professional Goals

This task was a fantastic hands-on experience that deepened my understanding of Additional Cloud Infrastructure: Creating and managing VM instances on GCP.



References and Further Reading

As I continue my journey, I’m inspired by the opportunities available at HNG Tech. Here are some roles that align with my aspirations:
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



Source link