2025.01.14 更新了使用docker安装的matomo的迁移过程。
docker部署适合想快速测试功能或不想花费更多时间配置环境的用户。
安装docker和docker compose
wget -qO- get.docker.com | bash
systemctl enable docker
sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
安装nginx proxy manager
mkdir docker docker/npm
cd docker/npm
vim docker-compose.yml
输入保存以下内容
version: '3.8'
services:
app:
image: 'jc21/nginx-proxy-manager:latest'
restart: unless-stopped
ports:
# These ports are in format <host-port>:<container-port>
- '80:80' # Public HTTP Port
- '443:443' # Public HTTPS Port
- '81:81' # Admin Web Port
# Add any other Stream port you want to expose
# - '21:21' # FTP
# Uncomment the next line if you uncomment anything in the section
# environment:
# Uncomment this if you want to change the location of
# the SQLite DB file within the container
# DB_SQLITE_FILE: "/data/database.sqlite"
# Uncomment this if IPv6 is not enabled on your host
# DISABLE_IPV6: 'true'
volumes:
- ./data:/data
- ./letsencrypt:/etc/letsencrypt
docker compose up -d
默认管理员用户,登录ip:81
后修改:
Email: admin@example.com
Password: changeme
添加proxy hosts,绑定域名到npm,开启ssl
安装matomo
mkdir /root/docker/matomo
cd /root/docker/matomo
vim docker-compose.yml
输入保存以下内容https://github.com/matomo-org/docker/blob/master/.examples/nginx/docker-compose.yml
services:
db:
image: mariadb:10.11
command: --max-allowed-packet=64MB
restart: always
volumes:
- db:/var/lib/mysql:Z
environment:
- MYSQL_ROOT_PASSWORD= # 修改数据库密码
- MARIADB_AUTO_UPGRADE=1
- MARIADB_DISABLE_UPGRADE_BACKUP=1
env_file:
- ./db.env
app:
image: matomo:fpm-alpine
restart: always
links:
- db
volumes:
- ./config:/var/www/html/config:z
- ./logs:/var/www/html/logs:z
- matomo:/var/www/html:z
environment:
- MATOMO_DATABASE_HOST=db
- PHP_MEMORY_LIMIT=2048M
env_file:
- ./db.env
web:
image: nginx:alpine
restart: always
volumes:
- matomo:/var/www/html:z,ro
# see https://github.com/matomo-org/matomo-nginx
- ./matomo.conf:/etc/nginx/conf.d/default.conf:z,ro
ports:
- 8080:80
cron:
image: matomo:fpm-alpine
restart: always
environment:
- MATOMO_DATABASE_HOST=db
volumes:
- ./config:/var/www/html/config:z
- ./logs:/var/www/html/logs:z
- matomo:/var/www/html:z
depends_on: [matomo]
entrypoint: "sh -c 'while true; do php console core:archive --url=https://matomo.example.com/; sleep 600; done'" # 修改matomo.example.com为你的matomo域名,sleep 600为每10分钟归档一次,官方建议1小时一次,根据流量和性能自行修改
volumes:
db:
matomo:
添加数据库配置文件:
vim db.env
输入保存:
MYSQL_PASSWORD= # 添加数据库密码,与yml文件匹配
MYSQL_DATABASE=matomo
MYSQL_USER=matomo
MATOMO_DATABASE_ADAPTER=mysql
MATOMO_DATABASE_TABLES_PREFIX=matomo_
MATOMO_DATABASE_USERNAME=matomo
MATOMO_DATABASE_PASSWORD= # 添加数据库密码
MATOMO_DATABASE_DBNAME=matomo
MARIADB_AUTO_UPGRADE=1
MARIADB_INITDB_SKIP_TZINFO=1
添加nginx配置文件
vim matomo.conf
输入保存:
upstream php-handler {
server app:9000;
}
server {
listen 80;
add_header Referrer-Policy origin; # make sure outgoing links don't show the URL to the Matomo instance
root /var/www/html; # replace with path to your matomo instance
index index.php;
try_files $uri $uri/ =404;
## only allow accessing the following php files
location ~ ^/(index|matomo|piwik|js/index|plugins/HeatmapSessionRecording/configs).php {
# regex to split $uri to $fastcgi_script_name and $fastcgi_path
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# Check that the PHP script exists before passing it
try_files $fastcgi_script_name =404;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param HTTP_PROXY ""; # prohibit httpoxy: https://httpoxy.org/
fastcgi_pass php-handler;
}
## deny access to all other .php files
location ~* ^.+\.php$ {
deny all;
return 403;
}
## disable all access to the following directories
location ~ /(config|tmp|core|lang) {
deny all;
return 403; # replace with 404 to not show these directories exist
}
location ~ /\.ht {
deny all;
return 403;
}
location ~ js/container_.*_preview\.js$ {
expires off;
add_header Cache-Control 'private, no-cache, no-store';
}
location ~ \.(gif|ico|jpg|png|svg|js|css|htm|html|mp3|mp4|wav|ogg|avi|ttf|eot|woff|woff2|json)$ {
allow all;
## Cache images,CSS,JS and webfonts for an hour
## Increasing the duration may improve the load-time, but may cause old files to show after an Matomo upgrade
expires 1h;
add_header Pragma public;
add_header Cache-Control "public";
}
location ~ /(libs|vendor|plugins|misc/user) {
deny all;
return 403;
}
## properly display textfiles in root directory
location ~/(.*\.md|LEGALNOTICE|LICENSE) {
default_type text/plain;
}
}
# vim: filetype=nginx
运行:
docker compose up -d
在npm反代8080端口的matomo,并强制跳转ssl访问。建议统计域名不使用analytics.xxx.com 因为analytics会被广告屏蔽规则屏蔽。
添加网站
请看:
性能优化
在设置中可以看到自动检测出的问题,根据提示修复,优化至没有叉号和红色感叹号即可。这里记录下我遇到的问题。
通过配置文件强制ssl访问并优化数据库设置
修改配置文件
vim /root/docker/matomo/config/config.ini.php
在 [General]
下添加一行
force_ssl = 1
在 [database]
下添加一行
schema = Mariadb
设置自动存档报告
在docker-compose.yml的cron部分已经设置了自动存档。只需在归档设置中关闭实时归档即可:
设置完成:
matomo使用cdn后获取访客真实ip
运行一天查看报表时,发现显示的ip为cdn节点ip,查看官方文档https://matomo.org/faq/how-to-install/faq_98/
发现需要修改config/config.ini.php
,在[General]
添加以下内容:
assume_secure_protocol = 1
proxy_host_headers[] = HTTP_X_FORWARDED_HOST
proxy_ip_read_last_in_list = 0
添加上述内容后重启docker,发现能正常获取访客ip,如果仍有问题,建议查看官方文档修改配置。
更新
如果matomo自动检测到更新,会在主页顶部提示更新,一键更新即可。
设置中也可以手动检查更新:
迁移使用docker安装的matomo
ROOT_PASSWORD
为你在docker-compose.yml中设置的数据库密码MYSQL_ROOT_PASSWORD
1. 在原服务器备份数据库
# 备份数据库,使用 docker-compose 执行
docker compose exec db mysqldump -u root -pROOT_PASSWORD --databases $(cat db.env | grep MYSQL_DATABASE | cut -d'=' -f2) > matomo_backup.sql
# 备份整个目录
tar -czf matomo_backup.tar.gz config logs matomo.conf docker-compose.yml db.env matomo_backup.sql
将数据复制到新服务器,我使用python开启临时http方式
python3 -m http.server 8000
2. 在新服务器上:
创建目录后恢复文件
# 下载备份文件
wget http://<原服务器ip>:8000/matomo_backup.tar.gz
# 解压备份文件
tar -xzf matomo_backup.tar.gz
# 启动服务(先不要还原数据库)
docker compose up -d
# 等待容器都启动后,导入数据库
cat matomo_backup.sql | docker compose exec -T db mysql -u root -pROOT_PASSWORD
如果没有报错,数据库应该已经正确导入。可以通过检查数据条数验证:
# 进入数据库查看
docker compose exec db mysql -u root -pROOT_PASSWORD
# 在 MySQL 命令行中执行:
SHOW DATABASES;
USE matomo;
SHOW TABLES;
# 查看其中一个表的数据条数,比如
SELECT COUNT(*) FROM matomo_log_visit;
此时参考原服务器配置反代,访问域名,查看是否有报错。
我遇到报错
The directory "/var/www/html/tmp/cache/tracker/" does not exist and could not be created.
尝试手动创建并设置权限
# 进入 matomo app 容器
docker compose exec app sh
# 在容器内执行
mkdir -p /var/www/html/tmp/cache/tracker
chown -R www-data:www-data /var/www/html/tmp
chmod -R 755 /var/www/html/tmp
再次刷新,报错消失,可以成功登录访问。
在设置页面查看有无报错
我遇到了geoip数据库报错,可能是新版本不会自动下载免费数据库。在系统-地理位置,拉到最下面自动下载免费的dbip数据库即可。
至此迁移完成,目前正常使用。
参考
https://github.com/matomo-org/docker/tree/master
https://github.com/matomo-org/docker/issues/298
暂无评论内容