---
title: Install New Relic PHP agent in GAE flexible environment
source: https://docs.newrelic.com/docs/apm/agents/php-agent/advanced-installation/install-new-relic-php-agent-gae-flexible-environment
---

With APM's [PHP agent](https://docs.newrelic.com/docs/agents/php-agent/getting-started/introduction-new-relic-php), you can monitor applications that reside in the [Google App Engine (GAE) flexible environment](https://cloud.google.com/appengine/docs/flexible/php/). Adding New Relic to your GAE flex app gives you insight into the health and performance of your app and extends GAE with metrics you can view in [APM](https://docs.newrelic.com/docs/apm/new-relic-apm/getting-started/introduction-new-relic-apm), [browser monitoring](https://docs.newrelic.com/docs/browser/new-relic-browser/getting-started/introduction-new-relic-browser), and dashboards.

This document explains how to add New Relic to your GAE flex app by configuring a [custom runtime](https://cloud.google.com/appengine/docs/flexible/custom-runtimes/about-custom-runtimes), and gives an example of deploying a PHP app with Docker.

> #### ⚠️ IMPORTANT
>
> The New Relic PHP agent can run in a GAE flexible environment using a custom runtime. Due to limitations of other environments, do not use the GAE standard environment or Google App Engine's ["native mode"](https://docs.newrelic.com/docs/accounts-partnerships/partnerships/google-cloud-platform-gcp/google-app-engine-environment#native-mode) installation.

## Build a custom runtime using Docker [#build-runtime]

See [Google's documentation for building custom runtimes](https://cloud.google.com/appengine/docs/flexible/custom-runtimes/build). This example describes how to add New Relic to your GAE flex app by installing the New Relic PHP agent, building a custom runtime, and deploying a single PHP application via Debian. For best results with the GAE flex environment, always use Debian.

For more information about deploying and configuring your PHP app in the GAE flexible environment, see:

-   [Google App Engine's documentation](https://cloud.google.com/appengine/docs/flexible/php/) for PHP
-   [Google App Engine's tutorials](https://cloud.google.com/appengine/docs/flexible/php/tutorials) to deploy a PHP app

**1. Set up the GAE project and install dependencies**

1.  Follow standard procedures to [install the New Relic PHP agent](https://docs.newrelic.com/docs/agents/php-agent/installation/php-agent-installation-overview) for your specific app server, and obtain your license key.
2.  Follow [Google App Engine procedures for PHP](https://cloud.google.com/appengine/docs/flexible/php/quickstart) to create a new Cloud Platform project, create an App Engine application, and complete other prerequisites for the [Google Cloud SDK](https://cloud.google.com/sdk/docs/).

    The Google Cloud SDK provides the `gcloud` command line tool to manage and deploy GAE apps.

**2. Extend the GAE image for PHP**

In this example, the Dockerfile extends the generic PHP image with application files for a single application in Debian. You can add your license key directly to your Dockerfile, or use an environment variable in your `docker run` command.

````dockerfile
FROM gcr.io/google-appengine/php:latest
ENV DOCUMENT_ROOT /app

RUN DEBIAN_FRONTEND=noninteractive apt-get update; DEBIAN_FRONTEND=noninteractive apt-get -y install wget sudo
RUN wget -O- https://download.newrelic.com/NEWRELIC_APT_2DAD550E.public | sudo gpg --import --batch --no-default-keyring --keyring /usr/share/keyrings/download.newrelic.com-newrelic.gpg
RUN echo 'deb [signed-by=/usr/share/keyrings/download.newrelic.com-newrelic.gpg]  http://apt.newrelic.com/debian/ newrelic non-free' | tee /etc/apt/sources.list.d/newrelic.list
RUN DEBIAN_FRONTEND=noninteractive apt-get update && DEBIAN_FRONTEND=noninteractive apt-get -y install newrelic-php5
RUN NR_INSTALL_KEY="new-relic-license-key" NR_INSTALL_SILENT=true newrelic-install install
```

NOTE: For Debian Trixie and later use the following `wget` command instead:

```dockerfile
wget -O- https://download.newrelic.com/NEWRELIC_APT_2DAD550E.public | sudo gpg --dearmor -o /usr/share/keyrings/download.newrelic.com-newrelic.gpg
```

````

**3. Configure your app.yaml**

The `app.yaml` configuration file is required for a GAE flexible environment app with a custom runtime. At a minimum, make sure it contains:

````yaml
env: flex
runtime: custom
runtime_config:
  document_root: .
```

````

**4. Build a Docker image**

The [Dockerfile](http://docs.docker.com/engine/reference/builder/) defines the Docker image to be built and is required for a GAE flexible environment app. When building the Docker image, use the PHP image from Google App Engine. Standard Docker images from other providers may cause problems with GAE.

To build the Docker image, run the following command. Be sure to include the period at the end of the code, to indicate the current directory contains the build files.

````shell
docker build --rm -t Docker-image-name .
```

````

**5. Deploy Docker image to initialized GAE flexible environment**

1.  To deploy your Docker image to your [initialized GAE flexible environment](https://cloud.google.com/sdk/docs/initializing), run the following command:

    ```shell
    gcloud app deploy --project php-app-name
    ```
2.  Wait until the deployment completes.
3.  To view your GAE flex app data in New Relic, go to the [APM **Overview** page](https://docs.newrelic.com/docs/apm/applications-menu/monitoring/apm-overview-page).

## Optional: Disable health checks [#health-checks]

Google App Engine sends [periodic health check requests](https://cloud.google.com/appengine/docs/flexible/go/configuring-your-app-with-app-yaml#health_checks) to confirm that an instance has been successfully deployed, and to check that a running instance maintains a healthy status. A health check is an HTTP request to the URL `/_ah/health`.

If you create a custom runtime, your app must be able to handle a large number of health check requests. Otherwise, your app data may not display correctly in APM.

If you notice performance issues, disable GAE health checks. In your `app.yaml`, add:

```yml
health_check:
  enable_health_check: False
```

## Get New Relic agent troubleshooting logs from GAE [#agent-logs]

Use these resources to troubleshoot your GAE flex environment app:

-   To connect to the GAE instance and start a shell in the Docker container running your code, see [Debugging an instance](https://cloud.google.com/appengine/docs/flexible/php/debugging-an-instance).
-   To redirect New Relic PHP agent logs to the [Stackdriver](http://cloud.google.com/logging/docs/view/logs_viewer_v2) in the [Cloud Platform Console](https://cloud.google.com/compute/docs/console), change the `newrelic.yml` file to:

    ```yml
    log_file_name: STDOUT
    ```
-   To view the logs, use the [Cloud Platform Console's Log Viewer](https://cloud.google.com/appengine/docs/flexible/php/writing-application-logs).
