DEV Community

Cover image for How to Setup A Simple Nginx Web Server on an Ubuntu GCP Virtual Machine.
Chinenye Genevieve Onyema
Chinenye Genevieve Onyema

Posted on • Edited on

How to Setup A Simple Nginx Web Server on an Ubuntu GCP Virtual Machine.

Nginx Web Server on Ubuntu GCP Virtual Machine

Introduction

I have always wanted to share my learning experiences and knowledge from my tasks and projects. In this article, as part of the HNG Internship program, I will provide a step-by-step guide on configuring a simple Nginx web server on a Google Compute Engine Ubuntu virtual machine.

Requirements

  • A Google Cloud account, if you don't have an account, Sign up. GCP is giving free $300 credits to new accounts.
  • Basic experience with Linux CLI.
  • Sign in to your Google Cloud console.

Steps on how to set up an Ubuntu Virtual Machine.

Step 1

  • Click on the three bars by the left side of the console. Google Cloud Console

Step 2

  • Click on Compute Engine and then clickon VM Instances Virtual Machines

Step 3

  • Click on "Create" Instance. Create Instance

Step 4: Machine Configuration settings

  • Give the instance any name of your choice.
  • Choose the region and zone nearest to you for your instance.
  • Choose the default General Purpose E2 machine for the task.

Machine Configuration

Machine types

Step 5: Operating System and Storage

  • Continue the instance setup by clicking on OS and Storage.

OS and Storage

  • Click "Change" in the Operating System configuration.

Operating system and Storage

Ubuntu Image

  • Change the OS image to Ubuntu and keep the remaining settings as default.
  • Click "Select".

Step 6: Networking

  • Choose Networking
    Networking

  • For the firewalls, tick to allow HTTP and HTTPS traffic.

  • Click on "create" instance

Open firewalls and create instance

Running Virtual Instance

Congratulations! You have successfully launched a Google Cloud Virtual Instance.

To SSH Into The Nginx Virtual Instance.

  • On the instance dashboard, Tick the instance and click on "SSH" A new window pops up.
  • Click "Authorize" to connect to the instance. And wait for SSH to connect to the instance successfully.

Good Job! You are now connected to your Instance Command Line interface.

SSH Connection

How to Configure Nginx on Ubuntu Virtual Instance

Step 1: Run the command below to Update and Upgrade the Instance.
sudo apt update & sudo apt upgrade -y

Step 2: Install Nginx
sudo apt install nginx -y

Step 3: Start and Enable Nginx

sudo systemctl start nginx
sudo systemctl enable nginx
Enter fullscreen mode Exit fullscreen mode

Step 4: Check if Nginx is actively running successfully
sudo systemctl status nginx

Step 5: Back up the Default Web Page

cd /var/www/html
sudo cp index.nginx-debian.html index.nginx-debian.html.bk
Enter fullscreen mode Exit fullscreen mode

Step 6: Create a new file named index.html
touch index.html

Step 7: Move the content of the default page to the new file
sudo mv index.nginx-debian.html index.html

Step 8: Open the index.html file
sudo vi index.html

Step 9: Modify the content of index.html file to the code below

  • To edit the file, press the letter "i"
<!DOCTYPE html>
<html>
<head>
<title>HNG Stage 0 Nginx Web Server</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h4>Welcome to DevOps Stage 0 - [Your Name]/[SlackName] </h4>

</body>
</html>
Enter fullscreen mode Exit fullscreen mode
  • After editing, press the escape key, colon, letters w and q. That is, ":wq"

Step 10: Restart Nginx
sudo systemctl restart nginx

Step 10: Copy the External Ip address and paste in your browser
34.89.121.56

Image description

Nginx web server

Possible Challenges

One potential challenge you may face when accessing your webpage is related to networking. Ensure that the firewall is configured to allow inbound HTTP traffic, as the NGINX server listens on port 80 by default.

How This Task Contributed to My Learning

As someone with extensive experience using AWS Cloud, I wanted to enhance my multi-cloud skills by leveraging Google Cloud as my development environment for this task. Previously, I primarily accessed virtual instances by clicking the SSH option on the Google Cloud Console. However, during this task, I explored alternative methods of connecting to a GCP instance using PuTTY, Git Bash, and other SSH tools.

This was particularly exciting for me because, on AWS, I have always accessed EC2 instances by downloading and using the private key. Through this experience, I learned how to generate private and public key pairs using PuTTY, add the public key to the GCP instance's SSH configuration, and securely authenticate my connection.

In a forthcoming article, I will share a step-by-step guide on connecting to a GCP instance using third-party SSH tools and also generating keys through the command line.

Hire your skilled DevOps Engineers and Cloud Engineers here.

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more