We like Elixir & Phoenix, and we like Heroku. So one day we thought: why not combine them? In today’s post we talk about how to deploy a Phoenix app to Heroku.
Phoenix is framework for the Elixir language, used to build fast, scalable web applications. Because Elixir runs on the Erlang Virtual Machine, it’s able to fully use all CPU cores without sacrificing code readability, robustness and security. It proves its values in low-latency, distributed and fault-tolerant systems. How many times have you heard: “Your call cannot be made at the moment because we are upgrading our servers”? Never? That’s because Erlang was created by Ericsson for handling these needs, within the context of telecommunication systems.
Then, on the other side, you have Heroku: a powerful platform for hosting applications written in multiple languages; Ruby, Python, Node.js, and Go, among others. It makes deploying, scaling and hosting a breeze.
Unfortunately Heroku by default does not support apps written in Elixir out of the box at the moment of writing. Luckily there are many 3rd party buildpacks that are compatible with Heroku’s official ones and can be used as a replacement or addon. In this article we will show you how to deploy Phoenix application using those extra buildpacks.
We assume you already have a Heroku account and a Phoenix application created.
Create an application on Heroku
In your application folder, create a Heroku app:
heroku create your-application-name --buildpack "https://github.com/HashNuke/heroku-buildpack-elixir.git"
heroku buildpacks:add https://github.com/gjaldon/heroku-buildpack-phoenix-static.git --app your-application-name
# config/prod.exs
config :heroku_phoenix, HerokuPhoenix.Endpoint,
http: [port: {:system, "PORT"}],
url: [scheme: "https", host: "your-application-name.herokuapp.com", port: 443],
force_ssl: [rewrite_on: [:x_forwarded_proto]],
cache_static_manifest: "priv/static/manifest.json",
secret_key_base: System.get_env("SECRET_KEY_BASE")
# Configure your database
config :heroku_phoenix, HerokuPhoenix.Repo,
adapter: Ecto.Adapters.Postgres,
url: System.get_env("DATABASE_URL"),
pool_size: String.to_integer(System.get_env("POOL_SIZE") || "10"),
ssl: true
# web/channels/user_socket.ex
defmodule HerokuPhoenix.UserSocket do
use Phoenix.Socket
# ...
transport :websocket, Phoenix.Transports.WebSocket,
timeout: 45_000
end
web: MIX_ENV=prod mix phoenix.server
heroku addons:create heroku-postgresql:hobby-dev
# hobby-dev package allows for 20 open connections - we want 18 for our app server dyno so remaining 2 can be used by migrations or iex console:
heroku config:set POOL_SIZE=18
heroku config:set SECRET_KEY_BASE=$(mix phoenix.gen.secret)
git push heroku master:different_branch
heroku run "POOL_SIZE=2 mix ecto.migrate"
heroku logs (use --tail if you want to tail them) - check logs
heroku run "POOL_SIZE=2 iex -S mix" - enter to the console
always_rebuild=true
We know all sorts of hints and tricks for configuring your software systems - and when we don’t have the answer we sure do love a challenge! If you would like to run your web application more effectively then make sure to get in contact with us at iRonin. We help business and software companies in the USA and around the world build faster, smoother, and more reliable systems.