Last Updated: November 28, 2017
· mejt

Deploing Symfony Project using MySQL to Heroku.

On official Symfony page you can find tutorial how deploy Symfony project to Heroku (http://symfony.com/doc/current/cookbook/deployment/heroku.html). Similar document you can find on Heroku webpage (https://devcenter.heroku.com/articles/getting-started-with-symfony2).

These documents are good, but I couldn't find information how I can set up properly database connections.

Short guide how deploy Symfony project to Heroku

On beginning you need install few required tools (I guess that you have these things, but I want to remind about it).

  1. First thing which you need is Heroku Toolbelt (https://toolbelt.heroku.com).
  2. Next, you need Git (http://git-scm.com).

After install all required tools you can go to next steps.

Open console and go to your project:

$ cd ~/yourSymfonyProject

Initialize new Git repository and commit the current state of your code.

$ git init
Initialized empty Git repository in ~/yourProjectSymfony/.git/
$ git add .
$ git commit -m "First commit"
[master (root-commit) f432531] First commit 89 files changed, 7865 insertions(+)

Now create Procfile where you can set you web/ directory.

$ touch Procfile
$ echo "web: bin/heroku-php-apache2 web/" > Procfile
$ git add .
$ git commit -m "Procfile for Apache and PHP"
[master 45343as] Procfile for Apache and PHP 1 file changed, 1 insertion(+)

Next you should login to heroku toolbelt, so type:

$ heroku login

And use your credentials.

After login success you can create new Heroku application:

$ heroku create
Creating rusty-server-1211 in organization heroku... done, stack is xakar-15 http://rusty-server-1211.herokuapp.com/ | git@heroku.com:rusty-server-1211.git
Git remote heroku added

Success? Awesome! You are ready to deploy your application:

$ git push heroku master

If you had any problems related with SSH Fingerprints then this article should be helpful: https://devcenter.heroku.com/articles/git-repository-ssh-fingerprints.

If you didn't see any errors then your project was deployed successfully.

This was short guide about deploy Symfony project to Heroku, if you need more information then you can see documents on start of this protip.

My problem

In my Symfony project I use MySQL and Doctrine ORM, after deploying process I saw blank screen and on logs I found errors related with database connection. It was logic, I didn't set up any database...

My solution

Go to your project in console and add new Heroku addon: ClearDB (MySQL Database). Free version have a 5MB storage (more info: https://addons.heroku.com/cleardb).

Add ClearDB addon to your Heroku App

Type in console:

$ cd ~/yourSymfonyProject
$ heroku addons:add cleardb:ignite

Check that environment variable for database was set:

$ heroku config:get CLEARDB_DATABASE_URL

Set config files in your project

You added database and next step is configure you config files.


If you use yaml files for configuration then open ~/yourSymfonyProject/config/config.yml

In first lines you should see imports section:

   - { resource: parameters.yml }
   - { resource: security.yml }

From this section remove import of parameters.yml (you can copy this line before delete). Result:

   - { resource: security.yml }


Open ~/yourSymfonyProject/config/config_dev.yml and find similar section (imports) in this file:

    - { resource: config.yml }

Add removed line from config.yml to the section before - { resource: config.yml } line. Result:

    - { resource: parameters.yml }
    - { resource: config.yml }


Your developer environment is ready to use, but we need to set up production environment.

Create new PHP file for parameters in config directory. I created file named parameters_production.php, full path: ~/yourSymfonyProject/config/parameters_production.php

    $db = parse_url(getenv('CLEARDB_DATABASE_URL'));

    $container->setParameter('database_driver', 'pdo_mysql');
    $container->setParameter('database_host', $db['host']);
    $container->setParameter('database_port', $db['port']);
    $container->setParameter('database_name', substr($db["path"], 1));
    $container->setParameter('database_user', $db['user']);
    $container->setParameter('database_password', $db['pass']);
    $container->setParameter('secret', getenv('SECRET'));
    $container->setParameter('locale', 'en');
    $container->setParameter('mailer_transport', null);
    $container->setParameter('mailer_host', null);
    $container->setParameter('mailer_user', null);
    $container->setParameter('mailer_password', null);

All lines above are required to start application, and I think all is clear.

Function getenv('CLEARDB_DATABASE_URL') get a environment variable from your server (we checked that this variable is exists above in Add ClearDB addon to your Heroku App section).

Second interesting line is $container->setParameter('secret', getenv('SECRET')); here you can see that you need set a new environment variable in server.
Go to console and type:

$ heroku config:set SECRET=your_super_token
Setting config vars and restarting rusty-server-1211... done, v19
SECRET: your_super_token

APPEND file with parameters

When you have new parameters file then you can append this to config.

Open ~/yourSymfonyProject/config/config_prod.yml and add your new file to import section in first position.

    - { resource: parameters_production.php }
    - { resource: config.yml }


I guess that all is fine, so you can deploy your changes.

Go to console, and commit changes.

$ git add .
$ git commit -m "Updated production config"
[master 46462ab] Updated production config 4 files changed, 24 insertions (+++-)


$ git push heroku master

Now probably your database is clear, so you'll create database and migrate your schema.

$ heroku run php app/console doctrine:database:create
$ heroku run php app/console doctrine:schema:update --force

If all things is well done then you see install required libraries and other dependencies.
After deploying process check results of your work. Type in console:

heroku open

I guess that you see your website on live, well done!

Any questions or problems? Write a comment.

Say Thanks

8 Responses
Add your response


Sorry I just had to run:
"heroku run php app/console doctrine:schema:create"

over 1 year ago ·

Hi, I guess that I forgot about create database and migration.
Try run commands below:

heroku run php app/console doctrine:database:create

heroku run php app/console doctrine:schema:update --force

over 1 year ago ·

Thank you, very usefull!

over 1 year ago ·

Thank you a lot!! Very Usefull!

6 months ago ·

Hi all, when I try to replicate my local database from Symfony code to Heroku, I receive an :

"heroku run php app/console doctrine:database:create"
Running php app/console doctrine:database:create on young-sands-32192... up, run.9721 (Free)
Could not open input file: app/console

The same when executing schame:create, any thoughts? Thanks!

4 months ago ·

Awm Notes

28 days ago ·

Great work here! But I just wanted to add that for some when updating or creating your doctrine entity the code that you'll need to use is as follows:

$ heroku run php bin/console doctrine:database:create
$ heroku run php bin/console doctrine:schema:update --force
7 days ago ·
Up43961310009 de0dbceda3cc8114bf93 l

Instead of Heroku, I would recommend using Cloudways platform to host and deploy Symfony on cloud (https://www.cloudways.com/en/symfony-hosting.php ). This platform has a great UI that makes the whole process lot easier. You can instantly launch a cloud server using this platform, even if you don't have any technical experience.

7 days ago ·