Phoenix Application Deployment with Nanobox

Steve Domino
HackerNoon.com
5 min readMay 2, 2017

--

This article was originally a pull request for the Phoenix documentation, however it was ultimately rejected (for reasons with which I completely agree):

Following the advice of Chris McCord, I’ve turned this into a blog post which will hopefully benefit the Elixir/Phoenix community.

The goal of this guide is to show you how to deploy a Phoenix application to production using Nanobox.

(warning, shameless plug ahead)

Why Nanobox? Two reasons really.

I’m an engineer at Nanobox, so naturally I want to promote it. But really, it’s more than that. I truly believe that Nanobox is the easiest way to develop and deploy any application.

Nanobox allows application developers to deploy their own applications, removing the need for DevOps, or automating it, or whatever you want to call it. Nanobox does it for you so you don’t have to.

(/shameless plug)

Before we begin

To get started, you really only need three things:

  • A Phoenix application
  • Nanobox Desktop (the Nanobox CLI tool)
  • A hosting account

You technically don’t even need Erlang, Elixir, or Phoenix, but chances are you already have them installed.

Note: All `nanobox` commands are run from the root of your application.

A Phoenix Application

If you need an application to deploy, you can use one of these “quickstarts”:

You can also create a new application following the Nanobox from scratch guide, or the Phoenix Up and Running guide.

Nanobox Desktop

If you’re using Nanobox for the first time, create a free Nanobox account and download and install Nanobox Desktop.

A Provider Account

The last thing you’ll need is a hosting account like Amazon AWS or DigitalOcean, which you’ll link with your Nanobox account. Nanobox is frequently adding more providers to the list, so if you don’t see yours, or you don’t use one, you can also create your own integration.

Doc: Connecting your account to your host

Doc: Creating a custom provider

Getting Started

There are four steps when deploying your application with Nanobox:

Create a Nanobox Application

New applications are created through the Nanobox Dashboard. During this process, you’ll be prompted to name your application, and select a host and region where you want your application to live.

Doc: Launching a new application

Configure your project

There are two pieces to configuring your application to run with Nanobox:

The boxfile.yml

Nanobox uses a simple config file called a boxfile.yml to provision development and production environments. Create a boxfile.yml at the root of your project with the following:

At this point, if you want to, you can use nanobox run to provision a local development environment. Once started, you can either view your app at the IP given from the run command, or generate a dns alias with the dns add command:

Now visit your app at nanobox-phoenix.dev

Doc: The boxfile.yml

Environment Variables

Nanobox uses environment variables (evars) to keep certain information secret. You’ll need to make a few modifications to your app to use evars.

First, you’ll load the secret key from the Nanobox evars rather than the config/prod.secret.exs by adding it to the config/prod.exs:

Note: You can generate a secret key for your application with mix phoenix.gen.secret.

Next, add a production database configuration to config/prod.exs:

Note: If you plan on developing or staging your application locally you’ll want to update your /config/dev.exsand your /config/test.exs with the same username, password, and hostname evars as above.

Nanobox generates DATA_DB_USER, DATA_DB_PASS, and DATA_DB_HOST evars for you, but you'll need to add a SECRET_KEY_BASE, and any others you want, manually.

Development and staging evars are added with the evar add command:

Note: Production evars are added through the Nanobox dashboard.

Once you’ve added all your evars, comment out the line import_config “prod.secret.exs" in your /config/prod.exs:

Doc: Environment Variables

Stage your application (optional)

You can stage a production deploy locally with the dry-run command. Although this step is optional, it’s highly recommended.

As with nanobox run, you can visit your application at the IP given from the dry-run command, or generate a dns alias with the dns add command:

Now try visiting your app at nanobox-phoenix.stage

Doc: Preview your app

Link and Deploy to production

The last thing you’ll need to do is link your codebase to the application you created at the beginning of this guide.

Note: The first time you try and link the application you’ll be asked to login using your Nanobox credentials.

That’s it!

Doc: Adding a remote

Important links

Hacker Noon is how hackers start their afternoons. We’re a part of the @AMI family. We are now accepting submissions and happy to discuss advertising & sponsorship opportunities.

If you enjoyed this story, we recommend reading our latest tech stories and trending tech stories. Until next time, don’t take the realities of the world for granted!

--

--

Steve Domino
HackerNoon.com

Husband, father, and tech enthusiast. I’m passionate about finding, using, and creating awesome stuff. Currently creating awesome stuff at https://getdivvy.com/