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'
Allow SSH:
sudo ufw allow 'OpenSSH'
Verify Firewall Rules:
sudo ufw status
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
Ensured the file had the correct permissions:
sudo chown -R www-data:www-data /var/www/html
sudo chmod -R 755 /var/www/html
5: Test the Configuration and Accessibility
Restart NGINX:
Restart NGINX to apply the changes:
sudo systemctl restart nginx
- 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
or
curl http://YOUR_SERVER_IP
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 testcurl http://localhost
orcurl 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)