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.

Heroku は、人気があり、高速でスケーラブルで信頼性の高い Platform As A Service (PaaS) です。 Heroku は、Heroku Postgres によるデータベース サポートを含む無料プランを提供しているため、API プラットフォームを試すのに便利な方法です。

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

API Platform Heroku 統合は、ClearDB アドオンによって提供される MySQL データベースもサポートします。

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

API プラットフォーム アプリケーションを Heroku にデプロイするのは簡単で、このチュートリアルでその方法を学びます。

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

注: このチュートリアルは、API プラットフォームで完全に機能しますが、Symfony StandardEdition に基づく Symfony アプリケーションでも機能します。

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.

まだお持ちでない場合は、Heroku でアカウントを作成してください。次に、Herokutoolbelt をインストールします。すでに Composer がインストールされていると思います。完璧です。必要になります。

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

この例の残りの部分で使用する新しい API プラットフォーム プロジェクトを作成します。

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.

Heroku は、その構成を環境変数に依存しています。アプリケーションをホストするために選択したプロバイダーに関係なく、環境変数を使用して本番環境を構成することは、API プラットフォームによって推奨されるベスト プラクティスです。

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

api/ ディレクトリのルートに Heroku app.json ファイルを作成して、デプロイを構成します。

{
  "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.

このファイルは、Heroku デプロイメント システムに、PHP コンテナーを構築し、Postgres アドオンを追加するようにも指示します。

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.

これでほぼ完了ですが、API プラットフォーム (および Symfony) には特定のディレクトリ構造があり、さらに構成を行う必要があります。Heroku に、ドキュメント ルートが public/ であり、他のすべてのディレクトリが非公開でなければならないことを伝える必要があります。

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

Procfile という名前の新しいファイルを api/ ディレクトリに次の内容で作成します。

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

Be sure to add the Apache Pack to your dependencies:

必ず依存関係に Apache Pack を追加してください。

composer require symfony/apache-pack

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

Heroku はそのままでは Varnish をサポートしていないため、その統合を無効にしましょう。

# 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.

Heroku は、アプリケーション ログを一元化して保持できるようにする、別の無料サービス Logplex を提供します。 API Platform は STDERR にログを書き込むため、シームレスに動作します。

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.

ただし、デフォルトのロガーの代わりに Monolog を使用する場合は、ファイルではなく STDERR に出力するように構成する必要があります。

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

api/config/packages/prod/monolog.yaml を開き、次のパッチを適用します。

     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

api/ ディレクトリに移動し、

  1. Initialize a git repository:

    git リポジトリを初期化します。

    git init

    git初期化

  2. Add all existing files:

    既存のすべてのファイルを追加します。

    git add --all

    git add --all

  3. Commit:

    専念:

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

    git commit -a -m "Heroku で実行する初めての API プラットフォーム アプリ!"

  4. Create the Heroku application:

    Heroku アプリケーションを作成します。

    heroku create

    ヒロク作成

  5. And deploy for the first time:

    そして、初めてデプロイします。

    git push heroku master

    git push heroku マスター

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.

終わったね。 API Platform で提供されるデモ API で遊ぶことができます。本番の準備ができており、Heroku インターフェイスからワンクリックでスケーリングできます。

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

ログを表示するには、heroku logs --tail を実行します。