Installing Nginx on a Google Cloud Platform (GCP) Virtual Machine

Installing Nginx on a Google Cloud Platform (GCP) Virtual Machine

When diving into DevOps, understanding cloud infrastructure and server management is essential. This guide will walk you through the basics of virtualization, virtual machines, and cloud computing before demonstrating how to create a virtual machine (VM) on Google Cloud Platform (GCP) and install Nginx.

What is Virtualization?

Virtualization is the process of creating virtual replicas of physical computing resources like storage, operating systems, and networks. It allows hardware resources to be partitioned into multiple virtual computers, known as virtual machines (VMs). Learn more about virtualization here.

What is a Virtual Machine (VM)?

A virtual machine is a software-defined computer that exists within a physical server. It has its own CPU, memory, storage, and network capabilities. VMs can run on your local machine or on remote servers. Discover more about VMs and their benefits.

What is Cloud Computing?

Cloud Computing utilizes remote servers hosted on the internet to store, manage, and process data, instead of relying on local machines or servers. Google Cloud Platform (GCP) is one such cloud service. Learn more about cloud computing here.

Creating a Virtual Machine on GCP

  1. Sign up for GCP: Create an account on the Google Cloud Platform.

  2. Create a VM:

    • Select Create VM as show in the image

    • Enable Compute Engine API

    • Click the Create Instance button in the navigation bar.

  3. Configure the VM:

    • Name: Change from instance-20250203-074548 to test.

    • Region: Leave as default.

    • Zone: Select us-central1-a.

  4. Choose OS and Storage:

    • In the sidebar, select OS and Storage.

    • Select Change button

    • Change the operating system from Debian to Ubuntu.

    • Click Select to save changes.

  5. Set Up Networking:

    • In the Networking section, check the boxes for:

    • Change the operating system from Debian to Ubuntu.

    • Click Select to save changes.

  6. Create the VM: Click the Create button to start the VM instance.

  7. Connect via SSH:

    • Open the SSH terminal in your browser.

    • Authorize the connection to your VM.


Installing Nginx on the VM

  1. Update Packages:

     sudo apt update
    
  2. Install Nginx:

     sudo apt install nginx -y
    
  3. Configure Firewall:

  4.  sudo ufw app list
     sudo ufw allow 'Nginx HTTP'
     sudo ufw allow 'Nginx HTTPS'
     sudo ufw allow 'OpenSSH'
     sudo ufw enable
     sudo ufw status
    
  5. check Nginx Status:

     sudo systemctl status nginx
    
  6. Verify Installation:

  7.  curl http://localhost
     curl http://your_external_ip_address
    

    Learn more about installing Nginx on Ubuntu

Customizing the Nginx Welcome Page

  1. Edit the Default Web Page:

     sudo vi /var/www/html/index.html
    
  2. Insert the Following HTML:

     <!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 the editor (:wq).
  3. Adjust Permissions:

     sudo chown -R www-data:www-data /var/www/html
     sudo chmod -R 755 /var/www/html
    
  4. Restart Nginx:

     sudo systemctl restart nginx
    
  5. Test the changes:

     curl http://localhost
     curl http://your_external_ip_address
    

Common Challenges

Issue: Unable to access the external IP via HTTP/HTTPS.
Cause: Forgot to check the HTTP and HTTPS boxes in the GCP Networking interface, even though the firewall rules were enabled on the server.
Solution: Ensure these options are selected during VM setup.


Need a DevOps Engineer?

Hire a DevOps Engineer here.

This guide provides a foundational understanding of cloud infrastructure and server management. If you encounter any issues, feel free to leave a comment!