Running Matomo & GlitchTip with Dokku
Tags: dokku, matomo, glitchtip
Published: 2023-11-07 20:22:32

I've been meaning to set up analytics and error reporting for my sites for a while now, and finally got around to it today. I'd heard Dokku was a good way to deploy resources like this, and while it worked pretty well in the end, some of the examples for these apps were quite dated. 

Thought I'd share my new instructions on these.

## Matomo

https://github.com/rclement/dokku-matomo

- I originally found this guide, but some of the instructions are dated and gave me trouble. Below should be a more accurate guide on running Matomo with the current version of Dokku.

### Deploy your own instance of Matomo on Dokku.

This setup is makes use of the great ready-to-use [matomo-docker](https://github.com/crazy-max/docker-matomo) image by [crazy-max](https://github.com/crazy-max), which does all of the heavy-lifting to properly deploy Matomo without too much headache.

Requirements
	

- [Dokku](https://github.com/dokku/dokku)
- [dokku-mariadb](https://github.com/dokku/dokku-mariadb)
- [dokku-letsencrypt](https://github.com/dokku/dokku-letsencrypt)

App and database

First create a new Dokku app. We will call it matomo.

```bash
dokku apps:create matomo
```

Next create the MariaDB database required by Matomo and link it to the app.

```bash
dokku mariadb:create mariadb-matomo
dokku mariadb:link mariadb-matomo matomo
```

### Configuration

Main configuration

```bash
dokku config:set --no-restart matomo TZ=America/Chicago
dokku config:set --no-restart matomo MEMORY_LIMIT=256M
dokku config:set --no-restart matomo UPLOAD_MAX_SIZE=16M
dokku config:set --no-restart matomo OPCACHE_MEM_SIZE=128
dokku config:set --no-restart matomo REAL_IP_FROM=0.0.0.0/32
dokku config:set --no-restart matomo REAL_IP_HEADER=X-Forwarded-For
dokku config:set --no-restart matomo LOG_LEVEL=WARN
```

### Persistent storage

You need to mount a volume on your host (the machine running Dokku) to persist all settings that you set in the Matomo interface.

```bash
mkdir /var/lib/dokku/data/storage/matomo
# UID:GUID are set to 101. These are the values the nginx image uses,
# that is used by crazymax/matomo
chown 101:101 /var/lib/dokku/data/storage/matomo
dokku storage:mount matomo /var/lib/dokku/data/storage/matomo:/data
```

### Domain setup

To get the routing working, we need to apply a few settings. First we set the domain.

```bash
dokku domains:add matomo matomo.example.com
```

We also need to update the ports set by Dokku.

```bash
dokku ports:set matomo http:80:8000
dokku ports:remove matomo http:80:5000
```

If Dokku proxy:report shows more than one port mapping, remove all port mappings except the added above.

### Deploy app for the first time

Deploy Matomo from latest version of the docker image

```bash
dokku git:from-image matomo crazymax/matomo:latest
```

Setup Let's Encrypt

Setup an SSL certificate via Let's Encrypt.

```bash
dokku letsencrypt:set matomo email user@domain.com
dokku letsencrypt:enable matomo
dokku letsencrypt:auto-renew matomo
```

### Grep MariaDB information for the setup

We will need to set up Matomo in the web interface and provide the database details. You should be able to access the page via https://matomo.example.com.

Run the command below to retrieve the DSN.

```bash
dokku mariadb:info mariadb-matomo
```

An example DSN might look like this: mysql://mariadb:ffd4fc238ba8adb3@dokku-mariadb-mariadb-matomo:3306/mariadb_matomo. Copy and paste the details as follows:

Hostname: dokku-mariadb-mariadb-matomo
Username: mariadb
Password: ffd4fc238ba8adb3
Database Name: mariadb_matomo

After going through the setup, you should be able to use Matomo.

## GlitchTip
I was originally planning to use Sentry, but they've really made it a pain to self-host. They pretty much require you to use docker compose, which Dokku doesn't appear to have solid support for. Or at least, documentation.

I would probably need a dedicated machine or proper Docker environment to run it alongside other applications such as Matomo.
So I looked for lighter weight error reporting applications and came across [GlitchTip](https://glitchtip.com/), which seems to fit the bill.

Didn't see any Dokku instructions for this one, but the following worked for me:

### Install Dokku plugins

Go to your dokku server and install following plugins:

Install official postgresql plugin

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

Install official redis plugin

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

### Prepare dokku

Create dokku app

```bash
dokku apps:create glitchtip
```

Create postgresql db and link it to the app

```bash
dokku postgres:create glitchtip
dokku postgres:link glitchtip glitchtip
```

Create redis instance and link it to the app

```bash
dokku redis:create glitchtip
dokku redis:link glitchtip glitchtip
```

### Set [required variables](https://glitchtip.com/documentation/install#configuration) for GlitchTip

```bash
dokku config:set --no-restart glitchtip SECRET_KEY=$(echo `openssl rand -base64 64` | tr -d ' ')
dokku config:set --no-restart glitchtip GLITCHTIP_DOMAIN=https://glitchtip.example.com
dokku config:set --no-restart glitchtip GLITCHTIP_DOMAIN="smtp://email:password@smtp_url:port"
```

### Domain setup


```bash
dokku domains:add glitchtip glitchtip.example.com
```

We also need to update the ports set by Dokku.

```bash
dokku ports:set glitchtip http:80:8080
dokku ports:remove glitchtip http:80:5000
```

If Dokku proxy:report shows more than one port mapping, remove all port mappings except the added above.

### Deploy

Deploy Matomo from their docker image

```bash
dokku git:from-image glitchtip glitchtip/glitchtip
```

### Setup Let's Encrypt

Setup an SSL certificate via Let's Encrypt.

```bash
dokku letsencrypt:set glitchtip email user@domain.com
dokku letsencrypt:enable glitchtip
dokku letsencrypt:auto-renew glitchtip
```

You should then be able to access glitchtip.example.com and create the first user account.