Self-Hosting Plausible Analytics with Docker on Debian 11
Original post at u.sb
This post walks through how to install Plausible Analytics with Docker on Debian 11 to self-host your website analytics.
PS: The same steps also apply to any Linux distribution that can run Docker.
Why self-host your analytics?
The reason is simple: you should own your site data. Do you really want all your website data to be sold to so‑called “big data” companies by third parties?
Plausible Analytics is a privacy‑focused web analytics platform. After testing it for several months, it has proven sufficient for almost all needs and can replace commercial products such as Google Analytics.
Installing Plausible Analytics
It’s recommended to install it under the /opt/plausible directory:
mkdir -p /opt/plausiblecd /opt/plausibleFirst, create a docker-compose.yaml file and adjust the parameters as needed:
version: "3.8"services: mail: image: bytemark/smtp restart: always
plausible_db: image: postgres:12 volumes: - db-data:/var/lib/postgresql/data environment: - POSTGRES_PASSWORD=postgres restart: always
plausible_events_db: image: yandex/clickhouse-server:21.3.2.5 volumes: - event-data:/var/lib/clickhouse - ./clickhouse/clickhouse-config.xml:/etc/clickhouse-server/config.d/logging.xml:ro - ./clickhouse/clickhouse-user-config.xml:/etc/clickhouse-server/users.d/logging.xml:ro ulimits: nofile: soft: 262144 hard: 262144 restart: always
plausible: image: plausible/analytics:latest command: sh -c "sleep 10 && /entrypoint.sh db createdb && /entrypoint.sh db migrate && /entrypoint.sh db init-admin && /entrypoint.sh run" depends_on: - plausible_db - plausible_events_db - mail - geoip volumes: - ./geoip:/geoip:ro ports: - 127.0.0.1:8000:8000 env_file: - plausible-conf.env restart: always
geoip: image: maxmindinc/geoipupdate env_file: - geoip.env volumes: - ./geoip:/usr/share/GeoIP
volumes: db-data: driver: local event-data: driver: local geoip: driver: localNext, in the same directory, create the geoip folder and the plausible-conf.env file:
mkdir -p geoiptouch plausible-conf.envtouch geoip.envEdit plausible-conf.env and configure it following the official guide. Suppose your URL is https://stat.example.com/, an example configuration would look like this:
ADMIN_USER_EMAIL=管理员邮箱ADMIN_USER_NAME=管理员用户名ADMIN_USER_PWD=管理员密码BASE_URL=https://stat.example.com/SECRET_KEY_BASE=随机字符MAILER_EMAIL=网站通知邮箱SMTP_HOST_ADDR=SMTP 主机名SMTP_HOST_PORT=SMTP 端口SMTP_USER_NAME=SMTP 用户名SMTP_USER_PWD=SMTP 密码DISABLE_REGISTRATION=trueGEOLITE2_COUNTRY_DB=/geoip/GeoLite2-Country.mmdbSECRET_KEY_BASE needs to be a 64‑character random string. You can generate one using openssl rand -base64 64 or pwgen 64.
Set DISABLE_REGISTRATION to true to disable user signups.
You can use any email delivery provider for SMTP. For a quick option, you can simply use free services like Gmail, or you can self‑host Mailcow for your own use; see the tutorial here.
Next, register an account with Maxmind. After registration, go to Account > Manage License Keys in the left menu, click Generate new license key to obtain a License Key, and make a note of both your Account ID and the License Key.
Then edit geoip.env and fill it in like this:
GEOIPUPDATE_EDITION_IDS=GeoLite2-CountryGEOIPUPDATE_FREQUENCY=168GEOIPUPDATE_ACCOUNT_ID=你的 Account IDGEOIPUPDATE_LICENSE_KEY=你的 License KeyNow pull the images and start the stack:
docker-compose pulldocker-compose up -dOnce it has started, you can access Plausible at http://127.0.0.1:8000/. If you want to expose it publicly, you’ll also need to configure an Nginx reverse proxy.
After reloading Nginx, you should be able to visit https://stat.example.com/.
Upgrading Plausible Analytics
The universal Docker upgrade workflow:
docker-compose pull
docker-compose up -d
docker system pruneBacking up Plausible Analytics
Backup
#!/usr/bin/env sh
docker-compose stopdocker run --rm -v plausible_db-data:/volume -v `pwd`:/backup loomchild/volume-backup backup plausible_db-datadocker run --rm -v plausible_event-data:/volume -v `pwd`:/backup loomchild/volume-backup backup plausible_event-datadocker-compose start -dRestore script:
#!/usr/bin/env sh
docker-compose stopdocker run --rm -v plausible_db-data:/volume -v `pwd`:/backup loomchild/volume-backup restore -f plausible_db-datadocker run --rm -v plausible_event-data:/volume -v `pwd`:/backup loomchild/volume-backup restore -f plausible_event-datadocker-compose start -dUninstalling Plausible Analytics
docker-compose downrm -rf /opt/plausibledocker image rm postgres:12docker image rm maxmindinc/geoipupdate:latestdocker image rm plausible/analytics:latestdocker image rm yandex/clickhouse-server:21.3.2.5docker image rm bytemark/smtp:latestdocker volume rm plausible_db-datadocker volume rm plausible_event-dataAdding to WordPress
Directly edit the header.php file of the theme you’re using and add the tracking script right after <?php wp_head(); ?>.
If you prefer not to modify your theme, you can simply install the official plugin.
Adding to VuePress
If you’re using VuePress v1.x, edit .vuepress/config.js and add the following under module.exports:
['script', {}, `const script = document.createElement('script');script.async = true;script.defer = true;script['data-domain'] = '统计域名';script.src = 'https://stat.example.com/js/plausible.js';document.head.appendChild(script);`],If you’re using VuePress v2.x, edit .vuepress/config.ts and add the following under export default:
['script', {}, `const script = document.createElement('script');script.async = true;script.defer = true;script['data-domain'] = '统计域名';script.src = 'https://stat.example.com/js/plausible.js';document.head.appendChild(script);`],Adding to Next.js
Install the next-plausible package and use code similar to the following:
import PlausibleProvider from 'next-plausible'
export default function MyApp({ Component, pageProps }) { return ( <PlausibleProvider domain="统计域名" customDomain="https://stat.example.com" selfHosted> <Component {...pageProps} /> </PlausibleProvider> )}For more integration options, refer to the official docs.