Matomo使用Linux Crontab定时存档网站统计报告 - 阿峰博客
Dragon

Matomo 使用 Linux Crontab 定时存档网站统计报告

2022-10-31 12:42 165 抢沙发 阿峰博客

根据Matomo官方的提示,matomo 建议定时处理网站统计报告定时进行存档操作。
参见官方地址:https://matomo.org/faq/on-premise/how-to-set-up-auto-archiving-of-your-reports/

为什么需要定时存档报告?

官方给出的说明:
If your website has more than a few hundreds visits per day (bravo!), waiting for Matomo to process your data may take a few minutes. The best way to avoid these waiting times is to set up a cron job on your server so that your data is automatically processed every hour.
如果您的网站每天的访问量超过数百次(太棒了!),等待 Matomo 处理您的数据可能需要几分钟。避免这些等待时间的最佳方法是在您的服务器上设置一个 cron 作业,以便每小时自动处理您的数据。
Matomo 使用 Linux Crontab 定时存档网站统计报告插图
Matomo 使用 Linux Crontab 定时存档网站统计报告插图1
网站访问量上去之后,matomo 处理网站统计数据的速度就会变慢,可能需要几分钟甚至更久,如果使用定时任务的方式处理数据,可以大幅减轻服务器的压力。
实时统计因为是实时写入数据,如果当前并发量过高,matomo 的压力会更大,所以设置定时处理任务是唯一也是最好的选择。
不过禁用在浏览器中查看报告时进行归档后,无法实时查看数据,只能等待 matomo定时任务处理成功后才可查看访问日志,只能读取预存档的报告。
下面介绍 Linux 设置 Crontab 以自动存档报告
Matomo 使用 Linux Crontab 定时存档网站统计报告插图2

1.0 创建定时处理脚本文件

首先需要登录 SSH 终端。
执行下方命令,创建 crontab。

nano /etc/cron.d/matomo-archive

Matomo 使用 Linux Crontab 定时存档网站统计报告插图3
添加下方的内容,以下是官方提供的内容,如果使用的是宝塔面板可以参考宝塔面板配置。

MAILTO="youremail@example.com" 
5 * * * * www-data /usr/bin/php /path/to/matomo/console core:archive --url=http://example.org/matomo/ > /home/example/matomo-archive.log

参数分解:

  • MAILTO="youremail@example.com"如果脚本执行过程中出现错误,脚本输出和错误消息将发送到 yourremail@example.com 地址。
  • www-data是执行 cron 作业的用户。用户有时是“apache”。建议以与 Web 服务器用户相同的用户身份运行 crontab(以避免文件权限不匹配)。
  • /usr/bin/php是 PHP 可执行文件的路径。它取决于您的服务器配置和操作系统。您可以在 linux shell 中执行命令“which php”或“which php”,以找出 PHP 可执行文件的路径。如果您不知道路径,请询问您的网络主机或系统管理员。
  • /path/to/matomo/console是您服务器上 Matomo 应用程序的路径。例如,它可能是/var/www/matomo/console.
  • --url=http://example.org/matomo/是脚本中唯一需要的参数,必须设置为您的 Matomo 基本 URL,例如。http://analytics.example.org/http://example.org/matomo/
  • > /home/example/matomo-archive.log是脚本将写入输出的路径。如果您不想记录最后一个 Matomo cron 输出文本,可以用/dev/null替换此路径。脚本输出包含有用的信息,例如存档了哪些网站,处理每个日期和网站需要多长时间等。此日志文件应写入您的网络服务器之外的位置,以便人们无法通过他们的浏览器(因为此日志文件将包含有关您的 Matomo 安装的一些敏感信息)。您还可以将>替换为>>以便将脚本输出附加到日志文件,而不是在每次运行时覆盖它(但我们建议您轮换此日志文件或删除它,例如每周一次)。
  • 2> /home/example/matomo-archive-errors.log是脚本将写入错误消息的可选路径。如果您在 cron 选项卡中省略了此项,则错误将通过电子邮件发送到您的 MAILTO 地址。如果您在 crontab 中写入此内容,则错误将记录在此指定的错误日志文件中。这个日志文件应该写在你的网络服务器之外的位置,这样人们就不能通过他们的浏览器查看它(因为这个日志文件将包含一些关于你的 Matomo 安装的敏感信息)。

linux cron'实用程序的描述: cron实用程序使用两种不同类型的配置文件:系统 crontab 和用户 crontabs。这两种格式之间的唯一区别是第六个字段。

  • 系统 crontab中,第六个字段是运行命令的用户名。这使系统 crontab 能够以任何用户身份运行命令。
  • 用户 crontab中,第六个字段是要运行的命令,所有命令都以创建该 crontab 的用户身份运行;这是一项重要的安全功能。

如果您将 crontab 设置为用户 crontab,您将改为编写:

5 * * * * /usr/bin/php /path/to/matomo/console core:archive --url=http://example.org/matomo/ > /dev/null

此 cron 作业将在每小时 5 分钟后触发日/周/月/年归档过程。这将确保当您访问 Matomo 仪表板时,数据已经被处理;Matomo 将快速加载。
以上内容来源 matomo 帮助。

1.1 宝塔面板参考下方实例

MAILTO="afeng@mail.afengim.com" 
5 * * * * www /usr/bin/php /www/wwwroot/we.afengim.com/console core:archive --url=http://we.afengim.com/ > /home/matomo/matomo-archive.log

可参考以上及以下实例修改

MAILTO="邮箱" 
5 * * * * www PHP 可执行文件路径 matomo 根目录绝对路径/console core:archive --url=网站首页 url > /home/matomo/matomo-archive.log

