This guide shows you how to set up Apache and PHP on a Compute Engine virtual machine instance on GCP. The instructions are for Ubuntu 20.04 Linux distribution (goes with PHP 7.4 in its default repositories).
Creating a virtual machine instance
Go to your Google Cloud Console and click
Compute Engine > VM Instances > Create
Create a name for your instance
Region — us-central1(Iowa)
Zone — us-central1-c
[Update] Google introduced E2-micro Free Tier, which is a part of a second-generation VM family on August 1, 2021
Series - E2
Machine type - e2-micro (2 vCPU, 1 GB memory)
Boot disc - Standard persistent disk, Image Ubuntu, 20.04 LTS (LTS — Long Term Support)
Firewall - checkmark
Allow HTTP traffic
Allow HTTPS traffic
It takes a few seconds to create the instance.
Installing Apache and PHP on the instance
On your instance click SSH to open the SSH window
In the opened window update packages that need upgrading typing
sudo apt-get update
sudo apt upgrade -y && sudo reboot
Now, install Apache
sudo apt-get install apache2 php libapache2-mod-php
and install PHP and the extensions with the apt package manager
sudo apt install php php-cli php-fpm php-json php-common php-mysql php-zip php-gd php-mbstring php-curl php-xml php-pear php-bcmath
Verify PHP Version
In Google Cloud console on your instance copy external IP and type it in a browser to verify that Apache is running:
Alternatively, you can install a LAMP (Linux, Apache, MySQL, PHP) stack by Google Click to Deploy automatically.
Upload files on the server.
If you don’t have a connection to your instance check this guide on how to connect to Google Cloud VM Instance.
Map your domain name to your instance.
Reserving a static external IP address
In the Cloud Console click
VPC Network > External IP addresses
In the column Type of your instance select Static
Pointing your domain at your web server IP
Create an A record in your DNS Management console and copy/paste your external IP address.
Create a CNAME record for WWW subdomain and point it to your naked domain.
It usually takes effect within a few next hours.
Install SSL certificate for your website.
Read this article to install auto-renewing Let’s Encrypt SSL Certificate using automated client Certbot.