Application Development

Introducing PHP 7.2 runtime on the App Engine standard environment

php 7.2

Today, Google Cloud is announcing the availability of PHP 7.2 on the App Engine standard environment, our latest second-generation runtime. It is available today in beta, so you can build and deploy reliable applications with even more flexibility. Similar to the other second-generation runtimes on App Engine standard (Python 3.7 and Node.js 8), PHP 7.2 is open and idiomatic, so you can run popular frameworks like Symfony, Laravel, and even WordPress.

You can see more on using PHP 7.2 on App Engine standard here:

PHP 7.2 apps get all the benefits of App Engine standard: auto-scaling instances in seconds, allowing your app to handle sudden bursts in demand; faster deployment times—usually in under a minute for PHP apps; and scaling down to zero instances if needed, making it perfect for apps that operate at any scale. Since PHP 7.2 is a second-generation runtime, you can run any code without restrictions. Existing PHP apps and open source libraries will run unmodified.

If you’re a long-time App Engine fan, you may have noticed that we are shipping new runtimes faster than we have in the past. This is due to the introduction of second-generation runtimes, which are based on gVisor technology. Since we don’t need to custom-modify language runtimes to work with App Engine, support for new languages can be launched quickly.

Getting started with PHP 7.2 for App Engine

To help you get started with PHP on App Engine, we’ve built a collection of getting started guides and samples to walk you through creating your code, using our APIs and services, and deploying to production. Additionally, you can check out these tutorials for deploying WordPress, Laravel, Symfony, and Slim Framework applications.

To deploy a simple application to App Engine on PHP 7.2, download and install the Google Cloud SDK. Once you’ve done this, create app.yaml and index.php files as shown below:

  # app.yaml
runtime: php72
  <?php
// index.php
echo 'Hello, World!';

And finally, deploy to App Engine:

  gcloud app deploy

Once the application is deployed, you can view it in the browser, or go to the Cloud Console to view the running instances. To explore a broader example, see Building an App.

Understanding framework-friendly routing

PHP 7.2 and the other new runtimes are opinionated on using a front controller. This means all traffic goes through a single PHP file. Most modern PHP frameworks use a front controller, including Laravel, Symfony, and Drupal. By default, App Engine serves all traffic to either public/index.php or index.php, but this can be changed by updating the entrypoint directive in app.yaml:


  # app.yaml
runtime: php72
entrypoint: serve web/index.php

The serve command handles running the App Engine web server. Read more about it in the app.yaml reference documentation.

If you are new to the world of front controllers, use a microframework such as the Slim Framework to get started. It provides a very straightforward way to define routes in your application, as shown here:

  $app = new Slim\App();
$app->get('/', function ($request, $response) {
    // Use the Null Coalesce Operator in PHP7
    $name = $request->getQueryParams()['name'] ?? 'World';
    return $response->getBody()->write("Hello, $name");
});
$app->run();

For applications that do not map well to a front controller (e.g. a flat-file PHP application), consider doing something similar to this WordPress front controller, where a wrapper controller routes requests to the other files in the application.

Using PHP 7.2 with Google Cloud APIs and services

The new PHP 7.2 runtime supports Google Cloud client libraries, so you can integrate Google Cloud services into your app and run it on App Engine, Compute Engine, or any other platform. At Google Cloud Next we also announced Cloud Scheduler and Cloud Tasks—services that mirror App Engine's popular scheduling and queueing functionality (App Engine cron and Task Queues). We are progressively evolving all of these original App Engine APIs to make them accessible from anywhere. However, at this time, the original App Engine-only APIs are not available for second-generation runtimes, including PHP 7.2.

To use Google Cloud client libraries, install them using Composer, like this:

  # Install just the Datastore library
composer require google/cloud-datastore
# Install ALL Google Cloud client libraries
composer require google/cloud

Try PHP 7.2 today

You can write apps using PHP 7.2 today on the App Engine standard environment—check out the docs to get started. And since it's a second-generation runtime, you can expect much faster updates, so stay tuned!