How to deploy to Fly.io
Fly.io is a great hosting company that lets you deploy your Phoenix application for free. And when you scale out of the free plan you can easily upgrade to the paid tiers. They also let you scale to servers around the world so you can always be close to your users.
The SAAS Starter Kit can more or less easily be deployed to Fly with just a few easy steps. First of all, make sure to get your Fly.io account and then we are ready to go.
Head over to https://fly.io/ to see the latest version of their installation guide.
However, on OS X and Homebrew, you can install it with:
brew install flyctl
The first deploy wont succeed but will let Fly analyze the code and generate some files that are needed.
First question is the app name. You can just leave it blank and let Fly autogenerate one:
When it comes to selecting region, I usually pick one on US eastcoast. Since I am in Europe and most of my expected visitors are from US and Europe, my thinking is that the server will be fast enough. And also, its Fly, so it easy to deploy on new locations.
When it comes to the database, answer Yes, and then pick a suitable one from the list. You can come prtty far with the Development one and upgrade later.
After the database is picked, the launch goes ahead and creates some files and later ask if I would like to deploy not. Pick No. There are some things in the code that needs to be updated.
To be able to run Phoenix in production, you need a new SECRET_KEY_BASE. You can generate a new one with the command:
And it will genereate a random string like:
You can set it on fly by using the command:
fly secrets set SECRET_KEY_BASE="/eA8tKVjSjmN9UH79zovIZ2nx ......"
There are a few minor modifications that you need to do on the Dockerfile. First is to make sure to install
RUN apt-get update -y && apt-get install -y build-essential git npm \
# compile assets
RUN cd assets && npm install
RUN mix assets.deploy
runtime.exs there need to be some updates.
With these changes, I should now be able to deploy and view the site.
Make sure that all steps run through and the the migrations are run. If you use the SAAS Starter Kit and havent done any modifications, this should be the case.
And when I visit the site with the temporary url, I get: