How to host Elixir, Phoenix apps on Vultr, DigitalOcean, Linode, AWS EC2 or any server

OpsCaptain Team — Feb, 27, 2018

A step by step guide on hosting your Elixir and Phoenix apps on your cloud server using OpsCaptain. The process involves providing OpsCaptain root access to your Ubuntu 16 server and then running a single command to deploy. Your server can be hosted with any cloud provider, that is with AWS, Vultr, DigitalOcean, linode etc. The only mandatory requirement is your server should be running Ubuntu version 16.

This tutorial is compatible with both Elixir and Phoenix apps. Under the hood, OpsCaptain will setup your server with Docker, OpsCaptain Agent, Proxy and use the Open source heroku-buildpack-elixir and heroku-buildpack-phoenix-static. After this tutorial, you will have a Heroku like PaaS on your own server.

Step 1. Create your OpsCaptain account

Create a new account from the OpsCaptain sign up page. You will be required to verify your email via an email sent to the provided address. Once you receive the email, simply click on the account activation link to activate your account.

Step 2. Provision a server with your cloud provider and provide OpsCaptain root access

Provision a server running Ubuntu 16 from your cloud provider. After this server is provisioned and you are able to log in to the server you just provisioned, navigate to the OpsCaptain Add a New Ubuntu 16 host page and provide the Ip address, and password of your server. It is important you log in to your server first as some cloud providers like DigitalOcean require you first change your password before using the server.

Step 3. Create your app from the dashboard

Navigate to the Create a new app page and then enter the name of your app, select the database required for your app, select the server you added in step 2 and then click on the "Go Captain" button to create your first app. You can create and host as many apps you want on your server.

Step 4. Prepare your Phoenix app to run on your server

This section mainly applies to Phoenix apps and is taken from the Pheonix documentation page on deploying to Heroku. It is pretty much the same thing as OpsCaptain uses the same buildpack you will be using on Heroku

Every new Phoenix project ships with a config file config/prod.secret.exs which stores configuration.

First, let’s make sure our secret key is loaded from environment variables instead of config/prod.secret.exs by adding a secret_key_base line in config/prod.exs (remember to add a comma to the end of the preceding line):

config :hello, HelloWeb.Endpoint, load_from_system_env: true, url: [host: "", port: 80], cache_static_manifest: "priv/static/cache_manifest.json", secret_key_base: Map.fetch!(System.get_env(), "SECRET_KEY_BASE")

Then, we’ll add the production database configuration to config/prod.exs if applicable. If you selected to create a database in Step 3, OpsCaptain automatically sets the environment variable "DATABASE_URL" on your executing containers.

# Configure your database config :hello, Hello.Repo, adapter: Ecto.Adapters.Postgres, url: System.get_env("DATABASE_URL"), pool_size: String.to_integer(System.get_env("POOL_SIZE") || "10"), ssl: true

Now, let’s tell Phoenix to use our OpsCaptain URL. You can find the OpsCaptain URL assigned to your app by clicking on the name of your app from the Apps & Databases page. You will be presented with the app details page, simply scroll down to the "App Url" section and copy the hostname from your app url. If you want SSL, you will have to first add your own custom domain and then Create a Free Let's encrypt SSL certificate

url: [scheme: "http", host: "", port: 80]

Step 5. Create the ocmanifest.json file

In your project's root folder, create the ocmanifest.json file and then using the below example JSON, enter the name of your app, the buildpacks and any environment variables you may have.

Note, you only have to specify the buildpacks attribute only once in your JSON file. The below example simply shows the values you will use when deploying a plain Elixir or Phoenix app.

{ // The name you provided when creating the app in step 1 name: "elixir-app", // Completely optional, configure environment variables if any env: { "POOL_SIZE": "18" "SECRET_KEY_BASE": "xvafzY4y01jYuzLm3ecJqo008dVnU3CN4f+MamNd1Zue4pXvfvUjbiXT8akaIF53" }, // If you are deploying plain Elixir use: buildpacks: ["elixir"] // Or If you are deploying Phoenix use: buildpacks: ["elixir","phoenix-static"] }

Step 6. Deploy your app

First you must install the OpsCaptain CLI and then deploy.

// Install the CLI using the below command npm install -g opscaptain-cli // CD to your project folder and then deploy opscaptain deploy