Edit on GitHub
Report issue
Page history

Submitted by @{[ TutorialCtrl.tutorial.github_username ]} {[ TutorialCtrl.tutorial.date_published | date:'mediumDate' ]}

title: How to Run Symfony Hello World on App Engine Standard Environment description: Run Symfony Standard on Google App Engine standard environment. Symfony is a popular set of reusable PHP components and a PHP framework for websites and web applications. author: jimtravis tags: App Engine, Symfony, PHP date_published: 2017-02-08


You can use Symfony with PHP on Google App Engine to develop your web apps. Getting to Hello World with Symfony on App Engine takes just a few minutes. We've provided modified source code for Symfony on GitHub. You can download the code and deploy it to App Engine by following the steps in this tutorial.

Symfony is a set of reusable PHP components and a PHP framework for websites and web applications.

Google App Engine's platform-as-a-service (PaaS) features let you easily run on Google's infrastructure, so your app can automatically scale to serve millions of users, while keeping your costs under control.

Objectives

Prerequisites

  • Develop using Mac OS X or Microsoft Windows.

  • Install Git.

  • Install Composer. This tutorial assumes that you followed the instructions to install Composer globally. Ensure that your system meets the system requirements. Composer will require that your PHP installation supports cURL.

  • Install and then initialize the Google Cloud SDK.

  • Create a new Google Cloud Platform project or retrieve the project ID of an existing project from the Google Cloud Platform Console. You can retrieve a list of your existing project IDs by using the gcloud command-line tool. From the command line, run:

    gcloud projects list
    

Deploying Hello World with Symfony on App Engine

To deploy Symfony to App Engine, follow these steps.

Get the source code

  1. Download the modified source code from GitHub. In a terminal window, enter the following command:

    composer create-project google/appengine-symfony-starter-project
    
  2. Change the current directory.

    cd appengine-symfony-starter-project
    

Run the app on your local computer

The following steps will serve your application on http://localhost:8080 using the script dev_appserver.py, which was installed with the Google Cloud SDK.

  1. Run the following command from the root of the project:

    composer run-script server --timeout=0
    

    This step is critical, as App Engine uses a read-only filesystem, so the cache files must be written first.

  2. After the instance starts, open your browser to http://localhost:8080.

  3. When the page loads, you see a simple text message that says Homepage., which is Symfony's "hello world" text.

Note: If the page fails to load, select Logs in the Google App Engine Launcher to see what error conditions caused the failure.

Deploy the app to the cloud

  1. To deploy the code to App Engine, enter the following command:

    composer run-script deploy --timeout=0
    

    Just like above, this script warms the Symfony file cache before deploying to App Engine, as App Engine uses a read-only filesystem.

  2. When the deployment finishes, your app will be serving traffic at http://[YOUR_PROJECT_ID].appspot.com.

    You can run the following command to open your browser to your app's URL:

    gcloud app browse
    
  3. When the page loads, you will see the message: Homepage.

Note: If the page fails to load, check the logs.

Building the Cache

This application includes two helper scripts, scripts/deploy.php and scripts/server.php, which can be run with composer run-script. Both of these are used to prime the file cache, which is required for the application to run due to App Engine Standard's read-only filesystem. The scripts are convenient wrappers for Symfony's cache:clear and cache:warmup commands, and are the equivalent to the following:

# This is equivalent to running `composer run-script server`
app/console cache:clear --no-debug --env=dev
app/console cache:warmup --no-debug --env=dev
dev_appserver.py .
# This is equivalent to running `composer run-script deploy`
app/console cache:clear --no-debug --env=prod
app/console cache:warmup --no-debug --env=prod
gcloud app deploy

Using Cloud Storage as your Filesystem

Setup

App Engine doesn't provide access to a file system, like the one you might use on a local computer or a server. You can use Cloud Storage in place of a local file system.

  1. Create and retrieve the name of your Google Cloud Storage bucket.
  2. Edit app/config/parameters.yml and replace YOUR_GCS_BUCKET_NAME with the bucket name you created above.
  3. Browse to /storage

This script will write a file "helloworld.txt" to the Cloud Storage bucket specified in parameters.yml, and then reads it back and displays it in the browser.

By registering Cloud Storage as a stream wrapper, the gs:// path can be used to read and write to a Cloud Storage bucket as if it was a filesystem.

Configuring the App Engine application

App Engine applications require a configuration file, named app.yaml, to configure the app. The Symfony project adds this file to the code from the original branch. In the handlers section of the file, you can see the URL routing handlers for the app. For example, you'll find the handler for your static assets and front controller there. The handlers look like this:

handlers:
# tell appengine where our static assets live
- url: /bundles
  static_dir: web/bundles
# the symfony front controller
- url: /.*
  script: web/app.php

The url setting defines the URL that is being handled. The value for script points to the file that contains the scripting code to run when the URL is requested.

Next steps

See more by @{[ TutorialCtrl.tutorial.github_username ]} and more tagged {[ tag ]}{[ $last ? '' : ', ' ]}

Submit a Tutorial

Share step-by-step guides

SUBMIT A TUTORIAL

Request a Tutorial

Ask for community help

SUBMIT A REQUEST

GCP Tutorials

Tutorials published by GCP

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.

Monitor your resources on the go

Get the Google Cloud Console app to help you manage your projects.