DEV Community

S3CloudHub
S3CloudHub

Posted on

How to Set Up a GCP VM and Install Apache2: Step-by-Step Guide for Beginners

Image description

If you're just starting with Google Cloud Platform (GCP) and want to deploy a simple web server, this guide will walk you through creating a Virtual Machine (VM) and installing Apache2, one of the most popular web servers. Let’s get started!

For a visual walkthrough of the concepts covered in this article, check out my YouTube Video:-
image alt text here

Step 1: Create a Google Cloud Account

Before you can access GCP, you need an account. If you don’t have one, follow these steps:

  1. Visit Google Cloud and click Get started for free.
  2. Sign in with your Google account or create a new one.
  3. Set up your billing information (GCP offers a free tier for beginners with $300 in credits for new users).

Step 2: Set Up a New Project

Once logged into GCP, navigate to the Google Cloud Console.

  1. Click the project dropdown on the top-left and select New Project.
  2. Name your project (e.g., "Apache2-WebServer"), and click Create.

Step 3: Enable the Compute Engine API

  1. On the Cloud Console Dashboard, type Compute Engine in the search bar.
  2. Click Compute Engine and enable it if it’s not already enabled.
  3. You’ll be redirected to the Compute Engine dashboard.

Step 4: Create a Virtual Machine (VM)

In the Compute Engine section, click on VM instances.

  1. Click Create Instance to start configuring your new VM.
  2. Set the following configurations:
    • Name: Enter a name for your VM (e.g., apache2-vm).
    • Region and Zone: Choose a region close to your location (e.g., us-central1).
    • Machine Type: Choose an instance size. For this tutorial, you can go with e2-micro, which is part of the free tier.
    • Boot Disk: Click Change and select Ubuntu 20.04 LTS (or another OS if preferred), then click Select.
  3. Leave the rest of the settings as default and click Create.

Step 5: Connect to Your VM

Once your VM instance is ready, you’ll see it listed in the VM Instances section.

  1. Click on the SSH button next to your VM instance to open a terminal session in your browser.
  2. You’re now connected to your VM via SSH.

Step 6: Update and Upgrade the VM

Before installing any software, it’s best practice to ensure your VM is updated. Run the following commands:

sudo apt update
sudo apt upgrade -y
Enter fullscreen mode Exit fullscreen mode

Step 7: Install Apache2

Apache2 is available in the default Ubuntu package repository, so you can install it with a simple command:

sudo apt install apache2 -y
Enter fullscreen mode Exit fullscreen mode

Step 8: Start Apache2 Service

Once Apache2 is installed, start the service:

sudo systemctl start apache2
Enter fullscreen mode Exit fullscreen mode

Step 9: Enable Apache2 to Start on Boot

You want Apache2 to start automatically whenever the VM reboots:

sudo systemctl enable apache2
Enter fullscreen mode Exit fullscreen mode

Step 10: Adjust the Firewall Settings

By default, Google Cloud blocks external traffic to VMs, so you need to open port 80 (HTTP) for your Apache2 web server:

  1. Go back to your VM instances page.
  2. Click on your VM name to open the instance details.
  3. Scroll down to the Network interfaces section and click View details.
  4. In the Firewall section, check the boxes for Allow HTTP traffic and Allow HTTPS traffic.

Step 11: Test Apache2 Installation

Now, you can test if Apache2 is running:

  1. Open your browser and enter the external IP address of your VM (found on the VM Instances page).
  2. You should see the default Apache2 welcome page, confirming that your web server is running.

Step 12: Troubleshooting (Optional)

If you don’t see the Apache2 welcome page:

  • Ensure Apache2 is running by executing: sudo systemctl status apache2.
  • Check the firewall settings in GCP to ensure port 80 is open.

Conclusion

Congratulations! You’ve successfully created a VM on GCP and installed Apache2. You now have a working web server that you can use to host static content or expand with dynamic frameworks like PHP or Node.js.

This simple setup is just the beginning. From here, you can explore additional security measures, SSL certificates, or even create automated deployment pipelines.

Top comments (0)