Matomo (Piwik),以前稱為 Piwik,是一款免費的網絡分析應用程序,可用於跟踪對一個或多個網站的在線訪問,並顯示這些訪問的報告以供分析。 Piwik 具有許多功能,例如 Google AdWords、Facebook Ads、Yahoo!搜索營銷、跟踪和報告 API、每次點擊費用 (CPC) 等。

本教程將向您展示如何在您的 Ubuntu 18.04 LTS 服務器上安裝 Matomo (Piwik)。

我需要

  • 運行 Ubuntu 18.04 LTS 的服務器。
  • 具有 sudo 權限的非 root 用戶。

安裝 LAMP 服務器

Piwik 在用 PHP 編寫的 Web 服務器上運行,並使用 MariaDB 存儲數據。因此,您需要安裝 Apache、MariaDB、PHP 和系統所需的任何其他 PHP 模塊。

使用以下命令更新 Ubuntu 存儲庫並安裝 PHP Apache 和 MariaDB:

sudo apt-get update -y
sudo apt-get install apache2 mariadb-server php7.2 libapache2-mod-php7.2 php7.2-common php7.2-sqlite php7.2-curl php7.2-intl php7.2-mbstring php7.2-xmlrpc php7.2-mysql php7.2-gd php7.2-xml php7.2-cli php7.2-zip wget unzip git -y

安裝所有軟件包後,啟動 Apache 和 MariaDB 服務,並使用以下命令允許它們在啟動時啟動:

sudo systemctl start apache2
sudo systemctl enable apache2
sudo systemctl start mariadb
sudo systemctl enable mariadb

接下來,我們需要修改 php.ini 文件。這可以使用以下命令完成:

sudo nano /etc/php/7.2/apache2/php.ini

進行以下更改:

memory_limit = 256M
upload_max_filesize = 200M
max_execution_time = 360
date.timezone = Europe/Berlin

完成後保存並關閉文件。

配置 MariaDB

接下來,您需要保護您的 MariaDB 安裝。為此,請運行以下腳本。

sudo mysql_secure_installation

請按以下方式回答所有問題:

    Enter current password for root (enter for none):
    Set root password? [Y/n]: N
    Remove anonymous users? [Y/n]: Y
    Disallow root login remotely? [Y/n]: Y
    Remove test database and access to it? [Y/n]:  Y
    Reload privilege tables now? [Y/n]:  Y

保護 MariaDB 後,登錄 MariaDB shell。

mysql -u root

使用以下命令創建數據庫和用戶:

MariaDB [(none)]> CREATE DATABASE matomodb;
MariaDB [(none)]> CREATE USER matomo;

接下來,使用以下命令授予對 Matomo (Piwik) 數據庫的權限:

MariaDB [(none)]> GRANT ALL PRIVILEGES ON matomodb.* TO 'matomo'@'localhost' IDENTIFIED BY 'mypassword';

用您選擇的安全密碼替換“mypassword”一詞。然後使用以下命令更新權限:

MariaDB [(none)]> FLUSH PRIVILEGES;

然後使用以下命令退出 MariaDB 控制台:

MariaDB [(none)]> exit

安裝 Piwik

首先,從官網下載最新版本的Matomo到/tmp目錄下。

cd /tmp
wget https://builds.matomo.org/piwik.zip

然後使用以下命令提取下載的文件:

unzip piwik.zip

然後將解壓縮的目錄複製到您的 apache 根目錄並賦予其適當的權限。

sudo cp -r piwik /var/www/html/matomo
sudo chown -R www-data:www-data /var/www/html/matomo/
sudo chmod -R 755 /var/www/html/matomo/

最後,使用以下命令創建 Apache Piwik 配置文件:

sudo nano /etc/apache2/sites-available/matomo.conf

添加以下行:

<VirtualHost *:80>
     ServerAdmin [email protected]
     DocumentRoot /var/www/html/matomo
     ServerName example.com

     <Directory /var/www/html/matomo/>
          Options FollowSymlinks
          AllowOverride All
          Require all granted
     </Directory>

     ErrorLog ${APACHE_LOG_DIR}/matomo_error.log
     CustomLog ${APACHE_LOG_DIR}/matomo_access.log combined

