How to run Hello World with PHP

It's easy to get started developing PHP apps that run on Google Cloud Platform. And because the apps you create will run on the same infrastructure that powers all of Google's products, you can be confident that they will scale to serve all of your users, whether there are a few or millions of them. This tutorial gets you going fast by deploying a simple Hello World app.

This tutorial assumes that you are familiar with PHP and that you have PHP 5.6, 7.0, or 7.1 installed. You also need to download Composer. This tutorial assumes the Composer executable is installed globally.

This 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 GCP Console to create a new GCP project, create an App Engine application, and enable billing:
    Go to App Engine

    When prompted, select the region where you want your App Engine application 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 Google Cloud SDK:
      Download the SDK
  3. Alternatively: You can use Google Cloud Shell, which comes with these prerequisites already installed.

Running PHP apps in the Google App Engine flexible environment

The App Engine flexible environment allow you to easily 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

Download and run the app

A simple Hello World sample app written in PHP is available to help you quickly get a feel for deploying an app to Cloud Platform. After you've completed 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

Copy the Hello World sample app to your local machine:

git clone https://github.com/GoogleCloudPlatform/getting-started-php.git

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:

  1. Install dependencies:

    composer install
    
  2. Start a local web server.

    php -S localhost:8000 -t web
    
  3. In your web 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.

Hello World code review

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 Cloud Platform

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 Cloud Platform.

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 App Engine flexible environment. For information about alternatives to the flexible environment, see About the Google Cloud Platform Services.

Deploy the app to Cloud Platform

Enter this command to deploy the sample:

gcloud app deploy

See the app run in the cloud

In your web browser, enter this address:

https://<your-project-id>.appspot.com

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

If you update your app, you can deploy the updated version by entering the same command you used to deploy the app the first time. The new deployment creates a new version of your app and promotes it to the default version. The older versions of your app remain, as do their associated 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.

Congratulations! You've deployed your first PHP app to Cloud Platform!

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

Configuring the App Engine flexible environment

As mentioned in the introduction, an app running in the 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 App Engine flexible environment.

Docker

The Dockerfile describes the runtime environment used by the app. The Hello World app uses the gcr.io/php-mvm-a/php-nginx runtime environment:

Because this app runs in Docker, it can be run without changes on any Docker host, such as Google Container Engine or Kubernetes.

Next Steps: Build the Bookshelf app

Bookshelf app logo

Create a Bookshelf app

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

Cleaning up

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

Send feedback about...