How to Load Balance a PHP Application on Google Cloud

Steps to Configure Load Balancing

  1. Enable APIs and create a Service account.
  2. Configure Compute Engine instance for Load Balancer.
  3. Install and Configure Apache.
  4. Install and Configure PHP
  5. Create Health Check.
  6. Create Instance Template.
  7. Create a Managed Instance Group.
  8. Reserve Global Region IPv4 and IPv6 addresses.
  9. Create Load Balancer.
  10. Install Google Managed SSL Certificate.
  11. Configure DNS.
  12. Setup Google Cloud CDN.


Step 1: Enable APIs and create a Service account

Cloud SQL >> Cloud SQL Client
Project >> Editor

Step 2: Configure Compute Engine Instance

Setup web application directories

-- yourdomainname
---- public
sudo mkdir -p /var/www/html/yourdomainname/public

Step 3: Install and Configure Apache

sudo apt install apache2
sudo ufw allow 'Apache Full'
sudo a2dissite 000-default
sudo nano /etc/apache2/sites-available/yourdomainname.conf
<VirtualHost *:80>
DocumentRoot /var/www/html/yourdomainname/public <Directory />
Options FollowSymLinks
AllowOverride None
<Directory /var/www/html/yourdomainname/public>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
Require all granted
sudo a2ensite yourdomainname.conf
sudo a2enmod rewrite
sudo systemctl restart apache2
sudo nano /var/www/html/yourdomainname/public/.htaccess
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule .* https://%{HTTP:Host}%{REQUEST_URI} [L,R=permanent]

Step 4: Install and Configure PHP 7.4

sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt update
sudo apt install php7.4 libapache2-mod-php7.4 php7.4-common php7.4-mysql php7.4-xml php7.4-xmlrpc php7.4-curl php7.4-gd php7.4-imagick php7.4-cli php7.4-dev php7.4-imap php7.4-mbstring php7.4-opcache php7.4-soap php7.4-zip php7.4-intl php7.4-bcmath unzip mysql-client -y
sudo nano /etc/php/7.4/apache2/php.ini
upload_max_filesize = 48M 
post_max_size = 48M
memory_limit = 256M
max_execution_time = 600
max_input_vars = 5000
max_input_time = 1000
sudo service apache2 restart

Step 5: Setup Cloud SQL

Using Cloud SQL Proxy

Using Private IP

Step 6: Create Instance Template

Step 7: Cloud SQL Proxy connection

#! /bin/bash
sudo apt update
wget -O cloud_sql_proxy
chmod +x cloud_sql_proxy
sudo mkdir /cloudsql; sudo chmod 777 /cloudsql
./cloud_sql_proxy -instances=INSTANCE_CONNECTION_NAME=tcp:3306

Step 8: Create a Managed Instance Group

Step 9: Reserve Global Region IPv4 and IPv6 Address

Step 10: Create Load Balancer

Backend configuration

Step 11: Create Health Check

Step 12: Frontend Configuration with Google managed SSL

Step 13: Configure DNS and Setup Cloud CDN




Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store



Installation and Set Up of Web Applications on Google Cloud