Plausible Analytics Dashboard

Plausible Analytics Dashboard

什么是Plausible Analytics

Plausible Analytics 是以隐私保护著称的网站统计工具,完全符合 GDPR,CCPA 和 PECR 标准,无 Cookie 追踪用户,脚本十分轻量(<1 kb),是 Google Analytics 的良好替代品

本次示例使用的是 Hetzner CAX21 ARM 架构伺服器来进行示范,Ubuntu 22.04.2 LTS

如果不想看完本教程,可以看官网的:Getting started | Plausible docs

搭建过程

安装Docker

这里不必多说,Docker 和 Docker Compose 都需要安装,看官网的 doc 文档很快就能安装好

Install Docker Engine on Ubuntu | Docker Documentation

从GitHub仓库Clone

git clone https://github.com/plausible/community-edition.git
cd community-edition

下面主要编辑两个文件,docker-compose.ymlplausible-conf.env

配置Docker Compose

请根据需要进行调整设置 Postgres 数据库,ClickHouse 数据库(用于统计数据)和 SMTP 服务器之间安装和网络。请在 DockerHub 上找到最适合你的 Plausible 版本(目前 Plausible 新版已适配 ARM64)

plausible/analytics Tags | Docker Hub

下面是官方提供的 compose 配置文件,plausible/community-edition: Example docker-compose setups for hosting Plausible Community Edition (github.com)

services:
  plausible_db:
    # Plausible v2.1.0 was tested against PostgreSQL versions 15 and 16
    # https://github.com/plausible/analytics/blob/v2.1.0/.github/workflows/elixir.yml#L21-L32
    image: postgres:16-alpine
    restart: always
    volumes:
      - db-data:/var/lib/postgresql/data
    environment:
      - POSTGRES_PASSWORD=postgres

  plausible_events_db:
    image: clickhouse/clickhouse-server:24.3.3.102-alpine
    restart: always
    volumes:
      - event-data:/var/lib/clickhouse
      - event-logs:/var/log/clickhouse-server
      - ./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

  plausible:
    image: ghcr.io/plausible/community-edition:v2.1.0
    restart: always
    command: sh -c "sleep 10 && /entrypoint.sh db createdb && /entrypoint.sh db migrate && /entrypoint.sh run"
    depends_on:
      - plausible_db
      - plausible_events_db
    ports:
      - 127.0.0.1:8000:8000
    env_file:
      - plausible-conf.env

volumes:
  db-data:
    driver: local
  event-data:
    driver: local
  event-logs:
    driver: local

ARM 架构下,官方指定的镜像不适配 ARM 架构,稍作修改 docker-compose.yaml 文件,替换了 SMTP 镜像为 ixdotai/smtp:latest

version: "3.3"
services:
  mail:
    image: ixdotai/smtp:latest
    restart: always

以下是我目前使用的 docker-comopose.yaml 配置,有配置 MaxMind 的数据源

version: "3.3"

services:
  mail:
    image: ixdotai/smtp:latest
    restart: always
    networks:
      - default

  plausible_db:
    # supported versions are 12, 13, and 14
    image: postgres:15.7
    restart: always
    volumes:
      - db-data:/var/lib/postgresql/data
    environment:
      - POSTGRES_PASSWORD=postgres
    networks:
      - default

  plausible_events_db:
    image: clickhouse/clickhouse-server:latest
    restart: always
    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
    networks:
      - default

  plausible:
    image: plausible/community-edition:latest
    restart: always
    command: sh -c "sleep 10 && /entrypoint.sh db createdb && /entrypoint.sh db migrate && /entrypoint.sh run"
    depends_on:
      - plausible_db
      - plausible_events_db
      - mail
      - geoip
    ports:
      - 8000:8000
    environment:
      - GEOLITE2_COUNTRY_DB=/geoip/GeoLite2-City.mmdb
    env_file:
      - plausible-conf.env
    volumes:
      - geoip:/geoip:ro
    networks:
      - default

  geoip:
    image: maxmindinc/geoipupdate:latest
    restart: always
    environment:
      - 'GEOIPUPDATE_EDITION_IDS=GeoLite2-City GeoLite2-Country GeoLite2-ASN'
      - GEOIPUPDATE_FREQUENCY=168 # update every 7 days
    env_file:
      geoip/geoip.conf
    volumes:
      - geoip:/usr/share/GeoIP
    networks:
      - default

volumes:
  db-data:
    driver: local
  event-data:
    driver: local
  geoip:
    driver: local

networks:
  default:
    ipam:
      config:
        - subnet: 172.20.0.0/16

配置环境变量

plausible-conf.env

先用以下方法生成 base64 随机密钥,然后编辑 plausible-conf.env 文件

openssl rand -base64 64 | tr -d '\n' ; echo
vim plausible-conf.env

新版本中已弃用 admin 的设置,Remove admin creation · plausible/hosting@7a3a3d2 (github.com)。我使用的环境如下,包括配置 SMTP 和 Google Search 集成

#ADMIN_USER_EMAIL=此处设置管理员邮箱
#ADMIN_USER_NAME=此处设置管理员用户名
#ADMIN_USER_PWD=此处设置管理员密码
BASE_URL=https://stats.microcharon.dev #此处设置你的Plausible管理地址
SECRET_KEY_BASE=NZLdRqYHZRYObdqbBEoPDV+xE74/BmRvifXK7VUJ/hAoXYcyfRuPcgR97PnCPMGXaEU+HVtiMOMzBCDDmazHPQ== #此处设置base64生成的随机字符
TOTP_VAULT_KEY=+J5qm+UKq9X7DQGpDc8hpLbMx5czQvgVFQKvoDUT+j4=
ENABLE_EMAIL_VERIFICATION=true #此处启用邮件验证
DISABLE_REGISTRATION=invite_only #此处是设置注册类型

