{"componentChunkName":"component---src-templates-lesson-js","path":"/phoenix-chat/deploying-the-api-to-heroku","result":{"data":{"mdx":{"body":"function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }\n\nfunction _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }\n\nfunction _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }\n\n/* @jsx mdx */\nvar _frontmatter = {\n  \"title\": \"Deploying the API to Heroku\",\n  \"order\": 49,\n  \"free\": false,\n  \"active\": true,\n  \"tech\": [\"phoenix\", \"heroku\"]\n};\n\nvar makeShortcode = function makeShortcode(name) {\n  return function MDXDefaultShortcode(props) {\n    console.warn(\"Component \" + name + \" was not imported, exported, or provided by MDXProvider as global scope\");\n    return mdx(\"div\", props);\n  };\n};\n\nvar layoutProps = {\n  _frontmatter: _frontmatter\n};\nvar MDXLayout = \"wrapper\";\nreturn function MDXContent(_ref) {\n  var components = _ref.components,\n      props = _objectWithoutProperties(_ref, [\"components\"]);\n\n  return mdx(MDXLayout, _extends({}, layoutProps, props, {\n    components: components,\n    mdxType: \"MDXLayout\"\n  }), mdx(\"h1\", null, \"Deploying the API to Heroku\"), mdx(\"ul\", null, mdx(\"li\", {\n    \"parentName\": \"ul\"\n  }, \"Phoenix Buildpacks\"), mdx(\"li\", {\n    \"parentName\": \"ul\"\n  }, \"Configuring the App\"), mdx(\"li\", {\n    \"parentName\": \"ul\"\n  }, \"Environment Variables\"), mdx(\"li\", {\n    \"parentName\": \"ul\"\n  }, \"Deploying\")), mdx(\"p\", null, \"The performance of Phoenix/Elixir is most apparent when using Heroku. Popular sites like \", mdx(\"a\", {\n    \"href\": \"hex.pm\",\n    \"parentName\": \"p\"\n  }, \"Hex.pm\"), \" run on a single dyno resulting in big cost savings.\"), mdx(\"p\", null, \"Before we proceed, you'll need to register for a Heroku account and install the \", mdx(\"a\", {\n    \"href\": \"https://toolbelt.heroku.com/\",\n    \"parentName\": \"p\"\n  }, \"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.\"), mdx(\"h2\", null, \"Phoenix Buildpacks\"), mdx(\"p\", null, \"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.\"), mdx(\"p\", null, \"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:\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-bash\",\n    \"parentName\": \"pre\"\n  }, \"$ heroku create --buildpack \\\"https://github.com/HashNuke/heroku-buildpack-elixir.git\\\"\\nCreating enigmatic-ravine-4201... done, stack is cedar-14\\nBuildpack set. Next release on enigmatic-ravine-4201 will use https://github.com/HashNuke/heroku-buildpack-elixir.git.\\nhttps://enigmatic-ravine-4201.herokuapp.com/ | https://git.heroku.com/enigmatic-ravine-4201.git\\nGit remote heroku added\\n\")), mdx(\"p\", null, \"Or if you've already signed in and created a Heroku project, you can add it as a git remote and assign a buildpack.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-bash\",\n    \"parentName\": \"pre\"\n  }, \"$ heroku buildpacks:add \\\"https://github.com/HashNuke/heroku-buildpack-elixir.git\\\"\\n\")), mdx(\"p\", null, \"It's probably worthwhile to look through the brief \", mdx(\"a\", {\n    \"href\": \"https://github.com/HashNuke/heroku-buildpack-elixir\",\n    \"parentName\": \"p\"\n  }, \"documentation\"), \" for this particular buildpack in case you need to do some debugging.\"), mdx(\"p\", null, \"Sometimes Heroku will not automatically add the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"git remote\"), \". You can test this by running the command \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"git remote -v\"), \" and you should see two for \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"origin\"), \" that go to Github and two for \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"heroku\"), \", which go to Heroku. In the event that it does not, log in to \", mdx(\"a\", {\n    \"href\": \"heroku.com\",\n    \"parentName\": \"p\"\n  }, \"Heroku.com\"), \", select the project, click \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Settings\"), \", and copy the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Git URL\"), \" from the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Info\"), \" section and run the command (with the proper URL):\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-bash\",\n    \"parentName\": \"pre\"\n  }, \"$ git remote add heroku <git_url>\\n\")), mdx(\"p\", null, \"The create command tells us it created \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"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.\"), mdx(\"blockquote\", null, mdx(\"p\", {\n    \"parentName\": \"blockquote\"\n  }, mdx(\"em\", {\n    \"parentName\": \"p\"\n  }, \"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:\")), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-bash\",\n    \"parentName\": \"pre\"\n  }, \"$ heroku buildpacks:add https://github.com/gjaldon/heroku-buildpack-phoenix-static.git\\n\")), mdx(\"h2\", null, \"Configuring the App\"), mdx(\"p\", null, \"With the application created and buildpack set, we need to make some final changes to Phoenix before we can deploy to Heroku.\"), mdx(\"p\", null, \"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 \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"host:\"), \" that relates to your Heroku project. Let's open \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"config/prod.exs\"), \" and add the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"secret_key_base\"), \" key and retrieve the value from the system.\"), mdx(\"p\", null, \"We're also setting \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"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.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-elixir\",\n    \"parentName\": \"pre\"\n  }, \"config :phoenix_chat, PhoenixChat.Endpoint,\\n  http: [port: {:system, \\\"PORT\\\"}],\\n  check_origin: false,\\n  force_ssl: [rewrite_on: [:x_forwarded_proto]],\\n  secret_key_base: System.get_env(\\\"SECRET_KEY_BASE\\\")\\n\")), mdx(\"p\", null, \"Since we won't be storing our secret keys in \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"config/prod.secret.exs\"), \", remove the following line from our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"config/prod.exs\"), \":\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-elixir\",\n    \"parentName\": \"pre\"\n  }, \"import_config \\\"prod.secret.exs\\\"\\n\")), mdx(\"p\", null, \"Lastly, we need to update our Repo configure to rely on the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"\\\"DATABASE_URL\\\"\"), \" environment variable Heroku sets:\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-elixir\",\n    \"parentName\": \"pre\"\n  }, \"config :phoenix_chat, PhoenixChat.Repo,\\n  adapter: Ecto.Adapters.Postgres,\\n  url: System.get_env(\\\"DATABASE_URL\\\"),\\n  pool_size: 10,\\n  ssl: true\\n\")), mdx(\"h2\", null, \"Environment Variables\"), mdx(\"p\", null, \"To populate our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"\\\"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:\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-bash\",\n    \"parentName\": \"pre\"\n  }, \"$ heroku addons:create heroku-postgresql:hobby-dev\\n\")), mdx(\"p\", null, \"We will also need to create an \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"elixir_buildpack.config\"), \" file that will tell Elixir about our environment variables in Heroku.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-bash\",\n    \"parentName\": \"pre\"\n  }, \"$ touch elixir_buildpack.config\\n\")), mdx(\"p\", null, \"Within that file, we need to add our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"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.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-js\",\n    \"parentName\": \"pre\"\n  }, \"config_vars_to_export=(\\n  DATABASE_URL\\n  SECRET_KEY_BASE\\n)\\n\")), mdx(\"p\", null, \"Finally, we need to generate and set a new secret key. With mix, let's generate a new secret:\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-bash\",\n    \"parentName\": \"pre\"\n  }, \"$ mix phoenix.gen.secret\\nNrRwQpjBoUeKz1JnvWT+WrmF+hSnRPvkeDkHKwIgv5blKVeS0PUp9GA69S6VGXVf\\n\")), mdx(\"p\", null, \"Now we can add our new secret key to Heroku:\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-bash\",\n    \"parentName\": \"pre\"\n  }, \"$ heroku config:set SECRET_KEY_BASE=\\\"NrRwQpjBoUeKz1JnvWT+WrmF+hSnRPvkeDkHKwIgv5blKVeS0PUp9GA69S6VGXVf\\\"\\n\")), mdx(\"p\", null, \"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.\"), mdx(\"p\", null, \"It might also be a good idea to set \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"always_rebuild\"), \" to true since this will save you from some hard-to-debug errors later on.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-js\",\n    \"parentName\": \"pre\"\n  }, \"config_vars_to_export=(\\n  DATABASE_URL\\n  SECRET_KEY_BASE\\n  MAILGUN_API_KEY\\n  MAILGUN_DOMAIN\\n)\\nalways_rebuild=true\\nelixir_version=1.3.2\\n\")), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-bash\",\n    \"parentName\": \"pre\"\n  }, \"$ heroku config:set MAILGUN_API_KEY=key-cec83902409402940958398a2cf3\\n$ heroku config:set MAILGUN_DOMAIN=https://api.mailgun.net/v3/mg.domainname.io\\n\")), mdx(\"p\", null, \"You're also going to need a \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Procfile\"), \" that tells your server how to run.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-bash\",\n    \"parentName\": \"pre\"\n  }, \"$ echo \\\"web: MIX_ENV=prod mix phoenix.server\\\" > Procfile\\n\")), mdx(\"h2\", null, \"Deploying\"), mdx(\"p\", null, \"We'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:\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-bash\",\n    \"parentName\": \"pre\"\n  }, \"$ git add .\\n$ git commit -am \\\"chore: adjust configuration for deploy\\\"\\n\")), mdx(\"p\", null, \"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 \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"develop\"), \" branch, you would have to run: \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"git push heroku develop:master\")), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-bash\",\n    \"parentName\": \"pre\"\n  }, \"$ git push heroku master\\n\\nCounting objects: 3, done.\\nDelta compression using up to 8 threads.\\nCompressing objects: 100% (3/3), done.\\nWriting objects: 100% (3/3), 288 bytes | 0 bytes/s, done.\\nTotal 3 (delta 2), reused 0 (delta 0)\\nremote: Compressing source files... done.\\n...\\n...\\n...\\nremote: Verifying deploy.... done.\\nTo https://git.heroku.com/enigmatic-ravine-4201.git\\n   f7ac6d3..e08a63e  test -> master\\n\")), mdx(\"p\", null, \"Before we visit our site, let's run our migrations on Heroku. We need to set the mix environment to \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"prod\"), \", as it defaults to \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"dev\"), \".\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-bash\",\n    \"parentName\": \"pre\"\n  }, \"$ heroku run MIX_ENV=prod mix ecto.migrate\\n\")), mdx(\"p\", null, \"Now we're ready to see our app in action, typing \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"heroku open\"), \" in the console will open our browser to our app:\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-bash\",\n    \"parentName\": \"pre\"\n  }, \"$ heroku open\\n\")), mdx(\"p\", null, \"And now we've successfully deployed our Phoenix application to Heroku. Take note of the URL of your API because we will need it later.\"));\n}\n;\nMDXContent.isMDXComponent = true;","excerpt":"Deploying the API to Heroku Phoenix Buildpacks Configuring the App Environment Variables Deploying The performance of Phoenix/Elixir is most…","frontmatter":{"title":"Deploying the API to Heroku","order":49,"active":true,"free":false,"tech":["phoenix","heroku"]}}},"pageContext":{"slug":"deploying-the-api-to-heroku","prevNode":{"frontmatter":{"title":"Routing Messages Using API Keys","active":true,"free":false,"order":48,"tech":["phoenix"]},"fields":{"slug":"routing-messages-using-api-keys"}},"nextNode":{"frontmatter":{"title":"Deploying the Frontend to S3","active":true,"free":false,"order":50,"tech":["react","webpack","aws"]},"fields":{"slug":"deploying-the-frontend-to-s3"}}}},"staticQueryHashes":["1723017916"]}