Deploying PHP to Heroku

July 13, 2018

It's no huge secret that we're a rails shop. But every now and again, we like to help our pals out and sometimes that means getting down and dirty with languages we may not be as familiar with. This time our challenge was with PHP. We agreed to help someone out by hosting their PHP site. Since we host most all our sites on Heroku, we decided to see how getting Heroku w/ PHP up and running would go. We found their getting started guide to be mostly helpful, but it did leave a few things out. First and foremost, Heroku requires that you use Composer for dependency management (in the rails world this would be akin to a gem file). More on composer here. Problem #1 This particular site uses MySQL and since Heroku's preferred database is PostgreSQL, you have to update your composer file to include mysql extensions. Add this to your composer file: "ext-mysql": "*", This will install the appropriate MySQL extensions on your heroku instance. Problem #2 Heroku only supports "modern" versions of PHP -- meaning any version after PHP 5.5. So you need to add this to your composer file to specify the version you're going to use: "php": "~5.5.0", Problem #3 Heroku uses an ephemeral file system (that is, read-only or temprorary). PHP's default logging mechanism is to write to a file. Obviously this will not work. Thankfully, there's an easy workaround. We just need to rewire PHP to push logging to the STDOUT that heroku supports and not write it to file. Add this to your composer file: "silex/silex": "~1.1", "monolog/monolog": "~1.7", "twig/twig": "~1.0" In the end, our composer file looks like this: { "require": { "php": "~5.5.0", "ext-mysql": "*", "silex/silex": "~1.1", "monolog/monolog": "~1.7", "twig/twig": "~1.0" } }