The performance of Phoenix/Elixir is most apparent when using Heroku. Popular sites like Hex.pm run on a single dyno resulting in big cost savings.
Before we proceed, you'll need to register for a Heroku account and install the Heroku toolbelt. The toolbelt is a command line interface for Heroku that allows us to easily deploy our code, read logs, or make environment changes.
Heroku relies on buildpacks to configure a dyno for a specific language and/or framework. For our Phoenix application we'll add a buildpack that will install Erlang, Elixir, and our application dependencies.
In one fell swoop we can create our new Heroku application and add our buildpack, let's run the Heroku create command in our project directory:
$ heroku create --buildpack "https://github.com/HashNuke/heroku-buildpack-elixir.git"Creating enigmatic-ravine-4201... done, stack is cedar-14Buildpack set. Next release on enigmatic-ravine-4201 will use https://github.com/HashNuke/heroku-buildpack-elixir.git.https://enigmatic-ravine-4201.herokuapp.com/ | https://git.heroku.com/enigmatic-ravine-4201.gitGit remote heroku addedOr if you've already signed in and created a Heroku project, you can add it as a git remote and assign a buildpack.
$ heroku buildpacks:add "https://github.com/HashNuke/heroku-buildpack-elixir.git"It's probably worthwhile to look through the brief documentation for this particular buildpack in case you need to do some debugging.
Sometimes Heroku will not automatically add the git remote. You can test this by running the command git remote -v and you should see two for origin that go to Github and two for heroku, which go to Heroku. In the event that it does not, log in to Heroku.com, select the project, click Settings, and copy the Git URL from the Info section and run the command (with the proper URL):
$ git remote add heroku <git_url>The create command tells us it created enigmatic-ravine-4201, the name of our application within Heroku, set our buildpack, and finally added a remote to git. The outputted URL is the URL for our application, right now it only shows the Heroku welcome page.
Note: Our Phoenix application doesn't rely on static assets but if it did, we'd need to add > another buildpack to handle asset compliation:
$ heroku buildpacks:add https://github.com/gjaldon/heroku-buildpack-phoenix-static.gitWith the application created and buildpack set, we need to make some final changes to Phoenix before we can deploy to Heroku.
For the purposes of security, we don't want to store our secret keys in a file on Heroku. Instead, we'll use environment variables. Be sure to use the host: that relates to your Heroku project. Let's open config/prod.exs and add the secret_key_base key and retrieve the value from the system.
We're also setting check_origin to false because we want to allow all of our anonymous chat clients to be able to connect no matter what their domain.
config :phoenix_chat, PhoenixChat.Endpoint, http: [port: {:system, "PORT"}], check_origin: false, force_ssl: [rewrite_on: [:x_forwarded_proto]], secret_key_base: System.get_env("SECRET_KEY_BASE")Since we won't be storing our secret keys in config/prod.secret.exs, remove the following line from our config/prod.exs:
import_config "prod.secret.exs"Lastly, we need to update our Repo configure to rely on the "DATABASE_URL" environment variable Heroku sets:
config :phoenix_chat, PhoenixChat.Repo, adapter: Ecto.Adapters.Postgres, url: System.get_env("DATABASE_URL"), pool_size: 10, ssl: trueTo populate our "DATABASE_URL" environment variable, we need to use a Heroku add-on database. For our application the Heroku Postgres hobby tier will be sufficient (and free). Using the Heroku toolbelt, let's add it to our project:
$ heroku addons:create heroku-postgresql:hobby-devWe will also need to create an elixir_buildpack.config file that will tell Elixir about our environment variables in Heroku.
$ touch elixir_buildpack.configWithin that file, we need to add our DATABASE_URL that you can find when you log into Heroku. This one will actually be added for you automatically, but we will need to do this in the future with Mailgun and other environment variables, so it's best to get used to doing it now.
config_vars_to_export=( DATABASE_URL SECRET_KEY_BASE)Finally, we need to generate and set a new secret key. With mix, let's generate a new secret:
$ mix phoenix.gen.secretNrRwQpjBoUeKz1JnvWT+WrmF+hSnRPvkeDkHKwIgv5blKVeS0PUp9GA69S6VGXVfNow we can add our new secret key to Heroku:
$ heroku config:set SECRET_KEY_BASE="NrRwQpjBoUeKz1JnvWT+WrmF+hSnRPvkeDkHKwIgv5blKVeS0PUp9GA69S6VGXVf"If you intend to send emails, you'll also need to set up a Mailgun account and add those keys to your configuration as well. Otherwise, you should comment out the mail portion of your app otherwise it will cause an error on deployment.
It might also be a good idea to set always_rebuild to true since this will save you from some hard-to-debug errors later on.
config_vars_to_export=( DATABASE_URL SECRET_KEY_BASE MAILGUN_API_KEY MAILGUN_DOMAIN)always_rebuild=trueelixir_version=1.3.2$ heroku config:set MAILGUN_API_KEY=key-cec83902409402940958398a2cf3$ heroku config:set MAILGUN_DOMAIN=https://api.mailgun.net/v3/mg.domainname.ioYou're also going to need a Procfile that tells your server how to run.
$ echo "web: MIX_ENV=prod mix phoenix.server" > ProcfileWe've created a Heroku project, added our buildpack, configured our application, and setup the environment, all that's left is for us to commit our changes and deploy them to Heroku:
$ git add .$ git commit -am "chore: adjust configuration for deploy"Deploying to Heroku is as easy as pushing your code. Note that Heroku ignores all branches other than master, so if you want to push a different branch, you need to specify. So if you wanted to push the develop branch, you would have to run: git push heroku develop:master
$ git push heroku masterCounting objects: 3, done.Delta compression using up to 8 threads.Compressing objects: 100% (3/3), done.Writing objects: 100% (3/3), 288 bytes | 0 bytes/s, done.Total 3 (delta 2), reused 0 (delta 0)remote: Compressing source files... done..........remote: Verifying deploy.... done.To https://git.heroku.com/enigmatic-ravine-4201.git f7ac6d3..e08a63e test -> masterBefore we visit our site, let's run our migrations on Heroku. We need to set the mix environment to prod, as it defaults to dev.
$ heroku run MIX_ENV=prod mix ecto.migrateNow we're ready to see our app in action, typing heroku open in the console will open our browser to our app:
$ heroku openAnd now we've successfully deployed our Phoenix application to Heroku. Take note of the URL of your API because we will need it later.
Previous: Routing Messages Using API KeysNext: Deploying the Frontend to S3