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:
- Visit Google Cloud and click Get started for free.
- Sign in with your Google account or create a new one.
- 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.
- Click the project dropdown on the top-left and select New Project.
- Name your project (e.g., "Apache2-WebServer"), and click Create.
Step 3: Enable the Compute Engine API
- On the Cloud Console Dashboard, type Compute Engine in the search bar.
- Click Compute Engine and enable it if it’s not already enabled.
- 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.
- Click Create Instance to start configuring your new VM.
- 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.
-
Name: Enter a name for your VM (e.g.,
- 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.
- Click on the SSH button next to your VM instance to open a terminal session in your browser.
- 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:
- Go back to your VM instances page.
- Click on your VM name to open the instance details.
- Scroll down to the Network interfaces section and click View details.
- 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:
- Open your browser and enter the external IP address of your VM (found on the VM Instances page).
- 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.