Deploying Symfony on OpenShift

Symfony is one of the most mature and longest-surviving frameworks in the PHP world. It’s modular component-based design allows it to be flexible while providing high performance.

This goal of this tutorial is not to teach Symfony as a framework. To do that, you should follow the official documentation. This tutorial expects you to be familiar with Symfony and to have an application you want to deploy on OpenShift.

There is a community-contributed cartridge that simplifies the deployment of Symfony on OpenShift in case you are starting a new project.

This guide is based on the following articles:

Get Started

Create an empty application:

$ rhc app create sym php-5.4 mysql-5.5

and remove the provided index.php file.

Enable Composer

To get composer up an running, create a marker file:

$ touch .openshift/markers/use_composer

Provide Application Code

Copy your application code to the repository or create a new application:

$ composer create-project symfony/framework-standard-edition php

and move all the file to the repository root:

$ mv php/* .
$ mv php/.gitignore .
$ rmdir php

Allow OpenShift to Pre-build Bootstrap Cache

OpenShift will run composer to install the dependencies of the project, however Symfony needs to generate cache file for bootstapping itself.

Create a new action hook .openshift/action_hooks/build with content:

php `find -name build_bootstrap.php`

and make it executable:

$ chmod +x .openshift/action_hooks/build

Remove parameters.yml from .gitignore

In order to make sure parameters.yml can be commited, edit the .gitignore file and remove:



Symfony can auto-load a configuration file providing database connectivity parameters.

Edit file app/config/config.yml and add a line to the imports: section:

    - { resource: params.php }

and create databse configuration file app/config/params.php:

  $container->setParameter('database_host', getenv("OPENSHIFT_MYSQL_DB_HOST"));
  $container->setParameter('database_port', getenv("OPENSHIFT_MYSQL_DB_PORT"));
  $container->setParameter('database_name', getenv("OPENSHIFT_APP_NAME"));
  $container->setParameter('database_user', getenv("OPENSHIFT_MYSQL_DB_USERNAME"));
  $container->setParameter('database_password', getenv("OPENSHIFT_MYSQL_DB_PASSWORD"));

Similarly you can configure your application to connect to other databases.