chooblarin’s blog

Hello PhoenixをHerokuで

August 27, 2015

  • #Elixir

つい先日弊社では Elixir の社内勉強会を行いました.

今回はPhoenixを Heroku で動かしてみます.

以下のサイトを参考にさせてもらいました.

Phoenix をインストール

公式サイトに記載の通りです.

Hex をインストール

$ mix local.hex

Phoenix アーカイブをインストール

$ mix archive.install https://github.com/phoenixframework/phoenix/releases/download/v0.17.1/phoenix_new-0.17.1.ez

Hello Phoenix

これも公式サイト通り.

Phoenix プロジェクトを作成

$ mix phoenix.new hello_phoenix

起動

$ cd hello_phoenix
$ mix phoenix.server

確認 => http://localhost:4000

Heroku にデプロイ

Buildpack にHeroku Buildpack for ElixirPhoenix Static Buildpackの 2 つを使用します.

.gitignore ファイルを編集

.gitignoreconfig/prod.secret.exsをコメントアウトします.

...

# The config/prod.secret.exs file by default contains sensitive
# data and you should not commit it into version control.
#
# Alternatively, you may comment the line below and commit the
# secrets file as long as you replace its contents by environment
# variables.
#/config/prod.secret.exs

Heroku の環境編集を設定

config/prod.secret.exs.gitignoreの対象から外したので,重要な情報を heroku の環境変数に置き換えます.

use Mix.Config

# In this file, we keep production configuration that
# you likely want to automate and keep it away from
# your version control system.
config :hello_phoenix_heroku, HelloPhoenixHeroku.Endpoint,
  secret_key_base: System.get_env("SECRET_KEY_BASE")

# Configure your database
config :hello_phoenix_heroku, HelloPhoenixHeroku.Repo,
  adapter: Ecto.Adapters.Postgres,
  username: System.get_env("DATABASE_USERNAME"),
  password: System.get_env("DATABASE_PASSWORD"),
  database: "hello_phoenix_heroku_prod",
  size: 20 # The amount of database connections in the pool
$ heroku config:set SECRET_KEY_BASE=<YOUR_SECRET_KEY_BASE>
$ heroku config:set SECRET_KEY_BASE=<YOUR_DATABASE_USERNAME>
$ heroku config:set SECRET_KEY_BASE=<YOUR_DATABASE_PASSWORD>

必要な設定ファイルを作成

elixir_buildpack.configファイルとProcfileをプロジェクトのルートに作成します.

  • elixir_buildpack.config
# Erlang version
erlang_version=17.5

# Elixir version
elixir_version=1.0.4

# Always rebuild from scratch on every deploy?
always_rebuild=false

# Export heroku config vars
config_vars_to_export=(DATABASE_URL SECRET_KEY_BASE)

# A command to run right after compiling the app
post_compile="pwd"
  • Procfile
web: mix phoenix.server

そしたら git コミットします.

$ git init
$ git add .
$ git commit -m "Hello, Phoenix"

デプロイ

Heroku アプリを作成

$ heroku create --buildpack "https://github.com/HashNuke/heroku-buildpack-elixir.git"

以下の 2 つの Buildpack を設定

$ heroku buildpacks:set https://github.com/gjaldon/phoenix-static-buildpack
$ heroku buildpacks:add --index 1 https://github.com/HashNuke/heroku-buildpack-elixir

ぷっしゅ

$ git push heroku master

出来た!