本教程适用于以下操作系统版本
- Debian 12 (Bookworm)
- Debian 11 (Bullseye)
- Debian 9 (Stretch)
本页内容
- 前提条件
- 安装 Apache、MariaDB 和 PHP
- 为 Matomo 创建 MariaDB 数据库
- 下载 Matomo
- 为 Matomo 配置 Apache
- 访问 Matomo Web 界面
- 使用 Let's Encrypt SSL 保护 Matomo
- 结论
Matomo(前身为 Piwik)是一款由开发者社区开发的免费开源网站分析应用。它用于跟踪你的网站,并提供关于你的网站及其访问者的详细信息,包括他们使用的搜索引擎和关键词、他们使用的语言、他们喜欢哪些页面、他们下载的文件等等。它提供大量功能,包括 Google AdWords、Facebook Ads、Yahoo、Search Marketing、Tracking and Reporting API 以及 Cost Per Click (CPC)。
在本教程中,我将向你展示如何在 Debian 11 上使用 Apache2 Web 服务器和免费的 Let's Encrypt SSL 证书安装 Matomo。
前提条件
- 一台运行 Debian 11 的服务器。
- 一个指向你服务器 IP 的有效域名。
- 服务器上已配置 root 密码。
安装 Apache、MariaDB 和 PHP
首先,你需要在服务器上安装 Apache Web 服务器、MariaDB 数据库服务器、PHP 及其他 PHP 扩展。你可以运行以下命令一次性安装它们:
apt-get install apache2 mariadb-server php libapache2-mod-php php-cli php-fpm php-fpm php-json php-json php-common php-mysql php-zip php-gd php-mbstring php-curl php-xml php-pear php-bcmath curl unzip -y
安装完所有软件包后,使用以下命令启动并启用 Apache 和 MariaDB 服务:
systemctl start apache2 mariadb
systemctl enable apache2 mariadb
完成后,你可以继续下一步。
为 Matomo 创建 MariaDB 数据库
Matomo 使用 MariaDB 作为数据库后端。因此你需要为 Matomo 创建一个数据库和用户。
首先,使用以下命令登录 MariaDB:
mysql
登录后,使用以下命令创建数据库和用户:
MariaDB [(none)]> CREATE DATABASE matomodb;
MariaDB [(none)]> CREATE USER 'matomo'@'localhost' IDENTIFIED BY 'password';
接下来,使用以下命令为 Matomo 数据库授予所有权限:
MariaDB [(none)]> GRANT ALL ON matomodb.* TO 'matomo'@'localhost' WITH GRANT OPTION;
接下来,使用以下命令刷新权限并退出 MariaDB shell:
MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> EXIT;
配置好 MariaDB 数据库后,你可以继续下一步。
下载 Matomo
接下来,访问 Matomo 官方网站,并使用以下命令下载最新版本的 Matomo:
wget http://builds.matomo.org/matomo-latest.zip
下载完成后,使用以下命令解压下载的文件:
unzip matomo-latest.zip
接下来,将解压出的目录移动到 Apache Web 根目录:
mv matomo /var/www/html/matomo
接下来,更改 Matomo 目录的所有权和权限:
chown -R www-data:www-data /var/www/html/matomo
chmod -R 755 /var/www/html/matomo
完成后,你可以继续下一步。
为 Matomo 配置 Apache
接下来,你需要创建一个 Apache 虚拟主机配置文件,以便在互联网上托管 Matomo。你可以使用以下命令创建它:
nano /etc/apache2/sites-available/matomo.conf
添加以下内容:
<VirtualHost *:80>
ServerAdmin
[email protected]
ServerName matomo.example.com
DocumentRoot /var/www/html/matomo/
<Directory /var/www/html/matomo>
DirectoryIndex index.php
Options FollowSymLinks
AllowOverride All
Require all granted
</Directory>
<Files "console">
Options None
Require all denied
</Files>
<Directory /var/www/html/matomo/misc/user>
Options None
Require all granted
</Directory>
<Directory /var/www/html/matomo/misc>
Options None
Require all denied
</Directory>
<Directory /var/www/html/matomo/vendor>
Options None
Require all denied
</Directory>
ErrorLog ${APACHE_LOG_DIR}/matomo_error.log
CustomLog ${APACHE_LOG_DIR}/matomo_access.log combined
</VirtualHost>
完成后保存并关闭文件,然后使用以下命令启用 Matomo 虚拟主机和 Apache rewrite 模块:
a2ensite matomo.conf
a2enmod rewrite
接下来,重启 Apache 服务以应用更改:
systemctl restart apache2
你还可以使用以下命令查看 Apache 状态:
systemctl status apache2
你将看到以下输出:
? apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
Active: active (running) since Sun 2022-03-06 06:33:24 UTC; 5s ago
Docs: https://httpd.apache.org/docs/2.4/
Process: 55482 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
Main PID: 55486 (apache2)
Tasks: 6 (limit: 4679)
Memory: 12.4M
CPU: 108ms
CGroup: /system.slice/apache2.service
??55486 /usr/sbin/apache2 -k start
??55487 /usr/sbin/apache2 -k start
??55488 /usr/sbin/apache2 -k start
??55489 /usr/sbin/apache2 -k start
??55490 /usr/sbin/apache2 -k start
??55491 /usr/sbin/apache2 -k start
Mar 06 06:33:24 debian11 systemd[1]: Starting The Apache HTTP Server...
Mar 06 06:33:24 debian11 apachectl[55485]: AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 209>
Mar 06 06:33:24 debian11 systemd[1]: Started The Apache HTTP Server.
访问 Matomo Web 界面
现在,打开你的 Web 浏览器,使用 URL http://matomo.example.com 访问 Matomo Web 界面。你将被重定向到以下页面:

