The PHP Runtime

Overview

The PHP runtime is the software stack responsible for installing your application code and its dependencies and running your application. The flexible runtime is declared in app.yaml:

runtime: php
env: flex

Runtimes in the flexible environment are built using Docker. The PHP runtime is based on Debian Jessie. The source code for the PHP runtime is publicly available on GitHub.

Choosing a PHP version

By default, the PHP runtime uses PHP 7.1, but you should explicitly declare your PHP version in the composer.json file to prevent your application from being automatically upgraded when a new version of PHP becomes available. The PHP versions 5.6.* and 7.0.* are also supported.

{
    "require": {
        "php": "7.1.*"
    }
}

Dependencies

The runtime looks for a composer.json file in your application's source directory and uses composer to install any dependencies before starting your application. For more information on declaring and managing packages, see Using PHP Libraries.

Using PHP extensions

The following PHP extensions are pre-installed and enabled on the system:

The following PHP extensions are compiled as shared, but disabled by default:

You can enable an extension by adding a php.ini file in the root of your application and using the extension directive:

; Enable the Redis extension
extension=redis.so

Alternatively, you can also enable an extension by adding a require to your composer.json:

{
    "require": {
        "ext-redis": "*"
    }
}

These packages allow the installation of most popular PHP extensions. If your application requires additional operating-system level dependencies, you will need to extend the PHP runtime or use a custom runtime to install the appropriate packages.

Application startup

The runtime starts your application using an optional entrypoint defined in app.yaml. By default, the entrypoint runs supervisord, which ensures the NGINX server is running.

You can just execute a php script, but it should be able to respond to HTTP requests on the port defined by the environment variable PORT:

entrypoint: php entrypoint.php

An example of this can be seen in Using Cloud Pub/Sub as part of the Bookshelf tutorial.

HTTPS and forwarding proxies

App Engine terminates the HTTPS connection at the load balancer and forwards the request to your application. The NGINX server is configured to set the HTTPS environment variable ($_SERVER["HTTPS"]) to "on" when the request is made via HTTPS.

Some applications also need to ascertain the user's IP address. This is available in the standard X-Forwarded-For header.

Disabled functions

The following functions are disabled with the disable_functions directive in php.ini:

  • exec
  • passthru
  • proc_open
  • proc_close
  • shell_exec
  • show_source
  • symlink
  • system

If you need any of the above functions, add a php.ini file in the root of your application and change the disable_functions directive. Alternatively, set the environment variable WHITELIST_FUNCTIONS to a comma-delimited list of functions to allow.

The Suhosin Security extension is installed for PHP 5.6, and is configured by default to run in simulation mode. Simulation mode logs the use of potentially dangerous functions, which will show in the Cloud Logging UI. Turning the simulation mode off will make your application exit when a potentially dangerous function is used. To enable this additional security feature, add a php.ini in your project root with the following option:

; Prevent the use of potentially dangerous functions rather than logging them
suhosin.simulation = Off

Patched functions

The function parse_str is patched and the second parameter is mandatory. If you call parse_str with only one parameter, it will throw a warning and the function will not be called.

Changing the document root

By default, the document root is /app. This means everything will be served by the web server. You can change the document root by setting the environment variable DOCUMENT_ROOT. You can do this in app.yaml:

runtime: php
vm: true
api_version: 1

runtime_config:
  document_root: web

Customizing NGINX

An nginx-app.conf configuration file is included in the server section of the main NGINX configuration file. The default configuration file looks like this:

location / {
  # try to serve files directly, fallback to the front controller
  try_files $uri /index.php$is_args$args;
}

To define a custom configuration file, put a file named nginx-app.conf in the project root directory. The runtime will override the default file with the file you provided.

By default, index.php is used as the framework front controller. You might need to change this to something different for your project. For instance, the Symfony framework uses app.php instead of index.php:

location / {
  # try to serve files directly, fallback to the front controller
  try_files $uri /app.php$is_args$args;
}

Extending the runtime

The flexible environment PHP runtime can be used to create a custom runtime. Custom runtimes are configured via a Dockerfile. You can generate a Dockerfile based on the PHP runtime using gen-config:

gcloud beta app gen-config --custom

You can then customize the Dockerfile and .dockerignore as desired. Finally, you will need to specify runtime: custom instead of runtime: php in app.yaml. See Customizing the PHP Runtime for more information.

Environment variables

The following environment variables are set by the runtime environment:

Environment variable Description
GAE_INSTANCE The name of the current instance.
GAE_MEMORY_MB The amount of memory available to the application process.
GAE_SERVICE The service name specified in your application's app.yaml file, or if no service name is specified, it is set to default.
GAE_VERSION The version label of the current application.
GCLOUD_PROJECT The Project ID associated with your application, which is visible in the Google Cloud Platform Console
PORT The port that will receive HTTP requests.

You can set additional environment variables with app.yaml.

Metadata server

Each instance of your application can use the Compute Engine metadata server to query information about the instance, including its host name, external IP address, instance ID, custom metadata, and service account information. App Engine does not allow you to set custom metadata for each instance, but you can set [project-wide custom metadata][metadata#projectwide] and read it from your App Engine and Compute Engine instances.

This example function uses the metadata server to get the external IP address of the instance:

function get_external_ip_using_google_cloud()
{
    $metadata = new Google\Cloud\Core\Compute\Metadata();
    $externalIp = $metadata->get(
        'instance/network-interfaces/0/access-configs/0/external-ip');

    return $externalIp;
}

function get_external_ip_using_curl()
{
    $url = 'http://metadata.google.internal/computeMetadata/v1/' .
        'instance/network-interfaces/0/access-configs/0/external-ip';

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Metadata-Flavor: Google'));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    return curl_exec($ch);
}

Send feedback about...

App Engine flexible environment for PHP docs