Deploy your Symfony 5 app to Heroku with the addon ClearDB MySQL

Mallle
4 min readFeb 16, 2020

--

In this article, I’m sharing my experience with deploying my Symfony 5 app, with a MySQL (version 5.6) database to Heroku, using a MacBook Pro running macOSCatalina 10.15.3.

I have never used Heroku before (I have actually never deployed a Symfony application before). But with the help from the excellent documentation that Heroku provides and some other helpful articles (see the end of this article), I managed to get my application and database deployed more or less smoothly.

For this example, I assume you have a Symfony app up and running already (in my case, I had a Symfony 5 app running locally). I also assume you are using Git for version control and that you have Git installed globally on your system, as well as Composer.

So here is what I did — and how you can do it :)

First, you need to signup on Heroku.

After that, you need to install the Heroku Command Line Interface (CLI) — from now on, you more or less only need your terminal.

$ brew install heroku/brew/heroku

After installing you can login to Heroku with the following command:

$ heroku login

With this command, your web browser opens, and you either have to login or click the login button.

Then check if you have PHP, Composer and Git installed locally with the following commands (just to be sure):

$ php -v
$ composer -V
$ git --version

Then go into your project that you want to deploy.

$ cd path/to/your/project

Make sure your latest commits are pushed.

Now its time to create a new project on Heroku for your new application that you want to deploy. Run the following command:

$ heroku create [projectname]

(Some project names are already in use, so you might have to try a few times to find a free name. If you don’t add a project name, Heroku generates one randomly for you.)

Before you can deploy your application, you have to add a new file to the root of your project and commit the changes. You can do that with the following 3 commands:

$ echo 'web: heroku-php-apache2 public/' > Procfile
$ git add Procfile
$ git commit -m "Heroku Procfile"

The next step is to configure Symfony to run in the prod environment when you deploy. You can run the following command to set the variable `APP_ENV` to prod:

$ heroku config:set APP_ENV=prod

(Your environment variable may start with something else than `APP_`, check your .env file in your Symfony project).

Then add a new APP_SECRET key to Heroku’s config variables with the following command:

$ heroku config:set APP_SECRET=$(php -r 'echo bin2hex(random_bytes(16));')

To see all your variables set on the project on Heroku run the following command:

$ heroku config

(You should now have 2 variables here. )

Now you are ready to deploy your app to Heroku. We don’t have a database yet, but we are adding that afterward. Make sure you have pushed all your local changes to Git!
Run the following command to deploy for the first time:

$ git push heroku master

Then run the following command to open your newly deployed app

$ heroku open

I hope you made it this far — we are almost done — and your new app is up and running with a database too. :)

For the database, I use the addon ClearDB MySQL https://elements.heroku.com/addons/cleardb.
Still in your terminal in your local project folder, run the following command (at the time of writing this version of the database is free of costs, but you have to add your credit card information anyway).

$ heroku addons:add cleardb:ignite

The commando create a new database for you and adds a new variable to your Heroku configs you can grab it with the following command:

$ heroku config:get CLEARDB_DATABASE_URL

or run the command below to see that you now have 3 config variables:

$ heroku config

I had trouble to connect to the database at first — but here is the solution that worked for me:

Add a copy of the value from the variable CLEARDB_DATABASE_URL to a variable called DATABASE_URL ib the heroku config. I could not add the value of the variable in the command line, as I did with the APP_ENV variable. So in the dashboard on the Heroku-website (choose your newly added project, go to settings, Config Vars), I was able to manually add a new config variable. If you do it the same way, you should now have 4 config variables, two of them with the same value.

So after that, I was able to connect to the database, and I hope you are as well. Now run the following command to make your migrations:

$ heroku run php bin/console doctrine:migrations:migrate

That’s it — now fill that database with data — and have fun with your new app.

For my success, I highly relied on these articles — so thanks a lot to the authors of those.

https://devcenter.heroku.com/articles/deploying-symfony4

https://medium.com/@jontorrado/deploying-a-symfony-4-application-to-heroku-with-webpack-abdf7771cde9

https://coderwall.com/p/qpitzq/deploing-symfony-project-using-mysql-to-heroku

Thank you for reading — I hope it worked out successfully for you, too.

--

--

Mallle

I’m a webdeveloper working with Symfony, MySql and Vue.js