733 字
4 分钟

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:

Terminal window
mkdir -p /opt/plausible
cd /opt/plausible

First, create a docker-compose.yaml file and adjust the parameters as needed:

Terminal window
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: local

Next, in the same directory, create the geoip folder and the plausible-conf.env file:

Terminal window
mkdir -p geoip
touch plausible-conf.env
touch geoip.env

Edit 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:

Terminal window
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=true
GEOLITE2_COUNTRY_DB=/geoip/GeoLite2-Country.mmdb

SECRET_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:

Terminal window
GEOIPUPDATE_EDITION_IDS=GeoLite2-Country
GEOIPUPDATE_FREQUENCY=168
GEOIPUPDATE_ACCOUNT_ID=你的 Account ID
GEOIPUPDATE_LICENSE_KEY=你的 License Key

Now pull the images and start the stack:

Terminal window
docker-compose pull
docker-compose up -d

Once 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:

Terminal window
docker-compose pull
docker-compose up -d
docker system prune

Backing up Plausible Analytics#

Backup#

backup.sh
#!/usr/bin/env sh
docker-compose stop
docker run --rm -v plausible_db-data:/volume -v `pwd`:/backup loomchild/volume-backup backup plausible_db-data
docker run --rm -v plausible_event-data:/volume -v `pwd`:/backup loomchild/volume-backup backup plausible_event-data
docker-compose start -d

Restore script:

restore.sh
#!/usr/bin/env sh
docker-compose stop
docker run --rm -v plausible_db-data:/volume -v `pwd`:/backup loomchild/volume-backup restore -f plausible_db-data
docker run --rm -v plausible_event-data:/volume -v `pwd`:/backup loomchild/volume-backup restore -f plausible_event-data
docker-compose start -d

Uninstalling Plausible Analytics#

Terminal window
docker-compose down
rm -rf /opt/plausible
docker image rm postgres:12
docker image rm maxmindinc/geoipupdate:latest
docker image rm plausible/analytics:latest
docker image rm yandex/clickhouse-server:21.3.2.5
docker image rm bytemark/smtp:latest
docker volume rm plausible_db-data
docker volume rm plausible_event-data

Adding 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:

Terminal window
['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:

Terminal window
['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:

Terminal window
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.

Self-Hosting Plausible Analytics with Docker on Debian 11
https://catcat.blog/en/debian11-docker-plausible-analytics.html
作者
猫猫博客
发布于
2023-02-26
许可协议
CC BY-NC-SA 4.0