DEV Community

Max Shestov
Max Shestov

Posted on • Updated on

Install Apache Server and PHP on Google Cloud Platform.

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
Alt Text
[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)
Alt Text
Boot disc - Standard persistent disk, Image Ubuntu, 20.04 LTS (LTS — Long Term Support)
Alt Text
Firewall - checkmark
Allow HTTP traffic
Allow HTTPS traffic
Alt Text
Click Create.
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
Enter fullscreen mode Exit fullscreen mode

Now, install Apache

sudo apt-get install apache2 php libapache2-mod-php
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Verify PHP Version

php --version
Enter fullscreen mode Exit fullscreen mode

In Google Cloud console on your instance copy external IP and type it in a browser to verify that Apache is running:
Alt Text
Do not use https:// in the URL as the encrypted connection is not configured at this moment and the page won’t open.

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
Alt Text
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.
Alt Text
It usually takes effect within a few next hours.

Discussion (0)