www 为执行定时任务的用户,一般为 www 即可
PHP 可执行文件路径一般默认为/usr/bin/php,可执行 which php 获取实际 path
Matomo 使用 Linux Crontab 定时存档网站统计报告插图4
matomo 根目录相对路径,如果是宝塔默认的网站路径,且不是在二级目录运行程序,一般为:/www/wwwroot/we.afengim.com
请注意!后面的/console 为 console 文件的路径,请不要删除!!!
网站首页 url 地址需要填写协议头,结尾需要带“/”
其他设置默认即可,若有额外需求可根据官方文档编写。
配置文件根据需求写完后复制内容,粘贴到 ssh 终端即可,Ctrl+O 后 Enter 即可保存文件,如需退出 nano 模式,Ctrl+X 即可退出。
Matomo 使用 Linux Crontab 定时存档网站统计报告插图5
Matomo 使用 Linux Crontab 定时存档网站统计报告插图6

1.2 测试 cron 处理效果

定时处理任务已经完成,那么我们测试定时处理统计数据任务是否有效。
执行下方命令至终端即可

su www -s /bin/bash -c "/usr/bin/php /www/wwwroot/we.afengim.com/console core:archive --url=http://we.afengim.com/"

请注意替换上方的网站路径及首页 URL。
Matomo 使用 Linux Crontab 定时存档网站统计报告插图7
根据输出的日志,任务处理成功!

[root@VM-12-10-centos ~]# su www -s /bin/bash -c "/usr/bin/php /www/wwwroot/we.afengim.com/console core:archive --url=http://we.afengim.com/"
INFO [2022-09-12 07:16:28] 22965  ---------------------------
INFO [2022-09-12 07:16:28] 22965  INIT
INFO [2022-09-12 07:16:28] 22965  Running Matomo 4.11.0 as Super User
INFO [2022-09-12 07:16:28] 22965  ---------------------------
INFO [2022-09-12 07:16:28] 22965  NOTES
INFO [2022-09-12 07:16:28] 22965  - If you execute this script at least once per hour (or more often) in a crontab, you may disable 'Browser trigger archiving' in Matomo UI > Settings > General Settings.
INFO [2022-09-12 07:16:28] 22965    See the doc at: https://matomo.org/docs/setup-auto-archiving/
INFO [2022-09-12 07:16:28] 22965  - Async process archiving supported, using CliMulti.
INFO [2022-09-12 07:16:28] 22965  - Reports for today will be processed at most every 900 seconds. You can change this value in Matomo UI > Settings > General Settings.
INFO [2022-09-12 07:16:28] 22965  - Archiving was last executed without error 1 hours 42 min ago.
INFO [2022-09-12 07:16:29] 22965  ---------------------------
INFO [2022-09-12 07:16:29] 22965  START
INFO [2022-09-12 07:16:29] 22965  Starting Matomo reports archiving...
INFO [2022-09-12 07:16:29] 22965  Start processing archives for site 1.
INFO [2022-09-12 07:16:29] 22965  Finished archiving for site 1, 0 API requests, Time elapsed: 0.069s [1 / 1 done]
INFO [2022-09-12 07:16:29] 22965  Done archiving!
INFO [2022-09-12 07:16:29] 22965  ---------------------------
INFO [2022-09-12 07:16:29] 22965  SUMMARY
INFO [2022-09-12 07:16:29] 22965  Processed 0 archives.
INFO [2022-09-12 07:16:29] 22965  Total API requests: 0
INFO [2022-09-12 07:16:29] 22965  done: 0 req, 146 ms, no error
INFO [2022-09-12 07:16:29] 22965  Time elapsed: 0.146s
INFO [2022-09-12 07:16:29] 22965  ---------------------------
INFO [2022-09-12 07:16:29] 22965  SCHEDULED TASKS
INFO [2022-09-12 07:16:29] 22965  Starting Scheduled tasks... 
INFO [2022-09-12 07:16:29] 22965  Scheduler: executing task Piwik\Plugins\CustomJsTracker\Tasks.updateTracker...
INFO [2022-09-12 07:16:29] 22965  Scheduler: finished. Time elapsed: 0.036s
INFO [2022-09-12 07:16:29] 22965  Scheduler: executing task Piwik\Plugins\PrivacyManager\Tasks.deleteLogData...
INFO [2022-09-12 07:16:29] 22965  Scheduler: finished. Time elapsed: 0.000s
INFO [2022-09-12 07:16:29] 22965  Scheduler: executing task Piwik\Plugins\PrivacyManager\Tasks.anonymizePastData...
INFO [2022-09-12 07:16:29] 22965  Scheduler: finished. Time elapsed: 0.001s
INFO [2022-09-12 07:16:29] 22965  done
INFO [2022-09-12 07:16:29] 22965  ---------------------------

设置完上面的操作后,请在:Administration > System -> General Settings(系统>通用设置)禁用 在浏览器中查看报告时进行归档 最多每 X 秒进行归档报告设置 3600 即 1 小时处理一次数据
Matomo 使用 Linux Crontab 定时存档网站统计报告插图8
有关 matomo 速度优化等文档可参见官方文档地址:
如何设置报告的自动存档
配置 Matomo 以提高速度

「点点赞赏,手留余香」

还没有人赞赏,快来当第一个赞赏的人吧!

阿峰博客给阿峰博客打赏
×
予人玫瑰,手有余香
  • 1
  • 5
  • 10
  • 20
  • 50
1
支付

本文为原创文章,版权归所有,欢迎分享本文,转载请保留出处!

2023-03-07

2022-11-04

发表评论