{"metadata":{"image":[],"title":"","description":""},"api":{"url":"","auth":"required","params":[],"results":{"codes":[]},"settings":""},"next":{"description":"","pages":[]},"title":"Heroku","type":"basic","slug":"heroku","excerpt":"","body":"### What we'll need\n\nThe only thing we'll need for this guide is a working Phoenix application.\n\nIf you don't already have one, follow the [Up and Running guide](http://www.phoenixframework.org/docs/up-and-running) to create your first Phoenix application.\n\n### Goals\n\nOur main goal for this is guide is to get a Phoenix application running on Heroku.\n\n## Steps\n\nLet's separate this process into a few steps so we can keep track of where we are.\n\n- Initialize Git repository\n- Sign up for Heroku\n- Install the Heroku Toolbelt\n- Create the Heroku application\n- Add the Phoenix static buildpack\n- Make our project Heroku-ready\n- Deploy time!\n- Useful Heroku commands\n\n## Initializing Git repository\n\n[Git](https://git-scm.com/) is a popular decentralized revision control system and is also used to deploy apps to Heroku.\n\nBefore we can push to Heroku we'll need to initialize a local Git repository and commit our files to it. We can do so by running the following commands in our project directory:\n\n```console\n$ git init\n$ git add .\n$ git commit -m \"Initial commit\"\n```\n\nYou can learn more about how Heroku is using Git [here](https://devcenter.heroku.com/articles/git#tracking-your-app-in-git).\n\n## Signing up for Heroku\n\nSigning up to Heroku is very simple, just head over to [https://signup.heroku.com/www-header](https://signup.heroku.com/www-header) and fill in the form.\n\nThe Free plan will give us one web [dyno](https://devcenter.heroku.com/articles/dynos#dynos) and one worker dyno, as well as a PostgreSQL and Redis instance for free.\n\nThese are meant to be used for testing and development, and come with some limitations. If you plan to run some serious business on Heroku you should definitely considering upgrading to a paid plan.\n\n## Installing the Heroku Toolbelt\n\nOnce we have signed up, we can download the correct version of the Heroku Toolbelt for our system [here](https://toolbelt.heroku.com/).\n\nThe Heroku CLI, part of the Toolbelt, is useful to create Heroku applications, list currently running dynos for an existing application, tail logs or run one-off commands (mix tasks for instance).\n\n## Creating the Heroku Application\n\nNow that we have the Toolbelt installed, let's create the Heroku application. In our project directory, run:\n\n> Note: the first time you use a Heroku command you need to log in. When prompted, just enter the email and password you gave during signup.\n\n```console\n$ heroku create --buildpack \"https://github.com/HashNuke/heroku-buildpack-elixir.git\"\nCreating mysterious-meadow-6277... done, stack is cedar-14\nBuildpack set. Next release on mysterious-meadow-6277 will use https://github.com/HashNuke/heroku-buildpack-elixir.git.\nhttps://mysterious-meadow-6277.herokuapp.com/ | https://git.heroku.com/mysterious-meadow-6277.git\nGit remote heroku added\n```\n\n> Note: the name of the Heroku application is the random string after \"Creating\" in the output above (mysterious-meadow-6277). It will be different when you create the application on your computer.\n\nThe `--buildpack` option we are passing allows us to specify the [Elixir buildpack](https://github.com/HashNuke/heroku-buildpack-elixir) we want Heroku to use.\nA [buildpack](https://devcenter.heroku.com/articles/buildpacks) is a convenient way of packaging framework and/or runtime support. In our case it's installing Erlang, Elixir, fetching our application dependencies, and so on, before we run it.\n\nThe URL in the output is the URL to our application. If we open it in our browser now, we will get the default Heroku welcome page.\n\n> Note: if you forgot to initialize your Git repository before running the `heroku create` command you can add the Git remote manually by running the following command: `heroku git:remote -a [your app name].`\n\n## Adding the Phoenix Static Buildpack\n\nWe need to compile static assets for a successful Phoenix deployment. The [Phoenix static buildpack](https://github.com/gjaldon/heroku-buildpack-phoenix-static) can take care of that for us, so let's add it now.\n\n```console\n$ heroku buildpacks:add https://github.com/gjaldon/heroku-buildpack-phoenix-static.git\nBuildpack added. Next release on mysterious-meadow-6277 will use:\n  1. https://github.com/HashNuke/heroku-buildpack-elixir.git\n  2. https://github.com/gjaldon/heroku-buildpack-phoenix-static.git\nRun `git push heroku master` to create a new release using these buildpacks.\n```\n\n## Making our Project Heroku-ready\n\nEvery new Phoenix project ships with a config file `config/prod.secret.exs` which stores configuration that should not be commited along with our source code. By default Phoenix adds it to our `.gitignore` file.\n\nThis works great except Heroku uses [environment variables](https://devcenter.heroku.com/articles/config-vars) to pass sensitive informations to our application. It means we need to make some changes to our config before we can deploy.\n\nFirst, let's copy over the content of `config/prod.secret.exs` to `config/prod.exs`.\n\nNow we need to load the necessary environment variables in our configuration.\n\nIn our case:\n\n```elixir\nconfig :hello_phoenix, HelloPhoenix.Endpoint,\n  secret_key_base: \"my_secret_key\"\n```\n\nbecomes:\n\n```elixir\nconfig :hello_phoenix, HelloPhoenix.Endpoint,\n  secret_key_base: System.get_env(\"SECRET_KEY_BASE\")\n```\n\nand:\n\n```elixir\nconfig :hello_phoenix, HelloPhoenix.Repo,\n  adapter: Ecto.Adapters.Postgres,\n  username: \"postgres\",\n  password: \"postgres\",\n  database: \"hello_phoenix_prod\",\n  size: 20 # The amount of database connections in the pool\n```\n\nbecomes:\n\n```elixir\nconfig :hello_phoenix, HelloPhoenix.Repo,\n  adapter: Ecto.Adapters.Postgres,\n  url: System.get_env(\"DATABASE_URL\"),\n  size: 20 # The amount of database connections in the pool\n```\n\nAlso we don't need to import the `config/prod.secret.exs` file in our prod config so you can delete the following line:\n\n```elixir\nimport_config \"prod.secret.exs\"\n```\n\nYour final `config/prod.exs` should now look something like this (I've removed the comments for readability):\n\n```\nuse Mix.Config\n\nconfig :hello_phoenix, HelloPhoenix.Endpoint,\n  http: [port: System.get_env(\"PORT\")],\n  url: [host: System.get_env(\"HOST\"), port: 80],\n  cache_static_manifest: \"priv/static/manifest.json\"\n\nconfig :logger, level: :info\n\nconfig :hello_phoenix, HelloPhoenix.Endpoint,\n  secret_key_base: System.get_env(\"SECRET_KEY_BASE\")\n\nconfig :hello_phoenix, HelloPhoenix.Repo,\n  adapter: Ecto.Adapters.Postgres,\n  url: System.get_env(\"DATABASE_URL\"),\n  size: 20 # The amount of database connections in the pool\n```\n\nWe can now remove the `config/prod.secret.exs` from the disk:\n\n```console\n$ rm config/prod.secret.exs\n```\n\nAnd from our `.gitignore` file:\n\n```\n# Mix artifacts\n/_build\n/deps\n/*.ez\n\n# Generate on crash by the VM\nerl_crash.dump\n\n# Static artifacts\n/node_modules\n\n# Since we are building assets from web/static,\n# we ignore priv/static. You may want to comment\n# this depending on your deployment strategy.\n/priv/static/\n\n### DELETE ALL THE LINES BELOW ###\n\n# The config/prod.secret.exs file by default contains sensitive\n# data and you should not commit it into version control.\n#\n# Alternatively, you may comment the line below and commit the\n# secrets file as long as you replace its contents by environment\n# variables.\n/config/prod.secret.exs\n```\n\n## Creating Environment Variables in Heroku\n\nThe `DATABASE_URL` config var is automatically created by Heroku when we add the [Heroku Postgres add-on]().\n\nWe still have to create the `SECRET_KEY_BASE` config:\n\n```console\n$ heroku config:set SECRET_KEY_BASE=\"my_secret_key_base\" # This needs a randomly generated string\nSetting config vars and restarting mysterious-meadow-6277... done, v3\nSECRET_KEY_BASE: my_secret_key_base\n```\n\n## Deploy Time!\n\nOur project is now ready to be deployed on Heroku.\n\nLet's commit all our changes:\n\n```\n$ git add config/prod.exs .gitignore\n$ git commit -m \"Heroku ready\"\n```\n\nAnd deploy:\n\n```console\n$ git push heroku master\nCounting objects: 55, done.\nDelta compression using up to 8 threads.\nCompressing objects: 100% (49/49), done.\nWriting objects: 100% (55/55), 48.48 KiB | 0 bytes/s, done.\nTotal 55 (delta 1), reused 0 (delta 0)\nremote: Compressing source files... done.\nremote: Building source:\nremote:\nremote: -----> Multipack app detected\nremote: -----> Fetching custom git buildpack... done\nremote: -----> elixir app detected\nremote: -----> Checking Erlang and Elixir versions\nremote:        WARNING: elixir_buildpack.config wasn't found in the app\nremote:        Using default config from Elixir buildpack\nremote:        Will use the following versions:\nremote:        * Stack cedar-14\nremote:        * Erlang 17.5\nremote:        * Elixir 1.0.4\nremote:        Will export the following config vars:\nremote:        * Config vars DATABASE_URL\nremote:        * MIX_ENV=prod\nremote: -----> Stack changed, will rebuild\nremote: -----> Fetching Erlang 17.5\nremote: -----> Installing Erlang 17.5 (changed)\nremote:\nremote: -----> Fetching Elixir v1.0.4\nremote: -----> Installing Elixir v1.0.4 (changed)\nremote: -----> Installing Hex\nremote: 2015-07-07 00:04:00 URL:https://s3.amazonaws.com/s3.hex.pm/installs/1.0.0/hex.ez [262010/262010] ->\n\"/app/.mix/archives/hex.ez\" [1]\nremote: * creating /app/.mix/archives/hex.ez\nremote: -----> Installing rebar\nremote: * creating /app/.mix/rebar\nremote: -----> Fetching app dependencies with mix\nremote: Running dependency resolution\nremote: Dependency resolution completed successfully\nremote: [...]\nremote: -----> Compiling\nremote: [...]\nremote: Generated phoenix_heroku app\nremote: [...]\nremote: Consolidated protocols written to _build/prod/consolidated\nremote: -----> Creating .profile.d with env vars\nremote: -----> Fetching custom git buildpack... done\nremote: -----> Phoenix app detected\nremote:\nremote: -----> Loading configuration and environment\nremote:        Loading config...\nremote:        WARNING: phoenix_static_buildpack.config wasn't found in the app\nremote:        Using default config from Phoenix static buildpack\nremote:        Will use the following versions:\nremote:        * Node 0.12.4\nremote:        Will export the following config vars:\nremote:        * Config vars DATABASE_URL\nremote:        * MIX_ENV=prod\nremote:\nremote: -----> Installing binaries\nremote:        Downloading node 0.12.4...\nremote:        Installing node 0.12.4...\nremote:        Using default npm version\nremote:\nremote: -----> Building dependencies\nremote:        [...]\nremote:        Running default compile\nremote:               Building Phoenix static assets\nremote:        07 Jul 00:06:22 - info: compiled 3 files into 2 files, copied 3 in 3616ms\nremote:        Check your digested files at 'priv/static'.\nremote:\nremote: -----> Finalizing build\nremote:        Creating runtime environment\nremote:\nremote: -----> Discovering process types\nremote:        Procfile declares types     -> (none)\nremote:        Default types for Multipack -> web\nremote:\nremote: -----> Compressing... done, 82.1MB\nremote: -----> Launching... done, v5\nremote:        https://mysterious-meadow-6277.herokuapp.com/ deployed to Heroku\nremote:\nremote: Verifying deploy... done.\nTo https://git.heroku.com/mysterious-meadow-6277.git\n * [new branch]      master -> master\n```\n\nTyping `heroku open` in the terminal should launch a browser with the Phoenix welcome page opened.\n\nAnd that's it!\n\n## Useful Heroku Commands\n\nWe can look at the logs of our application by running the following command in our project directory:\n\n```console\n$ heroku logs # use --tail if you want to tail them\n```\n\nWe can also start an IEx session attached to our terminal for experimenting in our app's environment:\n\n```console\n$ heroku run iex -S mix\n```\n\nIn fact, we can run anything using the `heroku run` command, like the Ecto migration task for instance:\n\n```console\n$ heroku run mix ecto.migrate\n```","updates":[],"order":1,"isReference":false,"hidden":false,"sync_unique":"","link_url":"","link_external":false,"_id":"55aab7fc3be8f60d00041d75","category":{"sync":{"isSync":false,"url":""},"pages":["55aab7fc3be8f60d00041d75"],"title":"Deployment","slug":"deployment","order":3,"from_sync":false,"reference":false,"_id":"559218ad1da5250d001e9673","createdAt":"2015-06-18T21:59:42.796Z","project":"54348ec95b10711400c6c445","version":"559218ac1da5250d001e966f","__v":1},"githubsync":"","project":"54348ec95b10711400c6c445","user":"5435b410495d5d0800f3a603","__v":2,"createdAt":"2015-07-18T20:33:00.584Z","version":{"version":"0.14.0","version_clean":"0.14.0","codename":"","is_stable":false,"is_beta":true,"is_hidden":false,"is_deprecated":false,"categories":["559218ad1da5250d001e9670","559218ad1da5250d001e9671","559218ad1da5250d001e9672","559218ad1da5250d001e9673","559218ad1da5250d001e9674"],"_id":"559218ac1da5250d001e966f","releaseDate":"2015-06-30T04:18:52.132Z","__v":1,"createdAt":"2015-06-30T04:18:52.132Z","forked_from":"5558c642eb56ae2f00f714fc","project":"54348ec95b10711400c6c445"}}
### What we'll need The only thing we'll need for this guide is a working Phoenix application. If you don't already have one, follow the [Up and Running guide](http://www.phoenixframework.org/docs/up-and-running) to create your first Phoenix application. ### Goals Our main goal for this is guide is to get a Phoenix application running on Heroku. ## Steps Let's separate this process into a few steps so we can keep track of where we are. - Initialize Git repository - Sign up for Heroku - Install the Heroku Toolbelt - Create the Heroku application - Add the Phoenix static buildpack - Make our project Heroku-ready - Deploy time! - Useful Heroku commands ## Initializing Git repository [Git](https://git-scm.com/) is a popular decentralized revision control system and is also used to deploy apps to Heroku. Before we can push to Heroku we'll need to initialize a local Git repository and commit our files to it. We can do so by running the following commands in our project directory: ```console $ git init $ git add . $ git commit -m "Initial commit" ``` You can learn more about how Heroku is using Git [here](https://devcenter.heroku.com/articles/git#tracking-your-app-in-git). ## Signing up for Heroku Signing up to Heroku is very simple, just head over to [https://signup.heroku.com/www-header](https://signup.heroku.com/www-header) and fill in the form. The Free plan will give us one web [dyno](https://devcenter.heroku.com/articles/dynos#dynos) and one worker dyno, as well as a PostgreSQL and Redis instance for free. These are meant to be used for testing and development, and come with some limitations. If you plan to run some serious business on Heroku you should definitely considering upgrading to a paid plan. ## Installing the Heroku Toolbelt Once we have signed up, we can download the correct version of the Heroku Toolbelt for our system [here](https://toolbelt.heroku.com/). The Heroku CLI, part of the Toolbelt, is useful to create Heroku applications, list currently running dynos for an existing application, tail logs or run one-off commands (mix tasks for instance). ## Creating the Heroku Application Now that we have the Toolbelt installed, let's create the Heroku application. In our project directory, run: > Note: the first time you use a Heroku command you need to log in. When prompted, just enter the email and password you gave during signup. ```console $ heroku create --buildpack "https://github.com/HashNuke/heroku-buildpack-elixir.git" Creating mysterious-meadow-6277... done, stack is cedar-14 Buildpack set. Next release on mysterious-meadow-6277 will use https://github.com/HashNuke/heroku-buildpack-elixir.git. https://mysterious-meadow-6277.herokuapp.com/ | https://git.heroku.com/mysterious-meadow-6277.git Git remote heroku added ``` > Note: the name of the Heroku application is the random string after "Creating" in the output above (mysterious-meadow-6277). It will be different when you create the application on your computer. The `--buildpack` option we are passing allows us to specify the [Elixir buildpack](https://github.com/HashNuke/heroku-buildpack-elixir) we want Heroku to use. A [buildpack](https://devcenter.heroku.com/articles/buildpacks) is a convenient way of packaging framework and/or runtime support. In our case it's installing Erlang, Elixir, fetching our application dependencies, and so on, before we run it. The URL in the output is the URL to our application. If we open it in our browser now, we will get the default Heroku welcome page. > Note: if you forgot to initialize your Git repository before running the `heroku create` command you can add the Git remote manually by running the following command: `heroku git:remote -a [your app name].` ## Adding the Phoenix Static Buildpack We need to compile static assets for a successful Phoenix deployment. The [Phoenix static buildpack](https://github.com/gjaldon/heroku-buildpack-phoenix-static) can take care of that for us, so let's add it now. ```console $ heroku buildpacks:add https://github.com/gjaldon/heroku-buildpack-phoenix-static.git Buildpack added. Next release on mysterious-meadow-6277 will use: 1. https://github.com/HashNuke/heroku-buildpack-elixir.git 2. https://github.com/gjaldon/heroku-buildpack-phoenix-static.git Run `git push heroku master` to create a new release using these buildpacks. ``` ## Making our Project Heroku-ready Every new Phoenix project ships with a config file `config/prod.secret.exs` which stores configuration that should not be commited along with our source code. By default Phoenix adds it to our `.gitignore` file. This works great except Heroku uses [environment variables](https://devcenter.heroku.com/articles/config-vars) to pass sensitive informations to our application. It means we need to make some changes to our config before we can deploy. First, let's copy over the content of `config/prod.secret.exs` to `config/prod.exs`. Now we need to load the necessary environment variables in our configuration. In our case: ```elixir config :hello_phoenix, HelloPhoenix.Endpoint, secret_key_base: "my_secret_key" ``` becomes: ```elixir config :hello_phoenix, HelloPhoenix.Endpoint, secret_key_base: System.get_env("SECRET_KEY_BASE") ``` and: ```elixir config :hello_phoenix, HelloPhoenix.Repo, adapter: Ecto.Adapters.Postgres, username: "postgres", password: "postgres", database: "hello_phoenix_prod", size: 20 # The amount of database connections in the pool ``` becomes: ```elixir config :hello_phoenix, HelloPhoenix.Repo, adapter: Ecto.Adapters.Postgres, url: System.get_env("DATABASE_URL"), size: 20 # The amount of database connections in the pool ``` Also we don't need to import the `config/prod.secret.exs` file in our prod config so you can delete the following line: ```elixir import_config "prod.secret.exs" ``` Your final `config/prod.exs` should now look something like this (I've removed the comments for readability): ``` use Mix.Config config :hello_phoenix, HelloPhoenix.Endpoint, http: [port: System.get_env("PORT")], url: [host: System.get_env("HOST"), port: 80], cache_static_manifest: "priv/static/manifest.json" config :logger, level: :info config :hello_phoenix, HelloPhoenix.Endpoint, secret_key_base: System.get_env("SECRET_KEY_BASE") config :hello_phoenix, HelloPhoenix.Repo, adapter: Ecto.Adapters.Postgres, url: System.get_env("DATABASE_URL"), size: 20 # The amount of database connections in the pool ``` We can now remove the `config/prod.secret.exs` from the disk: ```console $ rm config/prod.secret.exs ``` And from our `.gitignore` file: ``` # Mix artifacts /_build /deps /*.ez # Generate on crash by the VM erl_crash.dump # Static artifacts /node_modules # Since we are building assets from web/static, # we ignore priv/static. You may want to comment # this depending on your deployment strategy. /priv/static/ ### DELETE ALL THE LINES BELOW ### # The config/prod.secret.exs file by default contains sensitive # data and you should not commit it into version control. # # Alternatively, you may comment the line below and commit the # secrets file as long as you replace its contents by environment # variables. /config/prod.secret.exs ``` ## Creating Environment Variables in Heroku The `DATABASE_URL` config var is automatically created by Heroku when we add the [Heroku Postgres add-on](). We still have to create the `SECRET_KEY_BASE` config: ```console $ heroku config:set SECRET_KEY_BASE="my_secret_key_base" # This needs a randomly generated string Setting config vars and restarting mysterious-meadow-6277... done, v3 SECRET_KEY_BASE: my_secret_key_base ``` ## Deploy Time! Our project is now ready to be deployed on Heroku. Let's commit all our changes: ``` $ git add config/prod.exs .gitignore $ git commit -m "Heroku ready" ``` And deploy: ```console $ git push heroku master Counting objects: 55, done. Delta compression using up to 8 threads. Compressing objects: 100% (49/49), done. Writing objects: 100% (55/55), 48.48 KiB | 0 bytes/s, done. Total 55 (delta 1), reused 0 (delta 0) remote: Compressing source files... done. remote: Building source: remote: remote: -----> Multipack app detected remote: -----> Fetching custom git buildpack... done remote: -----> elixir app detected remote: -----> Checking Erlang and Elixir versions remote: WARNING: elixir_buildpack.config wasn't found in the app remote: Using default config from Elixir buildpack remote: Will use the following versions: remote: * Stack cedar-14 remote: * Erlang 17.5 remote: * Elixir 1.0.4 remote: Will export the following config vars: remote: * Config vars DATABASE_URL remote: * MIX_ENV=prod remote: -----> Stack changed, will rebuild remote: -----> Fetching Erlang 17.5 remote: -----> Installing Erlang 17.5 (changed) remote: remote: -----> Fetching Elixir v1.0.4 remote: -----> Installing Elixir v1.0.4 (changed) remote: -----> Installing Hex remote: 2015-07-07 00:04:00 URL:https://s3.amazonaws.com/s3.hex.pm/installs/1.0.0/hex.ez [262010/262010] -> "/app/.mix/archives/hex.ez" [1] remote: * creating /app/.mix/archives/hex.ez remote: -----> Installing rebar remote: * creating /app/.mix/rebar remote: -----> Fetching app dependencies with mix remote: Running dependency resolution remote: Dependency resolution completed successfully remote: [...] remote: -----> Compiling remote: [...] remote: Generated phoenix_heroku app remote: [...] remote: Consolidated protocols written to _build/prod/consolidated remote: -----> Creating .profile.d with env vars remote: -----> Fetching custom git buildpack... done remote: -----> Phoenix app detected remote: remote: -----> Loading configuration and environment remote: Loading config... remote: WARNING: phoenix_static_buildpack.config wasn't found in the app remote: Using default config from Phoenix static buildpack remote: Will use the following versions: remote: * Node 0.12.4 remote: Will export the following config vars: remote: * Config vars DATABASE_URL remote: * MIX_ENV=prod remote: remote: -----> Installing binaries remote: Downloading node 0.12.4... remote: Installing node 0.12.4... remote: Using default npm version remote: remote: -----> Building dependencies remote: [...] remote: Running default compile remote: Building Phoenix static assets remote: 07 Jul 00:06:22 - info: compiled 3 files into 2 files, copied 3 in 3616ms remote: Check your digested files at 'priv/static'. remote: remote: -----> Finalizing build remote: Creating runtime environment remote: remote: -----> Discovering process types remote: Procfile declares types -> (none) remote: Default types for Multipack -> web remote: remote: -----> Compressing... done, 82.1MB remote: -----> Launching... done, v5 remote: https://mysterious-meadow-6277.herokuapp.com/ deployed to Heroku remote: remote: Verifying deploy... done. To https://git.heroku.com/mysterious-meadow-6277.git * [new branch] master -> master ``` Typing `heroku open` in the terminal should launch a browser with the Phoenix welcome page opened. And that's it! ## Useful Heroku Commands We can look at the logs of our application by running the following command in our project directory: ```console $ heroku logs # use --tail if you want to tail them ``` We can also start an IEx session attached to our terminal for experimenting in our app's environment: ```console $ heroku run iex -S mix ``` In fact, we can run anything using the `heroku run` command, like the Ecto migration task for instance: ```console $ heroku run mix ecto.migrate ```