DEV Community

Shakirah A
Shakirah A

Posted on

Setting up an Ubuntu VM on Google Cloud, Installing NGINX, and Configuring Firewall Rules

Nginx-GCPI’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

Allow SSH:

sudo ufw allow 'OpenSSH'
Enter fullscreen mode Exit fullscreen mode

Verify Firewall Rules:

sudo ufw status
Enter fullscreen mode Exit fullscreen mode

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

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

5: Test the Configuration and Accessibility

Restart NGINX:

Restart NGINX to apply the changes:

sudo systemctl restart nginx
Enter fullscreen mode Exit fullscreen mode
  • Access the Web Page:

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

Troubleshooting some Challenges and to Overcame Them

  • NGINX Not Serving the Custom Page: if you tried to access the VM's IP address in the browser, and the default NGINX page is displayed instead of your custom HTML page. It might be that you hadn’t replaced the default index.html file in /var/www/html.
  • HTTP Connection Refused: Verify Firewall Rules are enabled. Also Ensure NGINX is running. Test your connections internally and externally, visit your server ip in the browser http://YOUR_SERVER_IP Alternatively, use curl to test curl http://localhost or curl http://YOUR_SERVER_IP
  • If you received a "Connection refused" error when attempting to access the site over HTTPS. This was because NGINX wasn’t configured to handle HTTPS traffic. You could use Certbot from Let’s Encrypt to set up an SSL/TLS certificate, I didnt here because the task is specifically focused using only port 80, so I didn’t pursue this solution.

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 these aspirations and may inspire you too:
DevOps Engineers
Cloud Engineers
Site Reliability Engineers
Platform Engineers
Infrastructure Engineers
Kubernetes Specialists
CI/CD Pipeline Engineers
Monitoring/Observability Engineers
Docker Specialists

Top comments (0)