</VirtualHost>

將 example.com 替換為您的電子郵件地址 [email protected] 使用您自己的域名和電子郵件地址。保存並關閉文件並使用以下命令啟用虛擬主機文件和 Apache 重寫模塊:

sudo a2ensite matomo
sudo a2enmod rewrite

重新啟動 Apache 以應用所有修改後的 Web 服務。

sudo systemctl restart apache2

使用 Let’s encrypt 為 Piwik 啟用 SSL

在這一步中,我們將使用免費的 Let’s encrypt SSL 證書為 Piwik 啟用 SSL。第一步是安裝 certbot Let’s Encrypt 客戶端,我們將使用它來獲取 SSL 證書。

sudo apt-get install certbot python-certbot-apache

使用以下命令請求 SSL 證書:

sudo certbot --apache

重要提示:RoundCube 網站使用的域名或子域必須可從 Internet 訪問才能獲得 SSL 證書。 Certbot 會問你幾個問題。

[email protected]: certbot --apache
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 at
https://acme-v01.api.letsencrypt.org/directory
-------------------------------------------------------------------------------
(A)gree/(C)ancel: a
-------------------------------------------------------------------------------
Would you be willing 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 EFF and
our work to encrypt the web, protect its users and defend digital rights.
-------------------------------------------------------------------------------
(Y)es/(N)o: n
Which names would you like to activate HTTPS for?
-------------------------------------------------------------------------------
1: example.com
-------------------------------------------------------------------------------
Select the appropriate numbers separated by commas and/or spaces, or leave input
blank to select all options shown (Enter 'c' to cancel): 1
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for example.com
Waiting for verification...
Cleaning up challenges
Created an SSL vhost at /etc/apache2/sites-available/matomo-le-ssl.conf
Enabled Apache socache_shmcb module
Enabled Apache ssl module
Deploying Certificate to VirtualHost /etc/apache2/sites-available/matomo-le-ssl.conf
Enabling available site: /etc/apache2/sites-available/matomo-le-ssl.conf
Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
-------------------------------------------------------------------------------
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
-------------------------------------------------------------------------------
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2
Redirecting vhost in /etc/apache2/sites-enabled/matomo.conf to ssl vhost in /etc/apache2/sites-available/matomo-le-ssl.conf
-------------------------------------------------------------------------------
Congratulations! You have successfully enabled https://example.com
You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=example.com
-------------------------------------------------------------------------------
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/example.com/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/example.com/privkey.pem
Your cert will expire on 2019-07-10. 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"
- Your account credentials have been saved in your Certbot
configuration directory at /etc/letsencrypt. You should make a
secure backup of this folder now. This configuration directory will
also contain certificates and private keys obtained by Certbot so
making regular backups of this folder is ideal.
- 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

Piwik 網絡託管現在可通過 https 訪問。

拜訪一位朋友

現在打開您的網絡瀏覽器並輸入 URL https://example.com,您將被重定向到 Matomo 歡迎頁面。

立即點擊 下一個 按鈕。顯示以下頁面。

現在確保安裝了所有必需的模塊。然後點擊 下一個 按鈕。顯示以下頁面。

數據庫設置

在此處輸入您的 Piwik 數據庫和用戶名。然後點擊 下一個 按鈕。顯示以下頁面。

創建數據庫表

立即點擊 下一個 按鈕。顯示以下頁面。

添加超級用戶

在此處輸入管理員用戶名和密碼。然後點擊 下一個 按鈕。顯示以下頁面。

使用 Piwik 構建網站

在這裡,指定站點名稱、站點 URL。然後點擊 下一個 按鈕。在下一頁上,您將看到 Matomo (Piwik) 網站的跟踪代碼。

網站代碼

立即點擊 下一個 按鈕。安裝完成後。顯示以下頁面。

Matomo 已成功安裝

立即點擊 繼續matomo 按鈕。顯示以下頁面。

登錄

在這裡,輸入您的管理員登錄憑據,然後單擊 登錄 按鈕。 Piwik 儀表板將顯示在下一頁上。

Matomo 儀表板