Programming

How to run Plausible Analytics on your Raspberry Pi

How to run Plausible Analytics on your Raspberry Pi

Self hosting Plausible Analytics

Plausible Analytics is a great alternative to established analytics tools such as Google Analytics, Matomo and others. It is privacy-oriented, open source, and can be self-hosted. More details about their analytics services on their webpage.

I was curious to see how this works and wanted to self host it on my Raspberry Pi that I have lying around. I thought that would work straightforward until I read this discussion, which mentions that ClickHouse does not provide a docker image for ARM, so Plausible Analytics does not work out of the box on ARM architectures. However, one can build the docker images for the ARM architecture required by all the software used by Plausible Analytics and run the whole thing on a Raspberry Pi. Let’s see how.

Building the Docker Images

Here are the steps I did to run Plausible Analytics on my Raspberry Pi 3. SSH into your Raspberry Pi and then go to a folder where you can store the repositories and files needed. We shall build the docker images (I assume you already have installed docker and docker-compose) for the ClickHouse database, the Bytemark/SMTP mail service, the Plausible Analytics, and finally for the caddy-gen reverse proxy service. I built everything using a fresh Ubuntu Server 20.04 LTS installation on 32GB USB Flash Drive. The total space occupied is around 8GB, so make sure you have enough space on your SD/USB/HDD before starting.

ClickHouse database

  1. Make a ClickHouse folder
  2. Get the Dockerfile from ClickHouse repository and the dependent files
  3. Build docker image for ARM64 as per the Dockerfile instructions
mkdir ClickHouse && cd ClickHouse
wget https://raw.githubusercontent.com/ClickHouse/ClickHouse/master/docker/server/Dockerfile && 
wget https://raw.githubusercontent.com/ClickHouse/ClickHouse/master/docker/server/docker_related_config.xml &&
wget https://raw.githubusercontent.com/ClickHouse/ClickHouse/master/docker/server/entrypoint.sh &&
wget https://raw.githubusercontent.com/ClickHouse/ClickHouse/master/docker/server/local.Dockerfile 

docker build . --network host --build-arg single_binary_location_url="https://builds.clickhouse.tech/master/aarch64/clickhouse" -t clickhouse/clickhouse-server

We have the docker image for ClickHouse database. Now let’s focus on the Analytics part.

Plausible Analytics

  1. Make a folder called plausible.
  2. Clone the Plausible/Analytics repo
  3. Build docker image
mkdir plausible && cd plausible
git clone https://github.com/plausible/analytics.git

cd analytics

docker build -t plausible/analytics . 

Bytemark SMTP server

The original docker-compose file that is used to start all containers requires a bytemark/smtp mail service. However, there is only a x86_64 image avaialble on Docker Hub, thus we need to build this docker image as well to work on our ARM architecture.

git clone https://github.com/BytemarkHosting/docker-smtp.git

cd docker-smtp/stretch
docker build -t bytemark/smtp . 

Glueing all together

Plausible offers a docker-compose file to start all the containers. Go ahead to your plausible folder and clone the hosting repository

cd plausible
git clone https://github.com/plausible/hosting.git

We need to modify a few things in that docker-compose.yml file.

  • change the image for mail service as following: image: bytemark/smtp:latest
  • change the image for plausible_events_db to image: clickhouse/clickhouse-server:latest

Next, we need to set up the parameters in plausible-conf.env as per the instructions.

Now we’re ready to start the containers. Go to the hosting folder and run docker-compose up -d.

Reverse proxy with caddy-gen

In the hosting repository there is also a folder called reverse-proxy. In that folder there is a docker-compose.caddy-gen.yml file that we can use to start a container for a reverse proxy. However, there is only a x86_64 image available, thus we need to build caddy-gen for our ARM architecture. Here are the steps.

  1. Clone the caddy-gen repository.
  2. Go to the caddy-gen folder
  3. Modify the Dockerfile as explained below, as we need to add support for Go as the forego binary that is being used in the Dockerfile is not compiled for ARM
git clone https://github.com/wemake-services/caddy-gen.git
cd caddy-gen

Open the Dockerfile with a text editor and add the following lines after line 8: ENV DOCKER_HOST="unix:///tmp/docker.sock"

# Install GO
RUN apk add --no-cache git make musl-dev go

# Configure Go
ENV GOROOT /usr/lib/go
ENV GOPATH /go
ENV PATH /go/bin:$PATH

RUN mkdir -p ${GOPATH}/src ${GOPATH}/bin

# Install Forego
RUN go get -u github.com/jwilder/forego &&
    cp /go/bin/forego /usr/bin/forego &&
    chmod u+x /usr/bin/forego
  1. Comment out or delete the following code portion in the caddy-gen/Dockerfile as forego is only compiled for x86_64, so we need to actually build/install it from sources (see above)
# Install Forego
# && wget --quiet "https://github.com/jwilder/forego/releases/download/v${FOREGO_VERSION}/forego" \
# && mv ./forego /usr/bin/forego \
# && chmod u+x /usr/bin/forego \
  1. Change the docker-gen architecture to be ARM64 instead of AMD64 – docker-gen-alpine-linux-amd64 should become docker-gen-alpine-linux-arm64,

Save the changes to the Dockerfile and run docker build -t caddy-gen . Once finished, open the reverse-proxy/docker-compose.caddy-gen.yml file and change the image to image: caddy-gen:latest and fill in the plausible labels with your information. Save changes and exit the editor.

For running the reverse proxy, here are the full instructions

Run docker-compose -f docker-compose.yml -f reverse-proxy/docker-compose.caddy-gen.yml up from the top level of the hosting folder and we should have all the things running.

Troubleshooting

I had some freezing issues when running Ubuntu Server 20.04 LTS with the 16GB uSD card, thus I switched to using a 32GB USB Flash Drive. This worked much better and I had almost no issues at all. Make sure you have enough GB left (~8GB) on your Raspberry Pi.