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
Set the project ID:
gcloud config set project `PROJECT_ID`
Replace PROJECT_ID
with your actual project ID.
SSH into the VM instance with:
gcloud compute ssh INSTANCE_NAME --zone ZONE
(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"
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
Install NGINX:
sudo apt install nginx
Verify Installation:
Check the NGINX version to confirm installation:
nginx -v
Step 2: Start and Enable NGINX
Start NGINX:
sudo systemctl start nginx
Enable NGINX to Start on Boot:
sudo systemctl enable nginx
Check NGINX Status:
Ensure NGINX is running:
sudo systemctl status nginx
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
Allow HTTP:
sudo ufw allow 'Nginx HTTP'
Allow HTTPS (optional):
sudo ufw allow 'Nginx HTTPS'
Verify Firewall Rules:
Allow SSH:
sudo ufw allow 'OpenSSH'
sudo ufw status
Step 4: Create a Custom HTML Page
Navigate to the Web Root Directory:
cd /var/www/html
Create or Edit the index.html File:
Use a text editor (e.g., vim) to create or edit the file:
sudo vim index.html
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>
Save and Exit:
:wq
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
Step 5: Test the Configuration
Restart NGINX:
Restart NGINX to apply the changes:
sudo systemctl restart nginx
Open a web browser and navigate to your server’s IP address:
Alternatively, use curl to test:
curl http://localhost
or
curl http://YOUR_SERVER_IP
Challenges Faced and How I Overcame Them
-
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. -
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