How to Host a Ghost Blog for Free on Fly.io

How to Host a Ghost Blog for Free on Fly.io
Photo by Drew Tilk / Unsplash

Ghost is an amazing platform for content creators with tons of features to enable blogging, newsletters and subscriptions. Most importantly, it’s a pleasure to use. If you’ve ever felt the urge to gouge our your own eyeballs after trying to use WordPress, you should definitely give Ghost a try!

If you’ve ever felt the urge to gouge our your own eyeballs after trying to use WordPress, you should definitely give Ghost a try!

If you want to get started with Ghost, they offer a fully hosted version starting at $9 a month. But because the project is fully open-source, you also have the option of hosting it yourself for free. While I believe Ghost is a product worth supporting with the paid plan, the free option is great for getting up and running with a new site.

Fly.io is an amazing app deployment platform that makes it easy to deploy your apps in multiple regions around the world, close to your customers. Their excellent command-line tools make it simple to configure and deploy applications in a predictable, reproducible way.

In this post, we’ll learn how to setup a Ghost blog using the Fly.io platform for free.

What We’re Going to Do

In this post, we’ll go through the following steps:

  • Install the Fly command-line tools
  • Create a free Fly.io account
  • Deploy an app using a pre-made image of the Ghost blogging platform
  • Setup and configure storage for Ghost to store our data in a sqlite3 database
  • Deploy our app!
  • Setup a custom domain name

Let’s get started!

Install the Fly.io Command-Line Tools

The very first step in getting started with Fly.io is to install the command- line tools. You can do so by running:

curl -L https://fly.io/install.sh | sh

Or, if you’re on a Mac running Homebrew, you can also install it like this:

brew install flyctl

Create an Account and Login to Fly.io

Now that we’ve got the CLI tools installed, we can create and login to our account using the command:

flyctl auth login

This command will open up a browser window to let you create and/or login to an account. I recommend authing with your GitHub account to make the process simple.

After you’ve logged in successfully in the browser, you should be able to return to the command-line. Your authentication session is automatically saved.

Create Your App

Next, we need to create an app with Fly, and configure it to use a ready-made Docker image of Ghost:

mkdir my-blog
cd my-blog
flyctl launch --image=ghost:5.17.2 --no-deploy

We’re using a Docker image of Ghost version 5.17.2, which is as of the time of this writing, the latest version. If you want to check that this is still the latest version, have a look at Docker Hub. Be aware though, that changing image versions may require different setup instructions than what’s described in this post.

After entering the command, follow the instructions given on the command-line. For the app name, choose one or let Fly choose one for you, but take note of it — you’ll need it later. When asked about which region you’d like to deploy to, simply pick one that’s closest to your audience!

You’ll note that we included a --no-deploy flag in our command. That’s because we’re not quite ready to deploy and launch the app yet, we’re just setting up the configuration. The result of this command will be our Fly configuration file fly.toml. We’ll make a couple of changes there before we deploy.

Create Storage for our Database

Next, we’ll need to create some persistent disk storage to hold our content:

flyctl volumes create data -s 3

Choose the same region you chose in the previous step. When this is done, we’ll have a 3GB volume called data that we can connect to our application instance.

Update Your Fly Config

Now we need to make some changes to our fly.toml config. We need to configure 1) the hostname 2) the database 3) where to mount our disk volume and 4) the port number.

Here are the relevant sections of the configuration. Note that you need to merge the configuration below with the rest of the configuration in each section, don’t just replace all of the existing config!

[env]  
  url = "https://your-project-name.fly.dev"  
  database__client = "sqlite3"  
  database__connection__filename = "content/data/ghost.db"  
  database__useNullAsDefault = "true"  
  database__debug = "false"
  
[mounts]
  source="data"
  destination="/var/lib/ghost/content"

[[services]]  
  internal_port = 2368

Deploy Your Blog!

Now that you’ve gotten your app configured, you can deploy it by simply running:

flyctl deploy

This process may take a minute or two, but in the end, you should have your blog deployed at https://your-project-name.fly.dev.

Navigate to https://your-project-name.fly.dev/ghost to setup your account and administer your new blog!

Optional — Set a Custom Domain

This step is optional, but if you want to, you can configure a custom domain name, instead of just your-project-name.fly.dev. Setting up the custom host will require that you have DNS access to your custom domain.

When you’re ready, you can add your domain with the following commands:

fly domains add hostname.com
fly certs add hostname.com

After adding the cert, you should see some output like this:

You are creating a certificate for xxxxxxxxxxxx.com
We are using lets_encrypt for this certificate.

You can direct traffic to xxxxxxxxxxxx.com by:

1: Adding an A record to your DNS service which reads

A @ XX.XX.XX.XX

You can validate your ownership of xxxxxxxxxxxx.com by:

2: Adding an AAAA record to your DNS service which reads:

AAAA @ XXXX:XXXX:X::X:XXXX

Be sure to follow the instructions here and set up the two DNS records! Your domain will not work until they are setup. Additionally, be prepared that it can take about 15 minutes or more to verify and issue the cert.

You can check the status of the cert with the command fly certs list and fly certs show hostname.com.

Optional — Configure Your Transactional Mailer

If you plan on using some of the email features of Ghost, you’ll want to setup your email configuration.

For bulk emailing, you will need to setup Mailgun in the Ghost admin, but to get simple transactional emails working (notifications, signup confirmations), you can do so with any valid SMTP configuration:

[env]  
  mail__from = "..."  
  mail__transport = "SMTP"  
  mail__options__host = "..."  
  mail__options__port = ...  
  mail__options__auth__user = ...  
  mail__options__auth__pass = ...

The Final Step — Enjoy!

If you’ve run all these commands successfully, you should have your new blog up and running. Navigate to https://your-project-name.fly.dev/ghost and start building your content!