Install Apache Server and PHP on Google Cloud Platform.
Tags: googlecloud, beginners, tutorial
Published: 2021-03-29 21:32:41

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4i8unihtvar13iaqy177.png)
[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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qrag6f5xd6tbzxytu19e.png)
Boot disc - Standard persistent disk, Image Ubuntu, 20.04 LTS (LTS — Long Term Support)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ef1kphgsvb8jntunpsfb.png)
Firewall - checkmark
Allow HTTP traffic
Allow HTTPS traffic
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zbi9njcktal5321uy2wr.png)
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
```
Now, install Apache
```
sudo apt-get install apache2 php libapache2-mod-php
```
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
```
php --version
```
In Google Cloud console on your instance copy external IP and type it in a browser to verify that Apache is running:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yiegvkd4t5xpmkfwfsyi.png)
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](https://console.cloud.google.com/marketplace/config/click-to-deploy-images/lamp) automatically.

## Upload files on the server.
If you don’t have a connection to your instance check [this guide](https://maxxsh.medium.com/connecting-to-google-cloud-instance-with-ssh-keys-a446d1e3e1b8) 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/54cjnht1g8x38z19r20y.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sz0swariyg3txmg40bq0.png)
It usually takes effect within a few next hours.