Services >> Cloud >> GCP >> Services >> App Engine >> How to deploy a sample PHP application to App Engine

 


Example 1: Using Cloud Shell in GCP Cloud console and existing project


## Enable Google App Engine Admin API

GCP Console -> APIs & Services -> Library

Type App Engine Admin API

Click on the result App Engine Admin API

Note if "ENABLE" button is shown, if yes, means it was not yet enable, click it to enable the API.  If "MANAGE" is shown, means already enabled

Open cloud shell

In the cloud shell:

  gcloud auth list
  gcloud config list project
  git clone https://github.com/GoogleCloudPlatform/php-docs-samples.git
  cd php-docs-samples/appengine/standard/helloworld/
  gcloud app deploy
  gcloud app browse


note the app URL listed in the output of that command, browse accordingly

 

 


Example 2: Using Cloud SDK and creating new project

## Assuming GCP cloud SDK was already installed in a Windows machine (at c:\home\bin\gcloud) and gcloud init done to define the account and region

c:\home\bin\gcloud>gcloud config list
[accessibility]
screen_reader = False
[compute]
region = asia-southeast1
zone = asia-southeast1-a
[core]
account = xxxxxxxxxxxxxxxx@gmail.com
disable_usage_reporting = True
project = xxxxxxxxx

Your active configuration is: [default]



