Run Symfony on App Engine flexible environment
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.
Prerequisites
- Create a project in the Cloud Console and make note of your project ID.
- Enable billing for your project.
- Install the Cloud SDK.
Prepare
Follow the official documentation for installing symfony from symfony.com.
Run
Run the app with the following command:
php bin/console server:run
Visit http://localhost:8000 to see the Symfony Welcome page.
Deploy
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 runtime_config: document_root: web front_controller_file: app.php
Symfony 4 and above
runtime: php env: flex runtime_config: document_root: public front_controller_file: index.php env_variables: APP_ENV: "prod"
Replace
post-install-cmd
incomposer.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:Run the following command to deploy your app:
gcloud app deploy
Visit
http://YOUR_PROJECT_ID.appspot.com
to see the Symfony welcome page!
Connect to CloudSQL with Doctrine
Setup
Follow the instructions to set up a Cloud SQL Second Generation instance for MySQL.
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.
Enable the CloudSQL APIs in your project.
Use the Cloud SDK from the command line to run the following command. Copy the
connectionName
value for the next step. ReplaceYOUR_INSTANCE_NAME
with the name of your instance:gcloud sql instances describe YOUR_INSTANCE_NAME
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 withgcloud
, to ensure proper authentication.
Configure
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 parameterdatabase_socket
, as Cloud SQL uses sockets to connect:parameters: 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 # ...
Modify your Doctrine configuration in
app/config/config.yml
and add a line forunix_socket
using the parameter we added:# Doctrine Configuration doctrine: dbal: 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 # ...
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 DB_HOST="127.0.0.1" DB_DATABASE=symfony DB_USERNAME=root DB_PASSWORD=YOUR_DB_PASSWORD \ php bin/console doctrine:database:create
Modify your
app.yaml
file with the following contents:runtime: php env: flex runtime_config: document_root: web front_controller_file: app.php env_variables: ## Set these environment variables according to your CloudSQL configuration. DB_DATABASE: symfony DB_USERNAME: root DB_PASSWORD: YOUR_DB_PASSWORD DB_SOCKET: "/cloudsql/YOUR_CLOUDSQL_CONNECTION_NAME" beta_settings: # 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"
Replace each instance of
YOUR_DB_PASSWORD
andYOUR_CLOUDSQL_CONNECTION_NAME
with the values you created for your Cloud SQL instance above.
Run
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, ]); } }
Now you can run locally and verify the connection works as expected.
DB_HOST="127.0.0.1" DB_DATABASE=symfony DB_USERNAME=root DB_PASSWORD=YOUR_DB_PASSWORD \ php bin/console server:run
Reward all your hard work by running the following command and deploying your application to App Engine:
gcloud app deploy
What's next
- Check out the Databases and the Doctrine ORM documentation for Symfony.
- View a Symfony Demo Application for App Engine flexible environment.
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.