CRAZYROV STUDIO

Deploying a PHP Website on GCP App Engine.



In this article we will look at how to deploy a PHP based dynamic website on the Google Cloud Platform App Engine.

Google App engine is one of the serverless compute services that is available on GCP. App engine supports different run time which include Node.js, Java, Ruby, C#, Go, Python, and PHP. Or you could also get your own language runtime as a container on App Engine Flexible.

There are a lot of features and functionalities that App engine supports, but in order to keep the video as short as possible I will not be taking you through the features and functionality of App engine except for the 2 main features that I saw as an advantage. First is that this is a fully managed service which means we just worry about our code and GCP is going to manage the platform for us. The second is that the App engine instances scale automatically and it can scale to 0 when there is no traffic at all which for most websites is a huge advantage. I have paid less than Rs.20 in the last months for my website which is not that busy. However the cost will depend on what instance you are selecting and how you have defined your scaling parameters.

For this video I have created a demo website and let's take a look at the directory structure of this website.

You can clone or fork the code and files from https://github.com/crazyrov/gcp_appengine_php_demo

Responsive image

The external packages can be downloaded into the runtime using composer, you can list all the packages with the respective versions.

composer.json
{
    "require": {
        "google/cloud-datastore": "^1.2"
    }
}

The settings for the App engine are defined in a yaml file - app.yaml. This is one of the most important configurations that you would want to consider as you are deploying your app to the App engine. You configure your App Engine app's settings in the app.yaml file. The app.yaml file also contains information about your app's code, PHP runtime, and entrypoint. If we want to use PHP version 7, then it is mandatory that we use a front controller for routing the request to a correct php page. In my site this is defined in index.php.

Here I am using cloud-datastore to store the count that you would see on the homescreen. Because we are using GCP services we will need to provide credentials for using the GCP services in our case the Cloud Datastore, we can use all the storage or other services using the security key which can be defined using an environment variable - GOOGLE_APPLICATION_CREDENTIALS, this is defined here in the app.yaml.

Let's take a look at the app engine runtime configuration files now. Here you will see that there are a number of elements defined. Let's go through them one by one, some of these are mandatory and some are optional. There are 3 categories of elements - Runtime and app elements, Scaling elements and handler elements.

app.yaml
runtime: php73

service: appenginedemo

instance_class: F1

automatic_scaling:
  max_instances: 1
  min_instances: 0

handlers:
  - url: /css
    static_dir: css
    secure: always
    redirect_http_response_code: 301

  # Serve images as static resources.
  - url: /img/(.+\.(gif|png|jpg))$
    static_files: img/\1
    upload: img/.*\.(gif|png|jpg)$

  # This automatically redirects to the front controller in index.php
  - url: .*
    script: auto
    secure: always
    redirect_http_response_code: 301


# [START gae_php_mysql_env]
env_variables:
  GOOGLE_APPLICATION_CREDENTIALS: credential_key.json
  GOOGLE_CLOUD_PROJECT: project_name

Runtime: The name of the runtime environment that is used by your app. To specify PHP 7.3, use.

Service: Required if creating a service. Optional for the default service. The first service in your app engine has to be a default service.

Instance class: This is an optional parameter, by default the runtime considers the instance type of F1 with autoscaling. But if you want to control this you can mention the instance type with this parameter, there are 2 categories of instance types depending on the type of scaling that you are going to opt for. F series incase of autoscaling and B series incase of basic and manual scaling. In case you want to know more refer to the link in the description below.

Handlers : Is a list of URL patterns and descriptions of how a request should be handled. App Engine can handle URLs by executing application code, or by serving static files uploaded with the code, such as images, CSS, or JavaScript.

If you've defined any static handlers, you must also define a handler for all other application traffic to route to an entrypoint. To route to an entrypoint, specify the path to your front controller with the entrypoint element, or use the script element if your app contains a public/index.php or index.php file which is what I have used.

Make sure that the GCP SDK is installed and configured. Also, the GCP should be configured to use the correct project. You can use the command gcloud config set project project_id. You can confirm that you are using the correct project by running the command gcloud config list. Once that is done navigate to the same location as the location of the app.yaml via a command line or powershell or the shell and then use the command gcloud app deploy to start the deployment of this application. Refer the below command and output for reference.

app.yaml
PS I:\Projects\gcp_appengine_php_demo> gcloud app deploy           
Services to deploy:

descriptor:      [I:\Projects\gcp_appengine_php_demo\app.yaml]
source:          [I:\Projects\gcp_appengine_php_demo]
target project:  [nw-command-reference-dev]
target service:  [appenginedemo]
target version:  [20201126t204925]
target url:      [https://appenginedemo-dot-nw-command-reference-dev.el.r.appspot.com]


Do you want to continue (Y/n)?  Y

Beginning deployment of service [appenginedemo]...
#============================================================#
#= Uploading 4 files to Google Cloud Storage                =#
#============================================================#
File upload done.
Updating service [appenginedemo]...done.
Setting traffic split for service [appenginedemo]...done.
Deployed service [appenginedemo] to [https://appenginedemo-dot-nw-command-reference-dev.el.r.appspot.com]

You can stream logs from the command line by running:
  $ gcloud app logs tail -s appenginedemo

To view your application in the web browser run:
  $ gcloud app browse -s appenginedemo

Once the app is deployed succesfully, page the url mentioned in the pervious command on a webbrowser. To check the website deployed as a demo for this article here - https://appenginedemo-dot-nw-command-reference-dev.el.r.appspot.com

Important Links:


Thank you for visting www.crazyrov.com, you can also check out my YouTube channel - crazyRov Studios for Data protection and cloud related technical videos.

CRAZYROVSTUDIO