How to deploy Elixir/Phoenix [without Database] app to heroku

Yasser Hussain
4 min readApr 17, 2017

--

I recently built a small API app in Elixir/Phoenix.

Part of the challenge was deploying it to heroku.

So in this post I will attempt to go through steps of deploying a Phoenix test app on heroku.

Note:- This set up is for apps that do not require any database. If you want to deploy an app with database please check out the official phoenix docs. I will be writing about it soon.

STEP 1 (Set up dev environment)

This step assumes you have set up your heroku account. If you haven’t already, please do so here.

Install elixir. See here.

Install heroku toolbelt. See here.

STEP 2 (Create your Phoenix app)

If you installed elixir following Step 1, you would have got elixir’s build/package management tool too “mix”. We will use it to create our application.

Open your terminal and run the following command.

$ mix phoenix.new test_app --no-ecto

Notes:-

  • test_app is just the name I chose. You could choose any other name.
  • Phoenix allows underscore characters in app names but not hyphens.
  • — no-ecto tells phoenix we don’t need a database.

If you see a question about installing dependencies, press “y” then enter.

Then cd into the directory just created.

$ cd test_app

STEP 3 (Commit your app)

We will use git to commit our app. Run the following commands in sequence.

$ git init
$ git add --all
$ git commit -m "First Commit"

STEP 4 (Create a new heroku app)

We will use heroku toolbelt we installed in Step 1 for this step. Run command —

$ heroku create my-elixir-test-app

Notes:-

  • Heroku app names are unique. So you should replace the app name above “my-elixir-test-app” with a name of your choice, or heroku will complain that the name has already been taken.
  • If this is the first time you are creating a heroku app. You may be asked to sign in. Just enter your heroku credentials.

Once you have created the heroku app. Add elixir and phoenix buildpacks.

Buildpacks are a way of telling heroku what framework and language we are using in the app, so that heroku can compile/build our apps properly.

Run the following commands.

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

STEP 5 (Make your app production ready)

In this step we are going to create a new file Procfile and modify two -existing ones -

  • config/prod.exs
  • web/channels/user_socket.ex

Notes:-

  • For detailed info about how to do this, check this out.
  • If you wan to see the exact changes required to achieve this, see my commit.

Create a Procfile with the following contents.

web: MIX_ENV=prod mix phoenix.server

One of the ways for creating such a file is to run this command

$ echo "web: MIX_ENV=prod mix phoenix.server" > Procfile

Procfile contains the command heroku will run to start up your app. For more info see heroku docs.

Now as I said above we need to modify the following two files

  • config/prod.exs
  • web/channels/user_socket.ex

In config/prod.exs -

  1. Replace line -

cache_static_manifest: "priv/static/manifest.json"

with

cache_static_manifest: "priv/static/manifest.json",
secret_key_base: System.get_env("SECRET_KEY_BASE")

Here we’re saying that we want the secret_key_base value to be taken from the environment variable SECRET_KEY_BASE.

2. Replace line -

url: [host: "example.com", port: 80],

with

url: [scheme: "https", host: "my-elixir-test-app.herokuapp.com", port: 443],
force_ssl: [rewrite_on: [:x_forwarded_proto]],

Note :- Replace the highlighted “my-elixir-test-app.herokuapp.com” with your full heroku app url.

Here we are simply setting up the https protocol.

3. Comment out line -

import_config “prod.secret.exs”

like so -

# import_config "prod.secret.exs"

The app secret will be taken from the environment variable so no need to load it from another file.

In web/channels/user_socket.ex,

Replace line -

transport :websocket, Phoenix.Transports.WebSocket

with

transport :websocket, Phoenix.Transports.WebSocket,
timeout: 45_000

Here we are limiting the idle time of our app.

This was certainly the hardest step. If you have successfully completed it next steps should be easy.

STEP 6 (Commit your production config changes)

Run following command —

$ git commit -am "Added heroku deploy configs"

STEP 7 (Set up heroku environment variables)

Create an app secret. Run

$ mix phoenix.gen.secret

You might see a bunch of output. If you do, that’s because mix is building your project.

We are interested in the last line of output. You will see a long string something like this -

aoE/12HHjsaduayasdlkkalsduweo732892j+asdSASuaios

It is a random and unique string and we will set it up as our app’s secret.

Like so -

$ heroku config:set SECRET_KEY_BASE="aoE/12HHjsaduayasdlkkalsduweo732892j+asdSASuaios"

Note:- Please use the string generated in your machine instead of “aoE/12HHjsaduayasdlkkalsduweo732892j+asdSASuaios”

STEP 7 (Last Step, Push your code to heroku)

Push your code to heroku by running —

$ git push -u heroku master

As soon as you push your code to heroku, heroku automatically starts building and deploying the project.

If successful you will see a message like this

remote: -----> Launching...
remote: Released v4
remote: https://my-elixir-test-app.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/my-elixir-test-app.git

Instead of the highlighted url, you will see url relating to your app.

Just head over there and see whether your app was deployed.

You should see something like this —

Phoenix App deployed. Hurray!!

If you don’t see that. Please a leave a comment below so I might help you.

Thanks.

--

--