Getting started with PHP

This tutorial is intended for those new to building apps in the cloud, such as engineers and web developers, who want to learn key app development concepts as they apply to Google Cloud.



This tutorial uses the following billable components of Google Cloud:

The tutorial is designed to keep your resource usage within the limits of Google Cloud's Always Free tier. To generate a cost estimate based on your projected usage, use the pricing calculator. New Google Cloud users might be eligible for a free trial.

When you finish this tutorial, you can avoid continued billing by deleting the resources you created. For more information, see Cleaning up.

Before you begin

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud Console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Cloud project. Learn how to confirm that billing is enabled for your project.

  4. To create a Firestore database in Native mode, complete the following steps:
    1. In the Cloud Console, go to the Firestore viewer page.
      Go to the Firestore viewer
    2. From the Select a Cloud Firestore mode screen, click Select Native Mode.
    3. Select a location for your Firestore database. This location setting is the default Google Cloud resource location for your Cloud project . This location is used for Google Cloud services in your Cloud project that require a location setting, specifically, your default Cloud Storage bucket and your App Engine app.
    4. Click Create Database.
  5. Enable the App Engine Admin, Cloud Storage, Cloud Logging, and Error Reporting APIs.

    Enable the APIs

  6. In Cloud Shell, open the app's source code.
    Go to Cloud Shell

    Cloud Shell provides command-line access to your Google Cloud resources directly from the browser.

  7. To download the sample code and change into the app directory, click Proceed.
  8. In Cloud Shell, configure the gcloud tool to use your new Google Cloud project:

    # Configure gcloud for your project
    gcloud config set project PROJECT_ID

    Replace PROJECT_ID with the Google Cloud project ID that you created using the Cloud Console.

    The gcloud command-line tool is the primary way you interact with your Google Cloud resources from the command line. In this tutorial, you use the gcloud tool to deploy and monitor your app.

Running your app

  1. Install your app dependencies using composer:

    composer install

  2. Run the PHP built-in web server:
    GOOGLE_CLOUD_PROJECT=PROJECT_ID php -S localhost:8080
    Replace PROJECT_ID with the Google Cloud project ID that you created.
  3. In Cloud Shell, click Web preview , and select Preview on port 8080. This opens a new window with your running app.

Deploying your app to App Engine

Google Cloud offers several options for running your code. For this example, you use App Engine to deploy a scalable app to Google Cloud. With zero-configuration deployments and zero server management, App Engine lets you focus on writing code. Plus, App Engine automatically scales to support sudden traffic spikes.

The app.yaml file is your main configuration file for deploying to App Engine:

runtime: php73

  APP_DEBUG: true
  LOG_CHANNEL: stderr
  1. In your terminal window, deploy the app to App Engine using the gcloud tool:

    # on the command-line
    gcloud app deploy

  2. In your web browser, enter the following URL:

    Replace the following:

    Bookshelf app homepage

For more information on deploying to App Engine, see the PHP 7 runtime environment.

Persisting your data with Firestore

You cannot store information on your App Engine instances, because it is lost if the instance is restarted, and doesn't exist when new instances are created. Instead, you use a database that all your instances read from and write to.

Google Cloud offers several options for storing your data. In this example, you use Firestore to store the data for each book. Firestore is a fully managed, serverless, NoSQL document database that lets you store and query data. Firestore auto scales to meet your app needs, and scales to zero when you're not using it. Add your first book now.

  1. In your web browser, enter the following URL:

    Replace the following:

  2. To create a book for your deployed app, click Add book.

    Add a book to the Bookshelf app
  3. In the Title field, enter Moby Dick.
  4. In the Author field, enter Herman Melville.
  5. Click Save. There is now an entry to your Bookshelf app.

    Moby Dick Bookshelf app entry
  6. In the Cloud Console, to refresh the Firestore page, click Refresh . The data appears in Firestore. The Bookshelf app stores each book as a Firestore document with a unique ID, and all these documents are stored in a Firestore collection. For the purposes of this tutorial, the collection is called books. Example of a Firestore document.

Firestore stores the books by using the Firestore Client Library. Here is an example of fetching a Firestore document:

