If you want to know how to deploy a phoenix app on the Heroku platform through the Heroku cli, I suggest you to look at the official documentation on the Phoenix website.
Some of the steps come from the documentation referenced above.
This documentation will guide you through how to setup a Phoenix application on Heroku through the Heroku dashboard.
https://github.com/HashNuke/heroku-buildpack-elixir.git
https://github.com/gjaldon/heroku-buildpack-phoenix-static.git
Click the Deploy link in your app navigation and select Github as your deployment method.
You will have to authorize Heroku to access your git repositories.
You will be able to select the repository you want to connect and if you want to have an automatic build
when changes are pushed to master.
Depending on the need of your application to connect to a database and that Postgres is the default choice of Ecto, we can add a Postgres database by just installing the addon and connect it to your app. The only thing you need to do is to click the Install Postgres button.
Note that this installation will add the DATABASE_URL
environment variable in your application settings.
We will see more of that later on.
Now that the project has been set up in Heroku we still need to modify a few files in our Phoenix project to get it ready for Heroku.
Note: if you copy paste pieces of the code examples, make sure adjust the project names.
playwith_phoenix needs to be your project and PlaywithPhoenix needs to be changed accordingly.
First, let's make sure our secret key is loaded from Heroku's environment variables instead of
config/prod.secret.exs
by adding a secret_key_base
in config/prod.exs
:
config :playwith_phoenix, PlaywithPhoenix.Endpoint,
...
secret_key_base: System.get_env("SECRET_KEY_BASE")
...
Then we will add our database configuration to config/prod.exs
:
# Configure your database
config :playwith_phoenix, PlaywithPhoenix.Repo,
adapter: Ecto.Adapters.Postgres,
url: System.get_env("DATABASE_URL"),
pool_size: String.to_integer(System.get_env("POOL_SIZE") || "10"),
ssl: true
Then we will tell Phoenix to use our Heroku URL and enforce SSL:
config :playwith_phoenix, PlaywithPhoenix.Endpoint,
...
url: [scheme: "https", host: "your_heroku_app_name.herokuapp.com", port: 443],
force_ssl: [rewrite_on: [:x_forwarded_proto]],
...
Since we use Heroku's environment variables we do not need to import config/prod.secret.exs
anymore in config/prod.exs
,
so we can just remove the following line:
import_config "prod.secret.exs"
Our config/prod.exs
file should now look like this:
use Mix.Config
# For production, we configure the host to read the PORT
# from the system environment. Therefore, you will need
# to set PORT=80 before running your server.
#
# You should also configure the url host to something
# meaningful, we use this information when generating URLs.
#
# Finally, we also include the path to a manifest
# containing the digested version of static files. This
# manifest is generated by the mix phoenix.digest task
# which you typically run after static files are built.
config :playwith_phoenix, PlaywithPhoenix.Endpoint,
http: [port: {:system, "PORT"}],
url: [scheme: "https", host: "your_heroku_app_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")
# Do not print debug messages in production
config :logger, level: :info
# Configure your database
config :playwith_phoenix, PlaywithPhoenix.Repo,
adapter: Ecto.Adapters.Postgres,
url: System.get_env("DATABASE_URL"),
pool_size: String.to_integer(System.get_env("POOL_SIZE") || "10"),
ssl: true
The last file we need to modify is config/config.exs
. This file will have your secret_key_base
.
Take note (copy) of this key because we will added it to our Heroku environment variables in a bit, and replace it with System.get_env("SECRET_KEY_BASE")
.
It should look like this:
# Configures the endpoint
config :playwith_phoenix, PlaywithPhoenix.Endpoint,
url: [host: "localhost"],
secret_key_base: System.get_env("SECRET_KEY_BASE"),
render_errors: [view: PlaywithPhoenix.ErrorView, accepts: ~w(html json)],
pubsub: [name: PlaywithPhoenix.PubSub,
adapter: Phoenix.PubSub.PG2]
So the last thing that we need to do is add our SECRET_KEY_BASE
enviroment variable and our database POOL_SIZE
.
Go to your Heroku application dashboard and go to settings.
There you can reveal your config vars and add them.
Note that the DATABASE_URL
has been set by the postgres installation!
When you merge a PR or push to master Heroku will build and deploy your phoenix app.