Deploy a Phoenix App with Distillery
This is a guide to deploying Phoenix projects on Render using Distillery.
We'll start off with a bare Phoenix project with Ecto, modify it to use Distillery, and deploy it on Render. The full source code for this tutorial is available at https://github.com/render-examples/phoenix-distillery.
Create a Phoenix App with Distillery
-
Create a new Phoenix app in the terminal:
-
Update
mix.exs
to add Distillery to deps:Then run
mix deps.get
in your terminal to update dependencies.
Configure Distillery
-
Let's configure Distillery for production. Update the
Endpoint
config inconfig/prod.exs
so it looks like this:Read more about Distillery configuration.
We're not going to check secrets into source control. Instead, we're going to manage them in production with Render environment variables. So let's delete the line at the end referring to
prod.secret.exs
.And delete the file
config/prod.secret.exs
. -
You're now ready to initialize your Distillery release.
This will create
rel/config.exs
,rel/vm.args
, and the empty directoryrel/plugins
.rel/vm.args
is used for runtime configuration by default, but we're going to use the Mix Config Provider instead so you can delete this file.
Creating a Mix Config Provider
-
Create a Mix configuration file at
rel/config/config.exs
:Contents of
rel/config/config.exs
:This sets up the Mix configuration provider to get values from runtime environment variables.
-
Update
rel/config.exs
to use your new provider. Change theenvironment :prod
section in the file to this:
Configuring Ecto
Let's configure Ecto to get the DATABASE_URL
from the environment. Change lib/phoenix_distillery/repo.ex
so it looks like this:
Build a Release with Distillery
You're now ready to build and run your first release!
The output should look like this:
You can now test your release (assuming PostgreSQL is up on your local machine):
You can now deploy your app in production! 🎉
Deploying to Render
-
Create a build script called
build.sh
at the root of your repo:Make the script executable before checking it into Git:
-
Create a new PostgreSQL database on Render.
-
Create a new Web Service on Render, and give Render permission to access your new repo.
-
Use the following values during creation:
Language Elixir
Build Command ./build.sh
Start Command ./_render/bin/phoenix_distillery foreground
Also add these environment variables to the web service:
Key Value SECRET_KEY_BASE
A sufficiently strong secret. You can generate a secret locally by running mix phx.gen.secret
DATABASE_URL
The internal database URL of the database you created above. That's it! Your web service will be live on your Render URL as soon as the build finishes.
Going forward, every push to your repo will automatically build your app and deploy it in production. If the build fails, Render will automatically stop the deploy process and the existing version of your app will keep running until the next successful deploy.
Read about customizing Elixir and Erlang versions for your app.