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):
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.
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
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.
Step 6. Deploy your app
First you must install the OpsCaptain CLI and then deploy.