DevOps – How To Create A Virtual Machine In Google Cloud Platform (GCP) And Run NGINX Web Server Into It

Author: Ayush Maggo


When it comes to hosting web content, Google Cloud Platform (GCP) provides a robust infrastructure, while NGINX stands out as a high-performance web server. In this tutorial, I will walk you through the process of creating a virtual machine (VM) in GCP and setting up NGINX to unleash the full potential of your web hosting capabilities.

What is a Virtual Machine?

A virtual machine in the cloud refers to a virtualized computing instance provided by cloud service providers. It allows users to run applications and operating systems remotely, accessing them through the internet.

Virtual machines have their own CPU, memory, network interface, and storage. They offer scalability, flexibility, and cost efficiency by enabling resource allocation on-demand.

In other terms, a virtual machine is a computing environment that utilizes software-defined hardware instead of specialized hardware.

What is an NGINX Server?

NGINX is a high-performance web server that was developed to handle the escalating needs of the modern web. Its main priorities are high performance, lots of parallelism, and sparse resource utilization. While NGINX is widely known as a web server, at its foundation, it is a reverse proxy server.

It also functions as a reverse proxy server, load balancer, and HTTP cache.

  • A GCP account with a configured project is required.
Create a Virtual Machine instance in Google Cloud Platform (GCP)
  • In the navigation menu, go to the Compute Engine section and select VM instances.
  • Click on “Create Instance” to initiate the creation of a new VM instance.
  • Provide a specific name for the Virtual Machine instance.
  • Select the region and Zone based on your requirements.
  • Choose a machine type based on your requirements. In most cases, the default option should suffice.
  • Under “Boot Disk,” click on “Change” to select an operating system image, such as a Linux Distribution or Windows Server. You can also customize the disk size and type according to your requirements.
  • Set the desired boot disk size according to your requirements.
  • Configure the firewall settings by selecting “Allow HTTP traffic” (port 80) or “Allow HTTPS traffic” (port 443) if you plan to run a web server like NGINX.
  • Leave the “Advanced options” as default.
  • Now, finally, click on “Create” to create the VM instance.
  • The process may take a few minutes. Once the Virtual Machine instance is successfully created, it will be listed in the Compute Engine instance list.
Connect to the VM instance
  • After the VM has been created, you can access it using SSH.
  • Open a terminal on your local machine and authenticate with your GCP account.
  • Run the below command to authenticate with your GCP account:
gcloud auth login
  • Set the project ID using the following command:
gcloud config set project <Project_ID>
  • Click on the SSH drop-down button of the VM instance and navigate to the “View gcloud command”.
  • By running the command “View gcloud command” in the terminal, you will connect to the VM instance.
Install an NGINX web server in Virtual Machine
  • In the SSH terminal, update the system packages by using this command:
sudo apt update
  • Now install the NGINX using the following command :
sudo apt install nginx
  • Run the following command to launch the NGINX service:
sudo service nginx start
  • Now run the following command to confirm the nginx is running:
ps auwx | grep nginx
  • Enter the Public IP address of your VM instance into the web browser.
  • Now as you can see below, NGINX web server is running.
  • If NGINX is installed correctly, you should see the default NGINX welcome page mentioned above.

This is how you can create a Virtual Machine in Google Cloud Platform (GCP) using Compute Engine and run multiple web servers on it.

Important links:
Happy learning!

We use cookies on this site to enhance your user experience. For a complete overview of how we use cookies, please see our privacy policy.