How to Install Matomo Web Analytics on Debian 11

Matomo, formerly known as Piwik is a free and open-source web analytics application developed by a community of developers. It is used to track your website and give detailed information on your website and its visitors, including the search engines and keywords they used, the language they speak, which pages they like, the files they download and so much more. It offers a lot of features including, Google AdWords, Facebook Ads, Yahoo, Search Marketing, Tracking and Reporting API, and Cost Per Click (CPC).

In this tutorial, I will show you how to install Matomo on Debian 11 with an Apache2 web server and a free Let's Encrypt SSL certificate.

Prerequisites

  • A server running Debian 11.
  • A valid domain name pointed with your server IP.
  • A root password is configured on the server.

Install Apache, MariaDB and PHP

First, you will need to install the Apache web server, MariaDB database server, PHP and other PHP extensions to your server. You can install all of them by running the following command:

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

After installing all the packages, start and enable Apache and MariaDB service using the following command:

systemctl start apache2 mariadb
systemctl enable apache2 mariadb

Once you are finished, you can proceed to the next step.

Create a MariaDB Database for Matomo

Matomo uses a MariaDB as a database backend. So you will need to create a database and user for Matomo.

First, log in to MariaDB with the following command:

mysql

Once you are log in, create a database and user with the following command:

MariaDB [(none)]> CREATE DATABASE matomodb;
MariaDB [(none)]> CREATE USER 'matomo'@'localhost' IDENTIFIED BY 'password';

Next, grant all the privileges to Matomo database with the following command:

MariaDB [(none)]> GRANT ALL ON matomodb.* TO 'matomo'@'localhost' WITH GRANT OPTION;

Next, flush the privileges and exit from the MariaDB shell with the following command:

MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> EXIT;

After configuring the MariaDB database, you can proceed to the next step.

Download Matomo

Next, visit the Matomo official website and download the latest version of Matomo with the following command:

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

Once the download is completed, unzip the downloaded file with the following command:

unzip matomo-latest.zip

Next, move the extracted directory to the Apache web root directory:

mv matomo /var/www/html/matomo

Next, change the ownership and permission of the Matomo directory:

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

Once you are finished, you can proceed to the next step.

Configure Apache for Matomo

Next, you will need to create an Apache virtual host configuration file to host Matomo on the internet. You can create it with the following command:

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

Add the following lines:

<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>

Save and close the file when you are finished then activate the Matomo virtual host and Apache rewrite module with the following command:

a2ensite matomo.conf
a2enmod rewrite

Next, restart the Apache service to apply the changes:

systemctl restart apache2

You can also check the Apache status with the following command:

systemctl status apache2

You will get the following output:

? 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.

Access Matomo Web UI

Now, open your web browser and access the Matomo web interface using the URL http://matomo.example.com. You will be redirected to the following page:

Matomo web installer

Click on the Next button. You should see the PHP configuration check page:

PHP setting check

Make sure all configurations are ok then click on the NEXT button. You should see the MariaDB database setup page:

Database setup

Provide your database server, username, database name, password then click on the NEXT button. You should see the following page:

Creating database tables

Click on the NEXT button. You should see the Super User creation page:

Matomo super user

Provide your admin username, password, email, and click on the NEXT button. You should see the website setup page:

setup a website

Provide your website name, URL, time zone, and click on the NEXT button. You should see the following page:

Tracking code

Click on the NEXT button. You should see the configuration verification page:

Matomo settings

Click on the CONTINUE TO MATOMO. You should see the Matomo login page:

Matomo login

Provide your admin username, password, and click on the SIGN IN button. You should see the Matomo dashboard on the following page: You can now copy the Tracking code shown in the above image and paste it to every page on your website.

Matomo dashboard

Secure Matomo with Let's Encrypt SSL

Next, you will need to install the Certbot client to install the Let's Encrypt SSL for your website. You can install it with the following command:

dnf install epel-release -y dnf install certbot python3-certbot-apache

Next, obtain and install an SSL certificate for your lets domain with the following command:

certbot --apache -d matomo.example.com

You will be asked to provide your email address and accept the term of service:

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

Conclusion

Congratulations! you have successfully installed Matomo on Debian 11. You can now add tracking code to your website and start monitoring it from the Matomo dashboard. Feel free to ask me if you have any questions.

Share this page:

0 Comment(s)