Self-Hosting Plausible Analytics With Dokku
Tags: docker, analytics, devops, deploy
Published: 2021-12-13 00:29:45

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](https://plausible.io/docs/self-hosting) 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](https://dokku.com) 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](https://heroku.com). Under the hood, Dokku is powered by [Docker](https://docker.com/), uses [Heroku buildpacks](https://devcenter.heroku.com/articles/buildpacks) by default, and has a number of official and community [plugins](https://dokku.com/docs/community/plugins/).

There is a [one-click](https://marketplace.digitalocean.com/apps/dokku) 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](https://kevinlangleyjr.dev/blog/easy-app-deployments-and-management-with-dokku) 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](https://gdpr.eu/), [CCPA](https://oag.ca.gov/privacy/ccpa), and [PECR](https://ico.org.uk/for-organisations/guide-to-pecr/what-are-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](https://plausible.io/docs/self-hosting) from their [documentation](https://plausible.io/docs/) 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](http://dokku.viewdocs.io/dokku/getting-started/installation/).

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:

```bash
dokku apps:create plausible
```

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

```bash
dokku plugin:install https://github.com/dokku/dokku-postgres.git postgres
dokku plugin:install https://github.com/dokku/dokku-clickhouse.git clickhouse
```

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

```bash
dokku postgres:create plausible-db
dokku clickhouse:create plausible-events-db
```

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.

```bash
dokku config:set plausible CLICKHOUSE_DATABASE_SCHEME=http
dokku postgres:link plausible-db plausible
dokku clickhouse:link plausible-events-db plausible
```

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.

```bash
dokku config:set plausible CLICKHOUSE_DATABASE_URL=$(dokku config:get plausible CLICKHOUSE_URL)/plausible
dokku config:unset plausible CLICKHOUSE_URL
```

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

```bash
dokku config:set plausible SECRET_KEY_BASE=$(openssl rand -base64 64 | tr -d '\n')
```

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

```bash
dokku config:set plausible BASE_URL=https://analytics.example.com
dokku domains:set plausible analytics.example.com
```

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

```bash
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
```

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.

```bash
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
```

Next, we need to clone (or fork!) the my [repository](https://github.com/kevinlangleyjr/dokku-plausible) on your local machine.

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

## Or HTTPS
git clone https://github.com/kevinlangleyjr/dokku-plausible.git
```

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

```bash
git remote add dokku dokku@example.com:plausible

git push dokku master
```

After the initial push and deploy, we can then finally set up the SSL certificate from [Let's Encrypt](https://letsencrypt.org/).

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

```bash
dokku plugin:install https://github.com/dokku/dokku-letsencrypt.git letsencrypt
```

Then we can set the certificate contact email.

```bash
dokku config:set --no-restart plausible DOKKU_LETSENCRYPT_EMAIL=email@example.com
```

And finally, generate the certificates!

```bash
dokku letsencrypt plausible
```

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

Profit!!!  🎉🎉🎉