点击 Next 按钮。你应该会看到 PHP 配置检查页面:

确认所有配置都正常,然后点击 NEXT 按钮。你应该会看到 MariaDB 数据库设置页面:

填写你的数据库服务器、用户名、数据库名、密码,然后点击 NEXT 按钮。你应该会看到以下页面:

点击 NEXT 按钮。你应该会看到超级用户创建页面:

填写你的管理员用户名、密码、邮箱,然后点击 NEXT 按钮。你应该会看到网站设置页面:

填写你的网站名称、URL、时区,然后点击 NEXT 按钮。你应该会看到以下页面:


点击 NEXT 按钮。你应该会看到配置验证页面:

点击 CONTINUE TO MATOMO。你应该会看到 Matomo 登录页面:

填写你的管理员用户名、密码,然后点击 SIGN IN 按钮。你应该会在以下页面看到 Matomo 仪表板:你现在可以复制上图中显示的跟踪代码,并将其粘贴到你网站的每个页面上。

使用 Let's Encrypt SSL 保护 Matomo
接下来,你需要安装 Certbot 客户端,以便为你的网站安装 Let's Encrypt SSL。你可以使用以下命令安装它:
dnf install epel-release -y dnf install certbot python3-certbot-apache
接下来,使用以下命令为你的域名获取并安装 SSL 证书:
certbot --apache -d matomo.example.com
系统将要求你提供邮箱地址并接受服务条款:
Saving debug log to /var/log/letsencrypt/letsencrypt.log Plugins selected: Authenticator apache, Installer apache Enter email address (used for urgent renewal and security notices) (Enter 'c' to cancel): [email protected] - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Please read the Terms of Service at https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must agree in order to register with the ACME server. Do you agree? - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - (Y)es/(N)o: Y - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Would you be willing, once your first certificate is successfully issued, to share your email address with the Electronic Frontier Foundation, a founding partner of the Let's Encrypt project and the non-profit organization that develops Certbot? We'd like to send you email about our work encrypting the web, EFF news, campaigns, and ways to support digital freedom. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - (Y)es/(N)o: Y Account registered. Requesting a certificate for matomo.example.com Performing the following challenges: http-01 challenge for matomo.example.com Waiting for verification... Cleaning up challenges Deploying Certificate to VirtualHost /etc/httpd/conf.d/matomo.conf Redirecting all traffic on port 80 to ssl in /etc/httpd/conf.d/matomo.conf - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Congratulations! You have successfully enabled https://matomo.example.com - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Subscribe to the EFF mailing list (email: [email protected] ). IMPORTANT NOTES: - Congratulations! Your certificate and chain have been saved at: /etc/letsencrypt/live/matomo.example.com/fullchain.pem Your key file has been saved at: /etc/letsencrypt/live/matomo.example.com/privkey.pem Your certificate will expire on 2022-05-09. To obtain a new or tweaked version of this certificate in the future, simply run certbot again with the "certonly" option. To non-interactively renew *all* of your certificates, run "certbot renew" - If you like Certbot, please consider supporting our work by: Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate Donating to EFF: https://eff.org/donate-le
结论
恭喜!你已成功在 Debian 11 上安装了 Matomo。你现在可以将跟踪代码添加到你的网站,并从 Matomo 仪表板开始监控它。如有任何问题,欢迎随时向我提问。