GoatCounter 轻量统计工具部署教程
GoatCounter 是一款用 Go 语言编写的轻量级网站统计工具,专注于隐私保护和简洁设计。它不使用 Cookie,不追踪个人身份信息,脚本体积仅约 3.5KB,对网站性能几乎没有影响。GoatCounter 使用 SQLite 作为数据库,部署极其简单,是博客和小型网站的理想统计方案。本文将在搬瓦工 VPS 上从零部署 GoatCounter。
一、系统要求
- 操作系统:Linux(Ubuntu 22.04 推荐)。
- 内存:256MB 即可运行(极其轻量)。
- 磁盘:GoatCounter 二进制文件不到 20MB。
二、下载和安装
2.1 下载预编译二进制文件
# 下载最新版本
cd /tmp
wget https://github.com/arp242/goatcounter/releases/download/v2.5.0/goatcounter-v2.5.0-linux-amd64.gz
gunzip goatcounter-v2.5.0-linux-amd64.gz
mv goatcounter-v2.5.0-linux-amd64 /usr/local/bin/goatcounter
chmod +x /usr/local/bin/goatcounter
# 验证安装
goatcounter version
2.2 创建数据目录
mkdir -p /opt/goatcounter
cd /opt/goatcounter
2.3 创建站点
goatcounter db create site \
-vhost stats.example.com \
-user.email admin@example.com \
-user.password "你的强密码" \
-db sqlite+/opt/goatcounter/goatcounter.db
三、运行 GoatCounter
3.1 创建 Systemd 服务
cat > /etc/systemd/system/goatcounter.service << 'EOF'
[Unit]
Description=GoatCounter web analytics
After=network.target
[Service]
Type=simple
User=www-data
Group=www-data
WorkingDirectory=/opt/goatcounter
ExecStart=/usr/local/bin/goatcounter serve \
-listen localhost:8081 \
-tls none \
-db sqlite+/opt/goatcounter/goatcounter.db \
-automigrate
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target
EOF
chown -R www-data:www-data /opt/goatcounter
systemctl daemon-reload
systemctl enable goatcounter
systemctl start goatcounter
3.2 配置 Nginx 反向代理
server {
listen 80;
listen 443 ssl http2;
server_name stats.example.com;
ssl_certificate /etc/letsencrypt/live/stats.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/stats.example.com/privkey.pem;
location / {
proxy_pass http://127.0.0.1:8081;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
# 缓存统计脚本
location = /count.js {
proxy_pass http://127.0.0.1:8081;
expires 30d;
add_header Cache-Control "public";
}
}
nginx -t && systemctl reload nginx
四、添加追踪代码
在要监控的网站的 HTML 中添加以下代码:
<script data-goatcounter="https://stats.example.com/count"
async src="https://stats.example.com/count.js"></script>
GoatCounter 的追踪脚本不使用 Cookie,不追踪用户个人信息,完全符合 GDPR 要求,无需显示 Cookie 同意弹窗。
五、高级功能
5.1 自定义事件追踪
// JavaScript 手动发送自定义事件
if (window.goatcounter) {
window.goatcounter.count({
path: 'button-click-购买',
title: '购买按钮点击',
event: true,
});
}
5.2 API 数据导出
# 使用 GoatCounter API 获取统计数据
curl -H "Authorization: Bearer YOUR_API_TOKEN" \
"https://stats.example.com/api/v0/stats/total?start=2026-03-01&end=2026-03-28"
# 导出完整数据
goatcounter db export csv \
-db sqlite+/opt/goatcounter/goatcounter.db \
> /root/backups/goatcounter_export.csv
5.3 公开统计页面
GoatCounter 支持将统计数据设为公开可见,这对于开源项目特别有用。在设置页面中启用"公开统计"选项即可。
六、数据备份
GoatCounter 使用 SQLite 数据库,备份只需复制一个文件:
# 备份数据库
cp /opt/goatcounter/goatcounter.db /root/backups/goatcounter_$(date +%Y%m%d).db
# 设置定时备份
echo "0 3 * * * cp /opt/goatcounter/goatcounter.db /root/backups/goatcounter_\$(date +\%Y\%m\%d).db" | crontab -
七、与其他统计工具对比
- GoatCounter vs Matomo:GoatCounter 更轻量,适合个人和小型网站;Matomo 功能更全面,适合企业级需求。
- GoatCounter vs Ackee:GoatCounter 使用 SQLite 更简单;Ackee 使用 MongoDB 功能更丰富。
- GoatCounter vs Fathom:两者理念相似;GoatCounter 完全开源免费,Fathom Lite 开源但功能有限。
总结
GoatCounter 是极其轻量且注重隐私的网站统计工具,内存占用极低,使用 SQLite 无需额外的数据库服务,部署在搬瓦工 VPS 上几乎不占用资源。它特别适合博客、个人网站和开源项目。如果你需要更全面的分析功能,可以参考 Matomo 分析平台 或 Fathom 隐私分析工具。选购搬瓦工 VPS 请查看 全部方案,购买时使用优惠码 NODESEEK2026 可享受 6.77% 的折扣,通过 bwh81.net 进入官网购买。