#SMTP Config
[email protected]
MAILER_NAME=Microcharon Stats Dev
SMTP_HOST_ADDR=smtp.outlook.com
SMTP_HOST_PORT=587
[email protected]
SMTP_USER_PWD=your-password
SMTP_HOST_SSL_ENABLED=false
SMTP_RETRIES=2
MAILER_ADAPTER=Bamboo.Mua

#Google Search Integration
GOOGLE_CLIENT_ID=your-google-client_id.apps.googleusercontent.com
GOOGLE_CLIENT_SECRET=GOCSPX-your-client-secret

geoip.conf

如果选择数据源为 MaxMind,则需要修改 geoip 目录下的 geoip.conf

GEOIPUPDATE_ACCOUNT_ID=889321
GEOIPUPDATE_LICENSE_KEY=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
MAXMIND_EDITION=GeoLite2-City #MaxMind 数据库版本,GeoLite2-City 指数据精确到城市

启动服务

注意是在该目录下启动服务,别搞错目录

cd hosting
docker compose up -d

检查端口是否开放,以及安全组是否放行,默认端口是8000,如果这一切都没问题,然后就可以用 http://your-ip:8000 即可访问你的自托管 Plausible 了

反向代理

NGINX反代

检查 X-Forwarded-For 标头设置正确,如果不正确的话,跟踪用户所在国家时就会导致测出不准确的数据

location / {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-NginX-Proxy true;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_redirect off;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_connect_timeout 300;
    proxy_send_timeout 300;
    proxy_read_timeout 300;
    send_timeout 300;
    proxy_pass http://127.0.0.1:8000;
}

Apache反代

hosting/reverse-proxy/apache2/plausible.conf at master · plausible/hosting · GitHub

<VirtualHost *:80>

 ServerAdmin [email protected]
 ServerName example.com

 ProxyPreserveHost On
 ProxyPass / http://localhost:8000/
 ProxyPassReverse / http://localhost:8000/

 SetEnvIf X-Forwarded-For "^.*\..*\..*\..*" forwarded
 LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
 LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" forwarded

 ErrorLog ${APACHE_LOG_DIR}/error.log
 CustomLog ${APACHE_LOG_DIR}/access.log combined env=!forwarded
 CustomLog ${APACHE_LOG_DIR}/access.log forwarded env=forwarded
  
</VirtualHost>

更多反代示例可在目录中找到,此处不写

添加脚本

直接将 JavaScript 脚本片段添加到 head 头部或 body 尾部处即可

<script defer data-domain="microcharon.com" src="https://stats.microcharon.dev/js/plausible.js"></script>

备份及恢复数据

其实最麻烦的是数据的问题,Plausible 虽然开源,但也分商业付费版的和自托管版的,托管在 Plausible 的就无需担忧备份问题,而自托管的有必要经常备份,防止数据丢失

有一名全栈开发人员已经帮我们解决了这个问题

Backup & Restore Docker Named Volumes | by Jarek Lipski | Medium

loomchild/volume-backup: docker volume backup & restore utility (github.com)

语法示例

#备份存储卷
docker run -v [volume-name]:/volume --rm --log-driver none loomchild/volume-backup backup > [archive-path]

#恢复存储卷
docker run -i -v [volume-name]:/volume --rm loomchild/volume-backup restore < [archive-path]

实际应用

备份相关数据,plausible_db-data 和 plausible_event-data 数据卷必须要备份

#暂时暂停相关 container
docker compose stop

#备份 plausible_db-data, plausible_event-data 存储卷到当前目录
docker run -v plausible_db-data:/volume --rm --log-driver none loomchild/volume-backup backup > backup_plausible_db-data.tar.bz2

docker run -v plausible_event-data:/volume --rm --log-driver none loomchild/volume-backup backup > backup_plausible_event-data.tar.bz2

#重新启动相关 container
docker compose start

删除相关 container 及 volume

#暂停相关 container
docker compose stop

#删除相关 container
docker compose rm

#删除相关 volume
docker volume rm plausible_db-data plausible_event-data

恢复相关数据,恢复 plausible_db-data 和 plausible_event-data 数据卷

#暂停相关 container
docker compose stop

#恢复 plausible_db-data, plausible_event-data 存储卷
docker run -i -v plausible_db-data:/volume --rm loomchild/volume-backup restore < backup_plausible_db-data.tar.bz2

docker run -i -v plausible_event-data:/volume --rm loomchild/volume-backup restore < backup_plausible_event-data.tar.bz2

#恢复相关 container
docker compose start

或是参照其它备份方法:Backup Docker Installation · plausible/analytics · Discussion #1132 (github.com)

参考资料

Getting started | Plausible docs

ARM architecture support · Discussion #903 · plausible/analytics (github.com)

Backup Docker Installation · plausible/analytics · Discussion #1132 (github.com)

Install Docker Engine on Ubuntu | Docker Documentation

maxmindinc/geoipupdate - Docker Image | Docker Hub

geoipupdate/doc/docker.md at main · maxmind/geoipupdate · GitHub

Unable to see Regions and Cities data · plausible/analytics · Discussion #2886 (github.com)

最后修改:2024 年 05 月 23 日
如果觉得我的文章对你有用,请随意赞赏