Self host Plausible with Fly
🗡️

Self host Plausible with Fly

Tags
Technology
Description
Plausible.io 💜 Fly.io
Published
Published December 3, 2022
🆕
Upgraded to version 2.0 following instructions here Aug 15, 2023
 
The latent docker image issue was gone, simply using the latest release tag plausible@latest in Dockerfile would work
City level data is automatically downloaded. Set an env variable MAXMIND_LICENSE_KEY before app startup if you need it
Ran into some Clickhouse connection issue during app boot up, resolved by setting CLICKHOUSE_DATABASE_URL (plausible_events_db) and DATABASE_URL (plausible_db) with their ip v4 addresses
 
Plausible.io is an open source alternative to Google Analytics created by some inspiring indie devs. Its vision to be transparent about how website traffic data is handled really separates it apart from other proprietary alternatives. I first came across it from above Indie Hacker Podcast and managed to get it hosted on an EC2 container. However, the AWS bills increased too fast that I had to switch to Fly.io for cheaper hosting.

How

By following the rule of “Don’t reinvent the wheel”, I found that similar need was already addressed by others, well summarized in a blog post plus a code repository.
I basically went through the same process by following the instructions, besides fixing some issues related to outdated information. In addition, I also enabled the
City level location data
Updated Nov 23, 2021
available in recent release.

Problems

Plausible deployment failures
  • Caused by under provisioning
    • Solution:
      fly scale memory 1024
  • release_command in fly.toml failed to create and migrate DB
    • I wound up deploying once to create the DB, then again to migrate the DB when the first deploy failed to run.
      Solution:
      1. Update fly.toml
        1. - release_command = 'db migrate' + release_command = 'db createdb'
      1. Deploy
      1. Run the migration command
        1. fly ssh console entrypoint.sh db migrate
The latest docker image release are behind development (i.e. master branch)
  • DB connection issue. Fly.io does all internal networking over IPv6, and that required a Change to how phoenix apps are built. The phx generators have been updated, but it required a PR for plausible.
  • The other issue is that I wanted to force_ssl , but that’s a compile-time configuration
    • config :plausible, PlausibleWeb.Endpoint, force_ssl: [rewrite_on: [:x_forwarded_proto]],
  • Missing features like region/city level information
Solution:
  1. Fork
  1. Copy fly.toml from the plausible directory to the forked analytics directory
  1. Deploy change fly deploy

Guides

Prerequisite

Make sure fly CLI is installed and is logged in to be able to run other commands
brew install fly fly auth login

Postgres DB

fly pg create plausible-db

Clickhouse

  1. cd clickhouse
  1. fly apps create plausible_clickhouse
  1. fly vol create plausible_clickhouse_data
  1. fly deploy

Plausible (Analytics)

  1. cd plausible
  1. fly apps create plausible
  1. fly pg attach plausible-db
  1. Set secrets
    1. openssl rand -base64 64 | tr -d '\n' ; echo fly secrets set SECRET_KEY_BASE=<FROM ABOVE> \ ADMIN_USER_NAME=<username> \ ADMIN_USER_EMAIL=<email> \ ADMIN_USER_PWD=<password> \ BASE_URL=<host_url>
  1. fly scale memory 1024 scale up to avoid under provision

Add Cities Level Data

  1. cd plausible
  1. Create a volume for geo data
    1. fly vol create myapp_data
      # fly.toml [mounts] source="myapp_data" destination="/data"
  1. Download geonames.csv & geolite2-city.mmdb to the container
    1. fly ssh console
    2. cd /data
    3. # geonames.csv wget https://s3.eu-central-1.wasabisys.com/plausible-application/geonames.csv # geolite2-city.mmdb export MAXMIND_LICENSE_KEY=LNpsJCCKPis6XvBP # replace this with your own wget "https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-City&license_key=${MAXMIND_LICENSE_KEY}&suffix=tar.gz" -O geolite2-city.mmdb.gz gunzip geolite2-city.mmdb.gz
  1. Set Secrets to the data location
    1. fly secrets set IP_GEOLOCATION_DB='/data/geolite2-city.mmdb' GEONAMES_SOURCE_FILE='/data/geonames.csv'

DNS Entries / SSL

fly certs create <BASE_URL>
 

 
Note:
Self host on EC2 guide