Deploy a Phoenix App on Render
This guide walks through deploying a Phoenix application on Render.
The finished code for this example is available on GitHub.
Getting started
Create a new Phoenix app in the terminal. We don't need a database for this example, so pass the --no-ecto flag to mix:
Create a build script
We need to run a series of commands to build our app on every push to our Git repo, which we can accomplish with a build script. Create a script named build.sh at the root of your repo:
Make sure the script is executable before you commit it:
Update your app for Render
Update config/runtime.exs to change the highlighted line below:
to this:
Render populates RENDER_EXTERNAL_HOSTNAME for config/runtime.exs.
If you add a custom domain to your Render app, don't forget to change the host to your new domain.
Build and test your release locally
Compile your release locally by running ./build.sh. The end of the output should look like this:
Test your release by running the following command and navigating to http://localhost:4000.
You might need to run mix deps.get locally to enable phx.gen.secret, because it's a dev dependency instead of a prod dependency. You should add/commit your mix.lock file before running this.
You might notice the start command is different from the output of build.sh. The phx.gen.release command in the build script creates a wrapper for launching your application that include setting the PHX_SERVER variable, so a webserver will start. You can also use the start command from the build.sh output, but you will need to set PHX_SERVER to true in the environment variables if you do.
If everything looks good, push your changes to your repo. Next, let's deploy your app to Render!
Deploy to Render
-
In the Render Dashboard, create a new web service and connect your Phoenix app's repo.
-
Set the following values during service creation:
Setting Value Language ElixirBuild Command ./build.shStart Command _build/prod/rel/phoenix_hello/bin/server -
Add the following environment variable to your web service:
Key Value SECRET_KEY_BASEA sufficiently strong secret. You can generate one from your local project directory with the following command:
That's it! Your Phoenix web service will be live at its onrender.com subdomain as soon as the deploy finishes.
Read about setting Elixir and Erlang/OTP versions for your app.