// Use the client library to call Firestore
use Google\Cloud\Firestore\FirestoreClient;

$firestore = new FirestoreClient([
    'projectId' => $projectId,
$collection =  $firestore->collection($collectionName);
$bookRef = $collection->document($bookId);
$snapshot = $bookRef->snapshot();

For more information on using Firestore, see Adding data to Firestore.

Storing file uploads in Cloud Storage

Now that you've added a book, it's time to add the book cover image. You cannot store files on your instances. A database isn't the right choice for image files. Instead, you use Cloud Storage.

Cloud Storage is the primary blob store for Google Cloud. You can use Cloud Storage to host app assets that you want to share across Google Cloud. To use Cloud Storage, you need to create a Cloud Storage bucket, a basic container to hold your data.

  1. In the Cloud Console, go to the Cloud Storage Browser page.

    Go to the Cloud Storage Browser page

  2. Click Create bucket.
  3. In the Create bucket dialog, enter a name for your bucket by appending your Google Cloud project ID to the string _bucket so the name looks like YOUR_PROJECT_ID_bucket. This name is subject to the bucket name requirements. All other fields can remain at their default values.
  4. Click Create.
  5. After your bucket is created, objects must be made publicly accessible to be viewed by users. To make your objects publicly accessible see Making Data Public.
  6. Click Edit book, and select an image to upload as your book's cover. For example, you can use this public domain image:
    Moby Dick book cover
  7. Click Save. You're redirected to the homepage, where there is an entry to your Bookshelf app.
    Moby Dick Bookshelf app entry

The bookshelf app sends uploaded files to Cloud Storage by using the Cloud Storage Client Library.

// Use the client library to call Cloud Storage
use Google\Cloud\Storage\StorageClient;

$storage = new StorageClient([
    'projectId' => $projectId,
$bucketId = $projectId . '_bucket';
$gcsBucket = $storage->bucket($bucketId);

For more information on using Cloud Storage, see the list of how-to guides.

Monitoring your app using Google Cloud's operations suite

You've deployed your app and created and modified books. To monitor these events for your users, use Application Performance Management.

Monitor logs with Cloud Logging

Cloud Console

  1. In your browser, go to the /logs URL in your app:

    This sends a custom entry to Cloud Logging. The entry has a log severity of NOTICE and contains the message `Hey, you triggered a custom log entry. Good job!`.

  2. Go to the Logs Viewer, where you can monitor your app in real time. When something goes wrong, this is one of the first places to look.
    Cloud Logging Log Viewer
  3. In the resource drop-down list, select GAE Application.
  4. In the logs drop-down list, select All logs.

    There is a row for your custom log entry.

    Entry in Log Viewer


  1. In your terminal window, use the gcloud command-line tool to monitor your app logs by listening for new log entries:

    gcloud app logs tail

  2. In your browser, go to the /logs URL in your app:

    This sends a custom entry to Cloud Logging. The entry has a log severity of NOTICE and contains the message `Hey, you triggered a custom log entry. Good job!`.

    The output of the gcloud command displays the new log entry:

    Waiting for new log entries...
    2019-03-27 22:17:01 default[20190327t151430]  "Hey, you triggered a custom log entry. Good job!"

Monitor errors with Error Reporting

  1. In the Cloud Console, go to the Error Reporting page.
    Go to Error Reporting page
    Error Reporting highlights errors and exceptions in your app and lets you set up alerting around them.
  2. In your browser, go to the /errors URL in your app.

    This generates a new test exception and sends it to Google Cloud's operations suite.

  3. In the Cloud Console, return to the Error Reporting page, and in a few moments the new error is visible. Click Auto Reload so you don't need to manually refresh the page.

    Error message from Error Reporting.

Clean up

To avoid incurring charges to your Google Cloud account for the resources used in this tutorial, either delete the project that contains the resources, or keep the project and delete the individual resources.

Delete the project

  1. In the Cloud Console, go to the Manage resources page.

    Go to Manage resources

  2. In the project list, select the project that you want to delete, and then click Delete.
  3. In the dialog, type the project ID, and then click Shut down to delete the project.

What's next