DEV Community

Cover image for Self-Hosting Plausible Analytics With Dokku
Kevin Langley Jr
Kevin Langley Jr

Posted on

Self-Hosting Plausible Analytics With Dokku

In my search for a self-hosted alternative to Google Analytics, I found Plausible Analytics. Plausible is an open-source and privacy focused alternative to Google Analytics, which I wanted to remove entirely from my sites. Self-hosting with the instructions from their documentation is not geared towards someone trying to host it on a Dokku host. This guide will help walk you through the steps of setting up Plausible on a Dokku host. We will create the application, install the required plugins, create all the necessary databases, link them to the application, and deploy the application.

What is Dokku?

Dokku is a popular, open-source, and self-hosted platform as a service (PaaS) that allows users to easily deploy and manage their applications, very similar to your own self-hosted Heroku. Under the hood, Dokku is powered by Docker, uses Heroku buildpacks by default, and has a number of official and community plugins.

There is a one-click install image you can use from Digital Ocean, but I personally like to start with a fresh box and enjoy installing and configuring it all myself.

The only system requirements are 1GB of memory and either Ubuntu 18.04/20.04 x64, Debian 9+ x64, or CentOS 7 x64.

You can read more about using Dokku in my blog post yesterday.

What is Plausible?

Plausible is an open-source and privacy focused alternative to Google Analytics. It uses no cookies and is fully compliant with GDPR, CCPA, and PECR. While it is easiest to just pay the $6 / per month for a cloud hosted instance, I will always choose self-hosted if it is an option and here is no exception. Self-hosting with the instructions from their documentation is not geared towards someone trying to host it on Dokku. Because of it, I wanted to document the process in hopes of helping others set up their own instance of Plausible.

The only requirement is a working Dokku host.

We are going to use the domain analytics.example.com for demonstration purposes. Be sure to replace it with your own domain name when running through the bash command snippets.

First, we're going to SSH into your Dokku Host and create the Plausible app:

dokku apps:create plausible
Enter fullscreen mode Exit fullscreen mode

Next, we will install the PostgreSQL and Clickhouse plugins, if they aren't already installed.

dokku plugin:install https://github.com/dokku/dokku-postgres.git postgres
dokku plugin:install https://github.com/dokku/dokku-clickhouse.git clickhouse
Enter fullscreen mode Exit fullscreen mode

And then, we'll create our instances of the PostgresSQL and Clickhouse databases.

dokku postgres:create plausible-db
dokku clickhouse:create plausible-events-db
Enter fullscreen mode Exit fullscreen mode

Let's then set the environment variables for the database scheme, which just sets it to use the scheme expected within the Plausible Docker image. Then we'll link the databases to the application.

dokku config:set plausible CLICKHOUSE_DATABASE_SCHEME=http
dokku postgres:link plausible-db plausible
dokku clickhouse:link plausible-events-db plausible
Enter fullscreen mode Exit fullscreen mode

Next, we'll slightly adjust the URL that was provided from linking the Clickhouse database and change the name of the environment variable for it as well to match what is expected within the Plausible docker image.

dokku config:set plausible CLICKHOUSE_DATABASE_URL=$(dokku config:get plausible CLICKHOUSE_URL)/plausible
dokku config:unset plausible CLICKHOUSE_URL
Enter fullscreen mode Exit fullscreen mode

Then, we'll generate a secrete key using openssl.

dokku config:set plausible SECRET_KEY_BASE=$(openssl rand -base64 64 | tr -d '\n')
Enter fullscreen mode Exit fullscreen mode

And then, we'll set the BASE_URL environment variable and set the domain for the application — make sure to use your domain here.

dokku config:set plausible BASE_URL=https://analytics.example.com
dokku domains:set plausible analytics.example.com
Enter fullscreen mode Exit fullscreen mode

Set the required SMTP environment variables to set up transactional emails.

dokku config:set plausible MAILER_EMAIL=admin@example.com \
                           SMTP_HOST_ADDR=mail.example.com \
                           SMTP_HOST_PORT=465 \
                           SMTP_USER_NAME=admin@example.com \
                           SMTP_USER_PWD=example1234 \
                           SMTP_HOST_SSL_ENABLED=true
Enter fullscreen mode Exit fullscreen mode

Let's set up our admin account for the application. And then, we can optionally disable registration, this is especially useful if this is just for you and you aren't inviting any clients or other users to utilize it.

dokku config:set plausible ADMIN_USER_EMAIL=admin@example.com \
                           ADMIN_USER_NAME=admin \
                           ADMIN_USER_PWD=password

## This is optional!
dokku config:set plausible DISABLE_REGISTRATION=true
Enter fullscreen mode Exit fullscreen mode

Next, we need to clone (or fork!) the my repository on your local machine.

## Via SSH
git clone git@github.com:kevinlangleyjr/dokku-plausible.git

## Or HTTPS
git clone https://github.com/kevinlangleyjr/dokku-plausible.git
Enter fullscreen mode Exit fullscreen mode

Set the Dokku host as the git remote for the repository and push it on up to your Dokku host!

git remote add dokku dokku@example.com:plausible

git push dokku master
Enter fullscreen mode Exit fullscreen mode

After the initial push and deploy, we can then finally set up the SSL certificate from Let's Encrypt.

Let's start by installing the Let's Encrypt plugin, if you don't already have it installed.

dokku plugin:install https://github.com/dokku/dokku-letsencrypt.git letsencrypt
Enter fullscreen mode Exit fullscreen mode

Then we can set the certificate contact email.

dokku config:set --no-restart plausible DOKKU_LETSENCRYPT_EMAIL=email@example.com
Enter fullscreen mode Exit fullscreen mode

And finally, generate the certificates!

dokku letsencrypt plausible
Enter fullscreen mode Exit fullscreen mode

At this point your instance of Plausible should be accessible via, the domain your provided, which for us is https://analytics.example.com.

Profit!!! 🎉🎉🎉

Top comments (2)

Collapse
 
bugaiov profile image
Nick Bugaiov

Thank you, man. You saved me time!!

Collapse
 
aggyk profile image
Agnes Selvita

This is amazing, thank you for creating this guide Kevin. I am researching for other, preferably self-hosted, analytics tool alternative, and deploying with dokku really is the simplest way.