Edit on GitHub
Report issue
Page history

Run Symfony on App Engine flexible environment

Author(s): @bshaffer ,   Published: 2017-03-15

Brent Shaffer | Developer Programs Engineer | Google

Contributed by Google employees.

You can check out PHP on Google Cloud to get an overview of PHP itself and learn ways to run PHP apps on Google Cloud.


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


Follow the official documentation for installing symfony from symfony.com.


  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. Create an app.yaml file with the following contents depending on the version of Symfony that you are using:

    Symfony 2 and 3

    runtime: php
    env: flex
      document_root: web
      front_controller_file: app.php

    Symfony 4 and above

    runtime: php
    env: flex
      document_root: public
      front_controller_file: index.php
        APP_ENV: "prod"
  2. Replace post-install-cmd in composer.json with the following script:

    "post-install-cmd": [
        "chmod -R ug+w $APP_DIR/var"

    In the context of Symfony's composer.json, it will look like this:

    Add post-install-cmd scripts to composer.json

  3. Run the following command to deploy your app:

    gcloud app deploy
  4. Visit http://YOUR_PROJECT_ID.appspot.com to see the Symfony welcome page!

Connect to CloudSQL with Doctrine


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

  2. Follow the instructions to install 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.

  3. Enable the CloudSQL APIs in your project.

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

    gcloud sql instances describe YOUR_INSTANCE_NAME
  5. Start the Cloud SQL proxy and replace YOUR_INSTANCE_CONNECTION_NAME with the connection name you retrieved in the previous step:

    cloud_sql_proxy -instances=YOUR_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 app/config/parameters.yml so the database fields pull from environment variables. We've also added some default values for environment variables which are not needed for all environments. Notice we've added the parameter database_socket, as Cloud SQL uses sockets to connect:

        database_host: '%env(DB_HOST)%'
        database_name: '%env(DB_DATABASE)%'
        database_port: '%env(DB_PORT)%'
        database_user: '%env(DB_USERNAME)%'
        database_password: '%env(DB_PASSWORD)%'
        database_socket: '%env(DB_SOCKET)%'
        # Set sane environment variable defaults.
        env(DB_HOST): ""
        env(DB_PORT): 3306
        env(DB_SOCKET): ""
        # Mailer configuration
        # ...
  2. Modify your Doctrine configuration in app/config/config.yml and add a line for unix_socket using the parameter we added:

    # Doctrine Configuration
            driver: pdo_mysql
            host: '%database_host%'
            port: '%database_port%'
            dbname: '%database_name%'
            user: '%database_user%'
            password: '%database_password%'
            charset: UTF8
            # add this parameter
            unix_socket: '%database_socket%'
        # ORM configuration
        # ...
  3. Use the symfony CLI to connect to your instance and create a database for the application. Be sure to replace YOUR_DB_PASSWORD below with the root password you configured:

    # create the database using doctrine
        php bin/console doctrine:database:create
  4. Modify your app.yaml file with the following contents:

    runtime: php
    env: flex
        document_root: web
        front_controller_file: app.php
        ## Set these environment variables according to your CloudSQL configuration.
        DB_DATABASE: symfony
        DB_USERNAME: root
        # for Cloud SQL, set this value to the Cloud SQL connection name,
        # e.g. "project:region:cloudsql-instance"
        cloud_sql_instances: "YOUR_CLOUDSQL_CONNECTION_NAME"
  5. Replace each instance of YOUR_DB_PASSWORD and YOUR_CLOUDSQL_CONNECTION_NAME with the values you created for your Cloud SQL instance above.


  1. In order to test that our connection is working, modify the Default Controller in src/AppBundle/Controller/DefaultController.php so that it validates our Doctrine connection:

    namespace AppBundle\Controller;
    use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
    use Symfony\Bundle\FrameworkBundle\Controller\Controller;
    use Symfony\Component\HttpFoundation\Request;
    class DefaultController extends Controller
            * @Route("/", name="homepage")
        public function indexAction(Request $request)
            $entityManager = $this->get('doctrine.orm.entity_manager');
            if ($entityManager->getConnection()->connect()) {
                echo 'DOCTRINE WORKS';
            // replace this example code with whatever you need
            return $this->render('default/index.html.twig', [
                'base_dir' => realpath($this->getParameter('kernel.project_dir')).DIRECTORY_SEPARATOR,
  2. Now you can run locally and verify the connection works as expected.

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

    gcloud app deploy

What's next

  1. Check out the Databases and the Doctrine ORM documentation for Symfony.
  2. View a Symfony Demo Application for App Engine flexible environment.

Submit a tutorial

Share step-by-step guides

Submit a tutorial

Request a tutorial

Ask for community help

Submit a request

View tutorials

Search Google Cloud tutorials

View tutorials

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see our Site Policies. Java is a registered trademark of Oracle and/or its affiliates.