如何在 Ubuntu 18.04 LTS 上安裝 Matomo Web Analytics

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

在本教程中,我將解釋如何在 Ubuntu 18.04 LTS 服務器上安裝 Matomo。

要求

  • 運行 Ubuntu 18.04 LTS 的服務器。
  • 一個非 root 用戶 sudo 特權。

安裝 LAMP 服務器

Matomo 運行在 Web 服務器上,用 PHP 語言編寫並使用 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

Save 和 close 文件,當你完成。

配置 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 數據庫授予權限:

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

用您選擇的安全密碼替換“mypassword”一詞。 接下來,使用以下命令刷新權限:

MariaDB [(none)]> FLUSH PRIVILEGES;

接下來,使用以下命令從 MariaDB 控制台退出:

MariaDB [(none)]> exit

安裝 Matomo

首先,從他們的官方網站下載最新版本的 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 使用以下命令為 Matomo 配置文件:

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] 使用您自己的域名和電子郵件地址。 Save 和 close 文件,然後啟用虛擬主機文件和 Apache 使用以下命令重寫模塊:

sudo a2ensite matomo
sudo a2enmod rewrite

重新開始 Apache 應用所有更改的 Web 服務:

sudo systemctl restart apache2

使用 Let’s encrypt 為 Matomo 啟用 SSL

在這一步中,我們將使用免費的 Let’s encrypt SSL 證書為 Matomo 啟用 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

現在可以通過 https 訪問 Matomo vhost。

訪問 Matomo

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

現在,點擊 下一個 按鈕。 您應該會看到以下頁面:

在這裡,確保已安裝所有必需的模塊。 然後,點擊 下一個 按鈕。 您應該會看到以下頁面:

在這裡,提供您的 Matomo 數據庫和用戶名。 然後,點擊 下一個 按鈕。 您應該會看到以下頁面:

現在,點擊 下一個 按鈕。 您應該會看到以下頁面:

在這裡,提供您的 admin 用戶名和密碼。 然後,點擊 下一個 按鈕。 您應該會看到以下頁面:

現在,提供站點名稱、網站 URL。 然後,點擊 下一個 按鈕。 您應該會在以下頁面中看到 Matomo 站點的跟踪代碼:

現在,點擊 下一個 按鈕。 一旦安裝完成。 您應該會看到以下頁面:

現在,點擊 繼續 MATOMO 按鈕。 您應該會看到以下頁面:

現在,提供管理員登錄憑據並單擊 登入 按鈕。 您應該會在以下頁面中看到 Matomo 儀表板: