2014-06-02

This article describes a beta feature. Functionality may change prior to general availability.

This guide will get you up and running with a Symfony2 application. For an introduction to using PHP on Heroku, please refer to Getting Started with PHP on Heroku.

Prerequisites

PHP (and ideally some Symfony2) knowledge.

A Heroku user account. Signup is free and instant.

Familiarity with the getting Started with PHP on Heroku guide, with PHP, Composer and the Heroku Toolbelt installed on your computer.

Creating a Symfony2 application

The application in this tutorial is based on the Symfony2 Quick Tour guide. It’s worth a read before (or while) following the instructions in this article.

Installing a Symfony Standard Edition project

To create a skeleton project following current best practices for Symfony2, use the composer create-project command; the one below sets it up in a directory named hello_symfony_heroku using Symfony version 2.4.x.

After downloading a long list of dependencies, the installer will prompt you to enter a few details regarding a database connection and mailer transports. For now, you can simply hit enter at each of these prompts to accept the default values; you can always change them later.

After it’s done, Composer has set up a fully functional Symfony Standard Edition project in the directory you specified, so we can cd to it.

Initializing a Git repository

It’s now time to initialize a Git repository and commit the current state:

Creating a Heroku application

To create a new Heroku application that you can push to, use the CLI’s create command:

You are now ready to deploy the application, but there is no code yet. Follow the next section to add some content to your application.

Adding some Hello World code

The standard edition application contains a couple of demo pages, but they are configured so they’re only visible in a development environment, and you should delete them later anyway.

Instead, you’ll simply generate a new Bundle and Controller using the SensioGeneratorBundle, which provides interactive commands for generating code skeletons. It is enabled by default in a Symfony2 standard edition project, so you can get started right away with no need for any setup work.

Generating a bundle skeleton

The generate:bundle command can be used to generate a new bundle and register it with your app.

The command runs in interactive mode, so it’ll ask a lot of questions. As the namespace, we’ll enter Example/GreetingBundle, and most of the other prompts can simply be confirmed using their default values (you’ll only have to choose a configuration format, and “annotation” will work just fine).

It’s a good idea to now commit this state:

Greeting the world

The generated bundle already contains a DefaultController class with an indexAction method. It expects a parameter in the URL and hands that to the template; in the next step, you will change this so the controller responds to the request URI “/” and always passes “World” as the name (or thing) to greet.

Simply adjust the contents of DefaultController.php (in the directory src/Example/GreetingBundle/Controller/) to look like this:

Next, add and commit that change

You are now ready for your first deploy.

Deploying to Heroku

To deploy your application to Heroku, you must first create a Procfile, which tells Heroku what command to use to launch the web server with the correct settings. After you’ve done that, you can simply git push, and you’re done!

Creating a Procfile

By default, Heroku will launch an Apache web server together with PHP to serve applications.

However, two special circumstances apply to our Symfony application:

The document root is in the web/ directory (not in the root directory) of the application;

The Composer bin-dir, where vendor binaries (and thus Heroku’s own boot scripts) are placed, is bin/, and not the default vendor/bin.

Vendor binaries are usually installed to vendor/bin by Composer, but sometimes (e.g. when running a Symfony Standard Edition project!), the location will be different. If in doubt, you can always run composer config bin-dir to figure out the right location.

Pushing to Heroku

Next up, it’s finally time to deploy to Heroku:

And that’s it! If you now open your browser, either by manually pointing it to the URL heroku create gave you, or by using the Heroku Toolbelt, the application will respond:

Et voilà! You should be seeing “Hello World!” in your browser.

Cleanup

By default, the Symfony2 app will log into your application’s app/log/ directory, which isn’t ideal as Heroku uses an ephemeral file system. On Heroku, the best way to handle logging is using Logplex, and the best way to send log data to Logplex is by writing to STDERR or STDOUT. Luckily Symfony2 uses the excellent Monolog library for logging, and so a new log destination is just a config file change away.

Changing the log destination for production

All that’s required to have Symfony log to STDERR is changing app/config/config_prod.yml. Locate the monolog/handlers/nested section in this file and change the value of path to php://stderr. Here is the git diff for your reference:

You can now add, commit and push as usual:

Next, run heroku logs --tail to keep the stream of logs from Heroku open in your terminal. Switch back to your browser and navigate to your Heroku application. As you refresh the page, you’ll see the access logs on your terminal.

Now, manually change the URL to /foobar. A brief moment later, the internal error logged by Symfony will appear on your terminal:

Press Ctrl+C on your keyboard again to leave the heroku logs command.

Further reading

Check out the PHP on Heroku Reference to learn about available versions, extensions, features and behaviors.

Review the instructions on customizing web server and runtime settings for PHP to learn more about configuring Apache, Nginx and PHP.

Browse the PHP category on Dev Center for more resources.

Use the PHP forums for discussions and questions!

Show more