Symfony
sponsored by SensioLabs
Menu
  • About
  • Documentation
  • Screencasts
  • Cloud
  • Certification
  • Community
  • Businesses
  • News
  • Download
  1. Home
  2. Documentation
  3. Cookbook
  4. Deployment
  5. Deploying to Heroku Cloud
  • Documentation
  • Book
  • Reference
  • Bundles
  • Cloud
Search by Algolia

Table of Contents

  • Setting up
    • Preparing your Application
  • Creating a new Application on Heroku
  • Deploying your Application on Heroku
    • Creating a Procfile
    • Pushing to Heroku

Deploying to Heroku Cloud

Edit this page

Warning: You are browsing the documentation for Symfony 2.4, which is no longer maintained.

Consider upgrading your projects to Symfony 6.2.

Deploying to Heroku Cloud

This step by step cookbook describes how to deploy a Symfony web application to the Heroku cloud platform. Its contents are based on the original article published by Heroku.

Setting up

To setup a new Heroku website, first signup with Heroku or sign in with your credentials. Then download and install the Heroku Toolbelt on your local computer.

You can also check out the getting Started with PHP on Heroku guide to gain more familiarity with the specifics of working with PHP applications on Heroku.

Preparing your Application

Deploying a Symfony application to Heroku doesn't require any change in its code, but it requires some minor tweaks to its configuration.

By default, the Symfony app will log into your application's app/log/ directory. This is not 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, Symfony uses the excellent Monolog library for logging. So, a new log destination is just a change to a config file away.

Open the app/config/config_prod.yml file, locate the monolog/handlers/nested section (or create it if it doesn't exist yet) and change the value of path from "%kernel.logs_dir%/%kernel.environment%.log" to "php://stderr":

1
2
3
4
5
6
7
8
# app/config/config_prod.yml
monolog:
    # ...
    handlers:
        # ...
        nested:
            # ...
            path: "php://stderr"

Once the application is deployed, run heroku logs --tail to keep the stream of logs from Heroku open in your terminal.

Creating a new Application on Heroku

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

1
2
3
4
5
$ heroku create

Creating mighty-hamlet-1981 in organization heroku... done, stack is cedar
http://mighty-hamlet-1981.herokuapp.com/ | git@heroku.com:mighty-hamlet-1981.git
Git remote heroku added

You are now ready to deploy the application as explained in the next section.

Deploying your Application on 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 Symfony applications:

  1. The document root is in the web/ directory and not in the root directory of the application;
  2. The Composer bin-dir, where vendor binaries (and thus Heroku's own boot scripts) are placed, is bin/ , and not the default vendor/bin.

Note

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.

Create a new file called Procfile (without any extension) at the root directory of the application and add just the following content:

1
web: bin/heroku-php-apache2 web/

If you prefer working on the command console, execute the following commands to create the Procfile file and to add it to the repository:

1
2
3
4
5
$ echo "web: bin/heroku-php-apache2 web/" > Procfile
$ git add .
$ git commit -m "Procfile for Apache and PHP"
[master 35075db] Procfile for Apache and PHP
 1 file changed, 1 insertion(+)

Pushing to Heroku

Next up, it's finally time to deploy your application to Heroku. If you are doing this for the very first time, you may see a message such as the following:

1
2
3
The authenticity of host 'heroku.com (50.19.85.132)' can't be established.
RSA key fingerprint is 8b:48:5e:67:0e:c9:16:47:32:f2:87:0c:1f:c8:60:ad.
Are you sure you want to continue connecting (yes/no)?

In this case, you need to confirm by typing yes and hitting <Enter> key - ideally after you've verified that the RSA key fingerprint is correct.

Then, deploy your application executing this command:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
$ git push heroku master

Initializing repository, done.
Counting objects: 130, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (107/107), done.
Writing objects: 100% (130/130), 70.88 KiB | 0 bytes/s, done.
Total 130 (delta 17), reused 0 (delta 0)

-----> PHP app detected

-----> Setting up runtime environment...
       - PHP 5.5.12
       - Apache 2.4.9
       - Nginx 1.4.6

-----> Installing PHP extensions:
       - opcache (automatic; bundled, using 'ext-opcache.ini')

-----> Installing dependencies...
       Composer version 64ac32fca9e64eb38e50abfadc6eb6f2d0470039 2014-05-24 20:57:50
       Loading composer repositories with package information
       Installing dependencies from lock file
         - ...

       Generating optimized autoload files
       Creating the "app/config/parameters.yml" file
       Clearing the cache for the dev environment with debug true
       Installing assets using the hard copy option
       Installing assets for Symfony\Bundle\FrameworkBundle into web/bundles/framework
       Installing assets for Acme\DemoBundle into web/bundles/acmedemo
       Installing assets for Sensio\Bundle\DistributionBundle into web/bundles/sensiodistribution

-----> Building runtime environment...

-----> Discovering process types
       Procfile declares types -> web

-----> Compressing... done, 61.5MB

-----> Launching... done, v3
       http://mighty-hamlet-1981.herokuapp.com/ deployed to Heroku

To git@heroku.com:mighty-hamlet-1981.git
 * [new branch]      master -> master

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:

1
2
$ heroku open
Opening mighty-hamlet-1981... done

You should be seeing your Symfony application in your browser.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
We stand with Ukraine.
Version:
Check Code Performance in Dev, Test, Staging & Production

Check Code Performance in Dev, Test, Staging & Production

Peruse our complete Symfony & PHP solutions catalog for your web development needs.

Peruse our complete Symfony & PHP solutions catalog for your web development needs.

↓ Our footer now uses the colors of the Ukrainian flag because Symfony stands with the people of Ukraine.

Avatar of Antoine Lamirault, a Symfony contributor

Thanks Antoine Lamirault for being a Symfony contributor

33 commits • 945 lines changed

View all contributors that help us make Symfony

Become a Symfony contributor

Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

Learn how to contribute

Symfony™ is a trademark of Symfony SAS. All rights reserved.

  • What is Symfony?
    • Symfony at a Glance
    • Symfony Components
    • Case Studies
    • Symfony Releases
    • Security Policy
    • Logo & Screenshots
    • Trademark & Licenses
    • symfony1 Legacy
  • Learn Symfony
    • Symfony Docs
    • Symfony Book
    • Reference
    • Bundles
    • Best Practices
    • Training
    • eLearning Platform
    • Certification
  • Screencasts
    • Learn Symfony
    • Learn PHP
    • Learn JavaScript
    • Learn Drupal
    • Learn RESTful APIs
  • Community
    • SymfonyConnect
    • Support
    • How to be Involved
    • Code of Conduct
    • Events & Meetups
    • Projects using Symfony
    • Downloads Stats
    • Contributors
    • Backers
  • Blog
    • Events & Meetups
    • A week of symfony
    • Case studies
    • Cloud
    • Community
    • Conferences
    • Diversity
    • Documentation
    • Living on the edge
    • Releases
    • Security Advisories
    • SymfonyInsight
    • Twig
    • SensioLabs
  • Services
    • SensioLabs services
    • Train developers
    • Manage your project quality
    • Improve your project performance
    • Host Symfony projects
    Deployed on
Follow Symfony
Search by Algolia