How to run a Hello World app with PHP

This tutorial gets you started developing with PHP on Google Cloud Platform (GCP) by deploying a Hello World app.

This tutorial assumes the following:

This article is part of the Getting started with PHP on Google Cloud Platform series. After you complete the prerequisites listed under Before you begin, the tutorial takes about 10 minutes to complete.

Before you begin

Before running this sample, take the following steps:

  1. Use the Google Cloud Platform Console to create a GCP project, create an App Engine app, and enable billing:
    Go to App Engine

    When prompted, select the region where you want your App Engine app located, and then enable billing.

  2. Install the following prerequisites locally:
    1. Download and install git.
    2. Download and install Composer. Ensure the Composer executable is installed globally.
    3. Download, install, and initialize the Cloud SDK:
      Download the SDK
  3. Alternatively, you can use Cloud Shell, which comes with these prerequisites installed.

Running PHP apps in the App Engine flexible environment

The App Engine flexible environment lets you deploy and run standard PHP web apps. The flexible environment provides the scaling, monitoring, and load balancing infrastructure, so you can focus on building your app.

To run a PHP app in the flexible environment, you need an app.yaml file that describes your app's runtime configuration.

Downloading and running the sample app

A Hello World sample app written in PHP is available to give you a feel for deploying an app to GCP. After you complete the prerequisites, you can download and deploy the Hello World sample app. This section guides you through getting the code and running the app locally.

Clone the Hello World app

  1. Copy the Hello World sample app to your local machine:

    git clone https://github.com/GoogleCloudPlatform/getting-started-php.git
    
  2. Go to the directory that contains the sample code:

    cd getting-started-php/1-hello-world
    

    Alternatively, you can download the sample as a zip and extract it.

Run the app on your local computer

To run the Hello World app on your local computer, complete the following steps.

  1. Install dependencies:

    composer install
    
  2. Start a local web server:

    php -S localhost:8000 -t web
    
  3. In your browser, enter this address:

    http://localhost:8000
    

You can see the Hello World message from the sample app displayed in the page. This page is delivered by a web server running on your computer.

When you are ready to move forward, press Ctrl+C to terminate the local web server process.

Reviewing the Hello World code

The index.php file contains the PHP code to start a server and respond to requests. This example uses Silex, but you can use other PHP web frameworks.

This code creates a new Silex Application object and then defines two GET routes:

require_once __DIR__ . '/../vendor/autoload.php';

$app = new Silex\Application();

$app->get('/', function () {
    return 'Hello World';
});

$app->get('/goodbye', function () {
    return 'Goodbye World';
});

// @codeCoverageIgnoreStart
if (PHP_SAPI != 'cli') {
    $app->run();
}
// @codeCoverageIgnoreEnd

return $app;

Running Hello World on GCP

This section guides you through deploying the Hello World app to the App Engine flexible environment. This diagram shows the process of deploying and running the app on GCP.

Hello World app deployment process and structure

The App Engine flexible environment runs containers to provision Compute Engine instances. The number of instances scales in response to traffic. To learn more, see the App Engine flexible environment documentation. For information about alternatives to the flexible environment, see About Google Cloud Platform services.

Deploy the app to GCP

To deploy the sample:

gcloud app deploy

See the app run in the cloud

In your web browser, enter the following address. Replace [YOUR_PROJECT_ID] with the name of your GCP project ID.

https://[YOUR_PROJECT_ID].appspot.com

This time, the page that displays the Hello World message is delivered by a web server running on GCP.

If you update your app, you can deploy the updated version by entering the same command you used when you first deployed the app. The new deployment creates a version of your app and promotes it to the default version.

The older versions of your app remain, as do their associated virtual machine (VM) instances. Be aware that all of these app versions and VM instances are billable resources. For information about deleting or stopping your VM instances, see Cleaning up at the end of this tutorial.

The next section discusses the configuration necessary for the app to run on GCP.

Configuring the App Engine flexible environment

An app running in the App Engine flexible environment requires these files:

  • app.yaml
  • Dockerfile

This section explores each requirement.

App configuration

The app.yaml file describes the app's deployment configuration.

runtime: php
env: flex

runtime_config:
  document_root: web

# This sample incurs costs to run on the App Engine flexible environment.
# The settings below are to reduce costs during testing and are not appropriate
# for production use. For more information, see:
# https://cloud.google.com/appengine/docs/flexible/python/configuring-your-app-with-app-yaml
manual_scaling:
  instances: 1
resources:
  cpu: 1
  memory_gb: 0.5
  disk_size_gb: 10

This app.yaml file specifies that the app uses a custom runtime and the flexible environment. For details about configuration settings for the flexible environment, see the App Engine flexible environment documentation.

Docker

The Dockerfile describes the runtime environment that the app uses. Because this app runs in Docker, you can run the app without changes on any Docker host, such as Google Kubernetes Engine or Kubernetes.

What's next

Now that you've completed Hello World, you can start exploring your next sample app: the Bookshelf app. The Bookshelf sample app is more complete, yet still a basic PHP web app that uses multiple GCP features, such as data storage, authentication, and logging.

Start the Bookshelf app tutorial

Cleaning up

If you're done with the tutorials and want to clean up resources that you've allocated, see Cleaning Up.

Was this page helpful? Let us know how we did:

Send feedback about...