Self host Plausible analytics
I already have a Hetzner VPS running ubuntu and Caddy reverse proxy serving this very website. By leaving Vercel I lost their analytics - and now I want them back. Plausible analytics should do the trick!
Prerequisities
- Ubuntu server
- Caddy running
docker
anddocker-compose
installed
Run Plausible CE
Their website doesn’t make it terribly obvious, but they regard Plausible and Plausible Community Edition as two separate things. What we are after is the Plausible Community Edition - I followed their quick start guide.
git clone -b v2.1.4 --single-branch --depth 1 https://github.com/plausible/community-edition plausible-ce
cd plausible-ce
echo "BASE_URL=https://analytics.vojtechstruhar.com" >> .env
echo "SECRET_KEY_BASE=$(openssl rand -base64 48)" >> .env
I did not modify on what ports does the service run. The default port in docker is 8000 and that’s fine by me. I want it to be accessible on port 5001. We can specify this binding in a compose.override.yml
file:
services:
plausible:
ports:
- 5001:8000
Now run the container
docker compose up -d
Setup a domain
I added analytics.vojtechstruhar.com
to my Hetzner DNS. It would be nice if they supported wildcard A records, but they don’t.
Point Caddy to the new service
All I have to do here is add a reverse proxy to my Caddyfile
analytics.vojtechstruhar.com {
reverse_proxy localhost:5001
}
And of course restart caddy so that the changes take effect
systemctl reload caddy
Setup Plausible tracking
Now you can go to your domain and setup the analytics. It will give you a snippet, which I just pasted into <head>
in my base Astro layout component. And voila!