Skip to content
Branch: master
Find file History
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.
src Add stackdriver to symfony php72 (#817) Jan 30, 2019

Run Symfony on App Engine Standard for PHP 7.2

This tutorial will walk you through how to create and deploy a Symfony project to App Engine Standard for PHP 7.2. You will learn how to:

  1. Create a Symfony project
  2. Configure it to run in the App Engine environment
  3. Deploy it to App Engine
  4. Set up a Cloud SQL database
  5. Configure Doctrine to communicate with Cloud SQL

Note: This repository is just a tutorial and is not a Symfony project in and of itself. The steps will require you to set up a new Symfony project in a separate directory.


  1. Create a project in the Google Cloud Platform Console and make note of your project ID.
  2. Enable billing for your project.
  3. Install the Google Cloud SDK.


This tutorial uses the Symfony Demo application. Run the following command to install it:

composer create-project symfony/symfony-demo:^1.2 $PROJECT_DIR


  1. Run the app with the following command:

     php bin/console server:run
  2. Visit http://localhost:8000 to see the Symfony Welcome page.


  1. Remove the scripts section from composer.json in the root of your project. You can do this manually, or by running the following line of code below in the root of your Symfony project:

    php -r "file_put_contents('composer.json', json_encode(array_diff_key(json_decode(file_get_contents('composer.json'), true), ['scripts' => 1]), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));"

    Note: The composer scripts run on the Cloud Build server. This is a temporary fix to prevent errors prior to deployment.

  2. Copy the app.yaml file from this repository into the root of your project and replace YOUR_APP_SECRET with a new secret or the generated secret in .env:

    runtime: php72
        APP_ENV: prod
    # URL handlers
    # ...

    NOTE Read more about the env and secret parameters in Symfony's documentation.

  3. Override the cache and log directories so that they use /tmp in production. This is done by modifying the functions getCacheDir and getLogDir to the following in src/Kernel.php:

    class Kernel extends BaseKernel
        public function getCacheDir()
            if ($this->environment === 'prod') {
                return sys_get_temp_dir();
            return $this->getProjectDir() . '/var/cache/' . $this->environment;
        public function getLogDir()
            if ($this->environment === 'prod') {
                return sys_get_temp_dir();
            return $this->getProjectDir() . '/var/log';
        // ...

    NOTE: This is required because App Engine's file system is read-only.

  4. Deploy your application to App Engine:

     gcloud app deploy
  5. Visit to see the Symfony demo landing page.

The homepage will load when you view your application, but browsing to any of the other demo pages will result in a 500 error. This is because you haven't set up a database yet. Let's do that now!

Connect to Cloud SQL with Doctrine

Next, connect your Symfony demo application with a Cloud SQL database. This tutorial uses the database name symfonydb and the username root, but you can use whatever you like.


  1. Follow the instructions to set up a Google Cloud SQL Second Generation instance for MySQL.

  2. Create a database for your Symfony application. Replace INSTANCE_NAME with the name of your instance:

    gcloud sql databases create symfonydb --instance=INSTANCE_NAME
  3. Enable the Cloud SQL APIs in your project.

  4. Follow the instructions to install and run the Cloud SQL proxy client on your local machine. The Cloud SQL proxy is used to connect to your Cloud SQL instance when running locally. This is so you can run database migrations locally to set up your production database.

    • Use the Cloud SDK from the command line to run the following command. Copy the connectionName value for the next step. Replace INSTANCE_NAME with the name of your instance:

      gcloud sql instances describe INSTANCE_NAME
    • Start the Cloud SQL proxy and replace INSTANCE_CONNECTION_NAME with the connection name you retrieved in the previous step:

      cloud_sql_proxy -instances=INSTANCE_CONNECTION_NAME=tcp:3306 &

    Note: Include the -credential_file option when using the proxy, or authenticate with gcloud, to ensure proper authentication.


  1. Modify your Doctrine configuration in config/packages/doctrine.yml and change the parameters under doctrine.dbal to be the following:

    # Doctrine Configuration
            driver: pdo_mysql
            url: '%env(resolve:DATABASE_URL)%'
        # ORM configuration
        # ...
  2. Use the Symfony CLI to connect to your instance and create a database for the application. Be sure to replace DB_PASSWORD with the root password you configured:

    # create the database using doctrine
    DATABASE_URL="mysql://root:DB_PASSWORD@" \
        bin/console doctrine:schema:create
  3. Modify your app.yaml file with the following contents. Be sure to replace DB_PASSWORD and INSTANCE_CONNECTION_NAME with the values you created for your Cloud SQL instance:

    runtime: php72
      APP_ENV: prod
      # Add the DATABASE_URL environment variable
      DATABASE_URL: mysql://root:DB_PASSWORD@localhost?unix_socket=/cloudsql/INSTANCE_CONNECTION_NAME;dbname=symfonydb
    # URL handlers
    # ...


  1. Now you can run locally and verify the connection works as expected.

        php bin/console server:run
  2. Reward all your hard work by running the following command and deploying your application to App Engine:

    gcloud app deploy

Set up Stackdriver Logging and Error Reporting

Install the Google Cloud libraries for Stackdriver integration:

# Set the environment variable below to the local path to your symfony project
composer require google/cloud-logging google/cloud-error-reporting

Copy over App Engine files

For your Symfony application to integrate with Stackdriver Logging and Error Handling, you will need to copy over the monolog.yaml config file and the ExceptionSubscriber.php Exception Subscriber:

# clone the Google Cloud Platform PHP samples repo somewhere
cd /path/to/php-samples
git clone

# enter the directory for the symfony framework sample
cd appengine/php72/symfony-framework/

# copy monolog.yaml into your Symfony project
cp config/packages/prod/monolog.yaml \

# copy ExceptionSubscriber.php into your Symfony project
cp src/EventSubscriber/ExceptionSubscriber.php \

The two files needed are as follows:

  1. config/packages/prod/monolog.yaml - Adds Stackdriver Logging to your Monolog configuration.
  2. src/EventSubscriber/ExceptionSubscriber.php - Event subscriber which sends exceptions to Stackdriver Error Reporting.

If you'd like to test the logging and error reporting, you can also copy over LoggingController.php, which exposes the routes /en/logging/notice and /en/logging/exception for ensuring your logs are being sent to Stackdriver:

# copy LoggingController.php into your Symfony project
cp src/Controller/LoggingController.php \
  1. src/Controller/LoggingController.php - Controller for testing logging and exceptions.

View application logs and errors

Once you've redeployed your application using gcloud app deploy, you'll be able to view Application logs in the Stackdriver Logging UI, and errors in the Stackdriver Error Reporting UI! If you copied over the LoggingController.php file, you can test this by pointing your browser to and

You can’t perform that action at this time.