Codementor Events

Beyond development : deploying a Symfony 4 application to Heroku

Published May 23, 2018Last updated Nov 19, 2018
Beyond development : deploying a Symfony 4 application to Heroku

I have used quite a multitude of Cloud Platforms such Google Cloud, OpenShift, and Microsoft Azure, but I actually found Heroku much more fun to work with, especially for it’s simplicity and ease of customization.

There are actually two main articles dealing with the subject of deploying a Symfony/Php App on Heroku, but since I found some differences, I have decided to write an article specific to the 4th version of Symfony.

So the first thing to do is creating a Heroku application, in your project root run :

heroku create

As of Symfony 4, Environment variables are used instead of param files, let’s give it a shot by setting the APP_ENV (Previously SYMFONY_ENV):

heroku config:set APP_ENV=prod

Heroku uses a file called Procfile, to describe processes and workers used by your app, so to run my Symfony4 app, with a document root set to “public”, I have used the following Procfile definition:

web: $(composer config bin-dir)/heroku-php-apache2 public/

This defines a web process, runing the heroku-php-apache2 stack, in the public directory, where the index.php is located. An Nginx package is also available, read more at Selecting a web server.

Deploying is then, as simple as pushing to the master branch

git push heroku master

I doubt that an App can be useful without a database access, and thus, I have also added teh necessary settings for such purpose. You can add databases to your heroku app as “add-ons”. In my case I have used MySQL, through the JawsDB add-on, you can chose any other add-on for any RDMS you prefer.

in the command line, run the following command :

heroku addons:create jawsdb

Getting the config string is easy, type the following command :

heroku config:get JAWSDB_URL

This command will output the mysql connection string, now it’s time to let your Symfony App know about it, you either use the default DATABASE_URL, or customize it in the /app/config/packages/doctrine.yaml

# /config/packages/doctrine.yaml doctrine: dbal: # configure these for your database server driver: 'pdo_mysql' server_version: '5.7' charset: utf8mb4 # With Symfony 3.3, remove the `resolve:` prefix url: '%env(resolve:DATABASE_URL)%'

copy the config string and set it up for your Heroku App :

heroku config:set DATABASE_URL=mysql://USER:PASSWORD@HOST:PORT/DATABASE_NAME

Now you will naturally think about running database migrations or other console commands, this can easily be achieved by running :

heroku run "php bin/console cache:clear" // or heroku run "php bin/console doctrine:migrations:migrate"

One last, extra tip: if you wish to add extra PHP extensions to the setup, all you need to do is define them in the “require” section of your composer.son:

{ "require": { "ext-bcmath": "*", "ext-mcrypt": "*", "ext-memcached": "*", "ext-mongodb": "^1.1.0" } }

As mentionned in the Heroku docs :

It is recommended that you use “*” as the version selector when specifying extensions that are bundled with PHP, as their version numbers can be highly inconsistent (they often report their version as “0”).

This was a quick introduction to deploying a Symfony 4 app to Heroku, happy coding.

Discover and read more posts from Achraf BS
get started
post commentsBe the first to share your opinion
Show more replies