Monday, October 21, 2024

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

 

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:-

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

  1. Once logged into GCP, navigate to the Google Cloud Console.
  2. Click the project dropdown on the top-left and select New Project.
  3. 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)

  1. In the Compute Engine section, click on VM instances.
  2. Click Create Instance to start configuring your new VM.
  3. 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.

4. 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

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

Step 8: Start Apache2 Service

Once Apache2 is installed, start the service:

sudo systemctl start apache2

Step 9: Enable Apache2 to Start on Boot

You want Apache2 to start automatically whenever the VM reboots:

sudo systemctl enable apache2

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.

Happy coding!

Connect with Me:

No comments:

Post a Comment

Wireless Security Configuration: Protect Your Network Now!

Introduction: In today’s connected world, wireless networks are as common as smartphones, and they’re often the gateway to our personal, pr...