c:\home\bin\gcloud>gcloud projects create myfaqbase-gae --name=myfaqbase-gae
Create in progress for [https://cloudresourcemanager.googleapis.com/v1/projects/myfaqbase-gae].
Waiting for [operations/cp.5923585435789889908] to finish...done.
Enabling service [cloudapis.googleapis.com] on project [myfaqbase-gae]...
Operation "operations/acat.p2-291748116573-8d1feaa5-f357-4644-98bf-915b287e2d2c" finished successfully.


c:\home\bin\gcloud>gcloud config set project myfaqbase-gae
Updated property [core/project].

c:\home\bin\gcloud>gcloud config list project
[core]
project = myfaqbase-gae

Your active configuration is: [default]



## Check if app engine api (appengine.googleapis.com) and cloud build (cloudbuild.googleapis.com) are enabled

c:\home\bin\gcloud>gcloud services list --enabled
NAME                              TITLE
bigquery.googleapis.com           BigQuery API
bigquerystorage.googleapis.com    BigQuery Storage API
cloudapis.googleapis.com          Google Cloud APIs
clouddebugger.googleapis.com      Cloud Debugger API
cloudtrace.googleapis.com         Cloud Trace API
datastore.googleapis.com          Cloud Datastore API
logging.googleapis.com            Cloud Logging API
monitoring.googleapis.com         Cloud Monitoring API
servicemanagement.googleapis.com  Service Management API
serviceusage.googleapis.com       Service Usage API
sql-component.googleapis.com      Cloud SQL
storage-api.googleapis.com        Google Cloud Storage JSON API
storage-component.googleapis.com  Cloud Storage
storage.googleapis.com            Cloud Storage API



## Enable if not

c:\home\bin\gcloud>gcloud services enable appengine.googleapis.com
Operation "operations/acat.p2-291748116573-12f28865-8ff4-4dbd-92b0-e02dbc8b72aa" finished successfully.


## To run gcloud app deploy, we also have to enable Cloud Build API, but this new project is not yet billing enabled
## link it to the billing enabled project

c:\home\bin\gcloud>gcloud services enable cloudbuild.googleapis.com --billing-project=insert-your-billing-enabled-project-here


## If the above does not work, use the gcloud beta command to enable billing




c:\home\bin\gcloud>gcloud beta billing projects link myfaqbase-gae --billing-account=your-billing-accoundID
billingAccountName: billingAccounts/xxxxxx-xxxxxx-xxxxxx
billingEnabled: true
name: projects/myfaqbase-gae/billingInfo
projectId: myfaqbase-gae



## create project directory on your Cloud SDK machine, cd into it and clone sample app engine PHP application

c:\home\bin\gcloud>mkdir myfaqbase-gae

c:\home\bin\gcloud>cd myfaqbase-gae

c:\home\bin\gcloud\myfaqbase-gae>git clone https://github.com/GoogleCloudPlatform/php-docs-samples.git
Cloning into 'php-docs-samples'...
remote: Enumerating objects: 19219, done.
remote: Counting objects: 100% (58/58), done.
remote: Compressing objects: 100% (52/52), done.
remote: Total 19219 (delta 21), reused 12 (delta 5), pack-reused 19161
Receiving objects: 100% (19219/19219), 20.74 MiB | 17.68 MiB/s, done.
Resolving deltas: 100% (12783/12783), done.


c:\home\bin\gcloud\myfaqbase-gae>cd php-docs-samples\appengine\standard\helloworld/

c:\home\bin\gcloud\myfaqbase-gae\php-docs-samples\appengine\standard\helloworld>dir
 Volume in drive C has no label.
 Volume Serial Number is 5A42-6539

 Directory of c:\home\bin\gcloud\myfaqbase-gae\php-docs-samples\appengine\standard\helloworld

03/02/2022  04:42 pm    <DIR>          .
03/02/2022  04:42 pm    <DIR>          ..
03/02/2022  04:42 pm               286 app.yaml
03/02/2022  04:42 pm                 8 composer.json
03/02/2022  04:42 pm                31 index.php
03/02/2022  04:42 pm             1,172 phpunit.xml.dist
03/02/2022  04:42 pm               291 README.md
03/02/2022  04:42 pm    <DIR>          test
               5 File(s)          1,788 bytes
               3 Dir(s)  270,163,283,968 bytes free



c:\home\bin\gcloud\myfaqbase-gae\php-docs-samples\appengine\standard\helloworld>type app.yaml
runtime: php74

# Defaults to "serve index.php" and "serve public/index.php". Can be used to
# serve a custom PHP front controller (e.g. "serve backend/index.php") or to
# run a long-running PHP script as a worker process (e.g. "php worker.php").
#
# entrypoint: serve index.php



c:\home\bin\gcloud\myfaqbase-gae\php-docs-samples\appengine\standard\helloworld>more index.php
<?php

echo 'hello world!';


c:\home\bin\gcloud\myfaqbase-gae\php-docs-samples\appengine\standard\helloworld>gcloud app create --project=myfaqbase-gae --region=asia-southeast1
You are creating an app for project [myfaqbase-gae].
WARNING: Creating an App Engine application for a project is irreversible and the region
cannot be changed. More information about regions is at
<https://cloud.google.com/appengine/docs/locations>.

Creating App Engine application in project [myfaqbase-gae] and region [asia-southeast1]....done.
Success! The app is now created. Please use `gcloud app deploy` to deploy your first app.


## Deploy the app

c:\home\bin\gcloud\myfaqbase-gae\php-docs-samples\appengine\standard\helloworld>gcloud app deploy
Services to deploy:

descriptor:                  [c:\home\bin\gcloud\myfaqbase-gae\php-docs-samples\appengine\standard\helloworld\app.yaml]
source:                      [c:\home\bin\gcloud\myfaqbase-gae\php-docs-samples\appengine\standard\helloworld]
target project:              [myfaqbase-gae]
target service:              [default]
target version:              [20220203t171722]
target url:                  [https://myfaqbase-gae.as.r.appspot.com]
target service account:      [App Engine default service account]


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

Beginning deployment of service [default]...
#============================================================#
#= Uploading 0 files to Google Cloud Storage                =#
#============================================================#
File upload done.
Updating service [default]...done.
Setting traffic split for service [default]...done.
Deployed service [default] to [https://myfaqbase-gae.as.r.appspot.com]

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

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


## Browse using the default browser

c:\home\bin\gcloud\myfaqbase-gae\php-docs-samples\appengine\standard\helloworld>gcloud app browse
Opening [https://myfaqbase-gae.as.r.appspot.com] in a new tab in your default browser.