GNU/Linux >> Linux Esercitazione >  >> Debian

Installare Matomo su Debian 11 con Apache, MariaDB e Let's Encrypt

Matomo — in precedenza Piwik — è una piattaforma di web analytics gratuita e open source, sviluppata e mantenuta da una community di sviluppatori. Dove molti strumenti si limitano a riassumere i dati, Matomo scende nei particolari: quali motori di ricerca e parole chiave hanno usato i visitatori, quali lingue parlano, su quali pagine si soffermano, quali file scaricano. A questo si aggiungono il monitoraggio dei costi pubblicitari di Google AdWords, Facebook Ads e Yahoo e la disponibilità di una API per il tracciamento e il reporting.

In questa guida installeremo Matomo su Debian 11 dietro ad Apache e lo proteggeremo con un certificato gratuito di Let's Encrypt.

Prerequisiti

  • Un server con Debian 11.
  • Un nome di dominio puntato all'indirizzo IP del server.
  • Accesso root al server.

Installazione di Apache, MariaDB e PHP

Matomo richiede un web server, un database e un set piuttosto completo di estensioni PHP:

apt-get install apache2 mariadb-server php libapache2-mod-php php-cli php-fpm php-json php-common php-mysql php-zip php-gd php-mbstring php-curl php-xml php-pear php-bcmath curl unzip -y

Avviare e abilitare entrambi i servizi:

systemctl start apache2 mariadb
systemctl enable apache2 mariadb

Creazione del database per Matomo

Matomo memorizza tutto in MariaDB: occorre quindi creare un database e un account dedicato.

mysql
MariaDB [(none)]> CREATE DATABASE matomodb;
MariaDB [(none)]> CREATE USER 'matomo'@'localhost' IDENTIFIED BY 'password';
MariaDB [(none)]> GRANT ALL ON matomodb.* TO 'matomo'@'localhost' WITH GRANT OPTION;
MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> EXIT;

Sostituire password con una password robusta e annotarla: servirà nella fase di configurazione via browser.

Download di Matomo

Scaricare la build corrente direttamente dai server del progetto:

wget https://builds.matomo.org/matomo-latest.zip

Estrarre l'archivio e spostarlo nella document root di Apache:

unzip matomo-latest.zip
mv matomo /var/www/html/matomo

Assegnare la proprietà all'utente del web server:

chown -R www-data:www-data /var/www/html/matomo
chmod -R 755 /var/www/html/matomo

Configurazione di Apache

Creare l'host virtuale. Le regole sul directory sono importanti: impediscono che le cartelle console, misc e vendor vengano servite direttamente, consentendo però gli override di .htaccess dove Matomo ne ha bisogno.

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>

Abilitare il sito e il modulo di rewrite (indispensabile per gli URL "puliti"), quindi riavviare Apache:

a2ensite matomo.conf
a2enmod rewrite
systemctl restart apache2

Verificare che Apache sia in esecuzione:

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/
Main PID: 55486 (apache2)
Tasks: 6 (limit: 4679)
Memory: 12.4M
CPU: 108ms
Mar 06 06:33:24 debian11 systemd[1]: Started The Apache HTTP Server.

Installazione tramite interfaccia web

Aprire https://matomo.example.com: si viene accolti dall'installer.

Installare Matomo su Debian 11 con Apache, MariaDB e Let s Encrypt

Fare clic su Next: Matomo analizza ora la configurazione PHP.

Installare Matomo su Debian 11 con Apache, MariaDB e Let s Encrypt

Se tutti i controlli risultano superati, proseguire con NEXT per raggiungere la schermata del database.

Installare Matomo su Debian 11 con Apache, MariaDB e Let s Encrypt

Inserire host del database, utente, nome del database e password creati nella sezione precedente, quindi fare clic su NEXT.

Installare Matomo su Debian 11 con Apache, MariaDB e Let s Encrypt

Un ulteriore NEXT permette di creare il super utente, cioè l'account con cui si accederà a Matomo.

Installare Matomo su Debian 11 con Apache, MariaDB e Let s Encrypt

Indicare nome utente amministratore, password ed email, poi passare alla configurazione del sito.

Installare Matomo su Debian 11 con Apache, MariaDB e Let s Encrypt

Assegnare un nome al sito, inserirne l'URL, scegliere il fuso orario e premere NEXT. Matomo mostra quindi lo snippet JavaScript di tracciamento.

Installare Matomo su Debian 11 con Apache, MariaDB e Let s Encrypt

Installare Matomo su Debian 11 con Apache, MariaDB e Let s Encrypt

Un ultimo NEXT apre il riepilogo di verifica.

Installare Matomo su Debian 11 con Apache, MariaDB e Let s Encrypt

Fare clic su CONTINUE TO MATOMO per raggiungere la pagina di accesso.

Installare Matomo su Debian 11 con Apache, MariaDB e Let s Encrypt

Effettuato il login si arriva alla dashboard. Copiare il codice di tracciamento mostrato e incollarlo in tutte le pagine del sito: finché non lo si fa, Matomo non avrà dati da misurare.

Installare Matomo su Debian 11 con Apache, MariaDB e Let s Encrypt

Aggiungere un certificato Let's Encrypt

Installare il client Certbot e il plugin per Apache:

apt-get install certbot python3-certbot-apache -y

Su Debian il pacchetto epel-release non serve (è specifico di RHEL e derivate): è sufficiente il comando qui sopra.

Richiedere e installare il certificato per il proprio dominio:

certbot --apache -d matomo.example.com

Certbot chiede un indirizzo email e l'accettazione dei termini di servizio, quindi riscrive l'host virtuale in modo da reindirizzare il traffico HTTP verso HTTPS:

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. (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/apache2/sites-available/matomo.conf
Redirecting all traffic on port 80 to ssl
Congratulations! You have successfully enabled https://matomo.example.com
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"

Il rinnovo automatico è gestito dal timer di systemd di Certbot; per verificarlo in qualsiasi momento:

certbot renew --dry-run

Conclusione

Matomo è installato su Debian 11 e raggiungibile via HTTPS. Non resta che inserire lo snippet di tracciamento nelle pagine: la dashboard inizierà a popolarsi e, trattandosi di un'installazione self-hosted, i dati raccolti restano di proprietà di chi gestisce il server. Per domande, lasciate un commento.


Debian
  1. Come installare MariaDB 10.x su Debian 11 e renderlo sicuro

  2. Installare Matomo Web Analytics su Ubuntu 20.04

  3. Installare Open Web Analytics su Debian 9

  4. Come installare Fathom Analytics su Debian 11

  5. Come Installare MariaDB su Debian 11

Installare Matomo su Ubuntu 18.04 LTS

Come installare MariaDB 10.6 su Debian 11 Bullseye

Come Installare MariaDB su Debian 8

Come Installare Nextcloud su Debian 8

Come Installare OwnCloud su Debian 9

Come installare Matomo su Debian 9 Stretch