Deploying an API Platform App on Heroku

Heroku is a popular, fast, scalable and reliable Platform As A Service (PaaS). As Heroku offers a free plan including database support through Heroku Postgres, it’s a convenient way to experiment with API Platform.

The API Platform Heroku integration also supports MySQL databases provided by the ClearDB add-on.

Deploying API Platform applications on Heroku is straightforward and you will learn how to do it in this tutorial.

Note: this tutorial works perfectly well with API Platform but also with any Symfony application based on the Symfony Standard Edition.

If you don’t already have one, create an account on Heroku. Then install the Heroku toolbelt. We’re guessing you already have a working install of Composer. Perfect, we will need it.

Create a new API Platform project which will be used in the rest of this example.

Heroku relies on environment variables for its configuration. Regardless of what provider you choose for hosting your application, using environment variables to configure your production environment is a best practice promoted by API Platform.

Create a Heroku app.json file at the root of the api/ directory to configure the deployment:

{
  "success_url": "/",
  "env": {
    "APP_ENV": "prod",
    "APP_SECRET": {"generator": "secret"},
    "CORS_ALLOW_ORIGIN": "https://your-client-url.com"
  },
  "addons": [
    "heroku-postgresql"
  ],
  "buildpacks": [
    {
      "url": "https://github.com/heroku/heroku-buildpack-php"
    }
  ],
  "scripts": {
    "postdeploy": "php bin/console doctrine:schema:create"
  }
}

The file also tells the Heroku deployment system to build a PHP container and to add the Postgres add-on.

We are almost done, but API Platform (and Symfony) has a particular directory structure which requires further configuration. We must tell Heroku that the document root is public/, and that all other directories must be private.

Create a new file named Procfile in the api/ directory with the following content:

web: vendor/bin/heroku-php-apache2 public/

Be sure to add the Apache Pack to your dependencies:

composer require symfony/apache-pack

As Heroku doesn’t support Varnish out of the box, let’s disable its integration:

# api/config/packages/api_platform.yaml
-    http_cache:
-        invalidation:
-            enabled: true
-            varnish_urls: ['%env(VARNISH_URL)%']
-        max_age: 0
-        shared_max_age: 3600
-        vary: ['Content-Type', 'Authorization', 'Origin']
-        public: true

Heroku provides another free service, Logplex, which allows us to centralize and persist application logs. Because API Platform writes logs on STDERR, it will work seamlessly.

However, if you use Monolog instead of the default logger, you’ll need to configure it to output to STDERR instead of in a file.

Open api/config/packages/prod/monolog.yaml and apply the following patch:

     handlers:
         nested:
             type: stream
-            path: "%kernel.logs_dir%/%kernel.environment%.log"
+            path: php://stderr
             level: debug

We are now ready to deploy our app!

Go to the api/ directory, then

  1. Initialize a git repository:

    git init

  2. Add all existing files:

    git add –all

  3. Commit:

    git commit -a -m “My first API Platform app running on Heroku!”

  4. Create the Heroku application:

    heroku create

  5. And deploy for the first time:

    git push heroku master

We’re done. You can play with the demo API provided with API Platform. It is ready for production and you can scale it in one click from the Heroku interface.

To see your logs, run heroku logs --tail.

You can also help us improve the documentation of this page.

Made with love by

Les-Tilleuls.coop can help you design and develop your APIs and web projects, and train your teams in API Platform, Symfony, Next.js, Kubernetes and a wide range of other technologies.

Learn more

Copyright © 2023 Kévin Dunglas

Sponsored by Les-Tilleuls.coop