Matomo, formerly Piwik, is free and open-source web analytics software that can be used to track website visits and display reports for data and audience analysis. Matomo is licensed under GPL License, written in PHP, and can run with MySQL or a MariaDB database server.

Matomo provides insightful reports for user tracking on your websites. This includes the referrer search engine and keywords, languages, page visits, file visitors’ downloads, and more. Matomo has become increasingly popular as an alternative to analytics services such as Google Analytics.

In this tutorial, you’ll learn how to install Matomo web analytics on the Debian 12 server. You’ll set up Matomo with the LAMP Stack and secure Matomo with HTTPS through Certbot and Letsencrypt.

Prerequisites

Before you start, make sure you have the following:

  • A Debian 12 server.
  • A non-root user with administrator privileges.
  • A domain name pointed to a server IP address.

Installing dependencies

Matomo is a PHP-based web analytics that uses MySQL/MariaDB as the database. To install Matomo, you must install LAMP (Linux, Apache, MySQL/MariaDB, and PHP) or LEMP (Linux, Nginx, MySQL/MariaDB, and PHP-FPM) Stack on your system. In this example, you’ll run Matomo with the LAMP Stack on the Debian server.

First, run the apt command below to update your Debian package index.

sudo apt update

Now install the LAMP Stack (Linux, Apache, MySQL/MariaDB, and PHP) packages with the following command:

sudo apt install apache2 mariadb-server php php-cli libapache2-mod-php php-common php-curl php-gd php-mbstring php-mysql php-xml php-intl php-zip wget unzip

Input Y to confirm the installation.

<img alt="install deps" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/07/echo/1-install-deps.png668840ad0847c.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="302" loading="lazy" src="data:image/svg xml,” width=”750″>

After the installation is complete, check the MariaDB server status with the command below. You’ll see the MariaDB server with the status active (running) and enabled.

sudo systemctl is-enabled mariadb

sudo systemctl status mariadb

<img alt="check mariadb" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/07/echo/2-check-mariadb.png668840ad262ec.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="266" loading="lazy" src="data:image/svg xml,” width=”750″>

Now check the Apache service status with the following command. In the following output, you can see the Apache web server is running.

sudo systemctl is-enabled apache2

sudo systemctl status apache2

<img alt="check apache" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/07/echo/3-check-apache23.png668840ad58e6d.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="251" loading="lazy" src="data:image/svg xml,” width=”750″>

Lastly, run the php command below to check the PHP version. You’ll see PHP 8.3 is installed.

php -v

<img alt="check php" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/07/echo/4-check-php.png668840ad777d8.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="162" loading="lazy" src="data:image/svg xml,” width=”669″>

Configuring PHP

After installing LAMP Stack, you’ll configure PHP installation by editing the php.ini file and restarting the Apache web server.

Open the default PHP configuration /etc/php/8.3/apache2/php.ini with the following nano editor.

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

Change the default configuration like the following – Make sure to change both the date.timezone and memory_limit options with your current server environment.

date.timezone = Europe/Stockholm

memory_limit = 256M

upload_max_filesize = 16M

max_execution_time = 300

Save the file and exit the editor.

Now run the systemctl command below to restart the Apache web server and apply your changes to PHP.

sudo systemctl restart apache2

Configuring MariaDB server

In this section, you’ll secure the MariaDB server and set up the root password for it. Then, you’ll create a new database and user that will be used by Matomo.

Run the mariadb-secure-installation command below to secure MariaDB and set up the root password.

sudo mariadb-secure-installation

Now you’ll be asked about the following configurations:

  • Switch local authentication to unix_socket? Input n.
  • Set up the new MariaDB root password. Input y to confirm, then type the new password for your MariaDB server deployment.
  • Remove anonymous user? Input y to confirm.
  • Remove the default database test from the deployment?. Input y to confirm.
  • Disallow MariaDB root login from remote connections? Input y to confirm.
  • Reload table privileges and apply the changes? Input y and press ENTER.

Next, run the mariadb command below to log in to the MariaDB server. Enter your root password when asked.

sudo mariadb -u root -p

Now run the following queries to create a new database and user matomo with the password p4ssword. Adjust the database details below.

CREATE DATABASE matomo;

CREATE USER 'matomo'@'localhost' IDENTIFIED BY 'p4ssword';

GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, INDEX, DROP, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES ON matomo.* TO 'matomo'@'localhost';

FLUSH PRIVILEGES;

<img alt="create database and user" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/07/echo/5-create-db-user.png668840ad9852e.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="128" loading="lazy" src="data:image/svg xml,” width=”750″>

Lastly, run the query below to check user matomo@localhost and exit from the MariaDB server. This will ensure that user matomo@localhost can access the database matomo.

SHOW GRANTS FOR matomo@localhost;

quit

<img alt="check db user" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/07/echo/6-check-user.png668840adbc8df.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="150" loading="lazy" src="data:image/svg xml,” width=”750″>

Downloading Matomo source code

Now that you’ve configured PHP and created a database and user, you can install Matomo. Now you’ll download the Matomo source code, set up the document root directory, and then change the ownership with the proper Apache user.

Go to the /var/www Directory and download the Matomo source code using the wget Command below.

cd /var/www/

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

Once downloaded, run the unzip command below to extract the matomo.zip file. And then change the ownership of the /var/www/matomo directory to the user www-data.

unzip matomo.zip

sudo chown -R www-data:www-data /var/www/matomo

Setting up Apache virtual host

After you’ve downloaded Matomo, you’ll create a new Apache virtual host file for Matomo. So make sure your domain name is pointed to a server IP address.

Create a new Apache virtual host file /etc/apache2/sites-available/matomo.conf with the nano editor.

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

Insert the configuration below and make sure to replace the ServerName option with your domain name.


    ServerAdmin webmaster@localhost
    ServerName matomo.howtoforge.local
    DocumentRoot /var/www/matomo/

    
        DirectoryIndex index.php
        Options FollowSymLinks
        AllowOverride All
        Require all granted
    

    
        Options None
        Require all denied
    

    
        Options None
        Require all granted
    

    
        Options None
        Require all denied
    

    
        Options None
        Require all denied
    

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


Save the file and exit when finished.

Now run the a2ensite command below to activate the matomo.conf file. Then run the apachectl command below to verify your Apache configuration. If you’ve proper Apache syntax, you’ll see an output Syntax is OK.

sudo a2ensite matomo.conf

sudo apachectl configtest

Lastly, run the systemctl command below to restart the Apache web server and apply your changes. Once Apache restarted, your Matomo installation is ready.

sudo systemctl restart apache2

<img alt="setup apache" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/07/echo/7-setup-apache.png668840ade2195.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="300" loading="lazy" src="data:image/svg xml,” width=”692″>

Securing Matomo with HTTPS

In this section, you’ll generate SSL/TLS certificates and secure Matomo with HTTPS through Certbot and Letsencrypt.

Install the certbot and python3-certbot-apache packages with the command below.

sudo apt install certbot python3-certbot-apache

Now run the certbot command below to generate SSL certificates and secure your matomo domain name. Make sure to change the domain name and email address with your information.

sudo certbot --apache --agree-tos --redirect --hsts --staple-ocsp --email [email protected] -d matomo.howtoforge.local

After the process is complete, your SSL certificates will be available in the /etc/letsencrypt/live/donmain.com directory. Also, your Matomo installation is secured with HTTPS.

Installing Matomo web analytics

Open your web browser and visit your Matomo domain name such as https://matomo.howtoforge.local/. if the installation is successful, you’ll see the welcome message like the following:

Click Next to confirm the installation.

<img alt="welcome message" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/07/echo/8-welcome.png668840ae12404.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="290" loading="lazy" src="data:image/svg xml,” width=”750″>

On the System Check section, ensure your system is ready and click Next again.

<img alt="system check" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/07/echo/9-system-check.png668840ae356d8.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="411" loading="lazy" src="data:image/svg xml,” width=”750″>

Now input the details, including the MariaDB database name, user, and password, and then click Next to migrate the database.

<img alt="setup database" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/07/echo/10-db-setup.png668840ae54b1c.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="431" loading="lazy" src="data:image/svg xml,” width=”750″>

Once migrated, click Next again.

<img alt="database migration" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/07/echo/11-tables-created.png668840ae79093.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="295" loading="lazy" src="data:image/svg xml,” width=”750″>

Enter a new administrator username, email address, and password for Matomo web analytics.

<img alt="setup admin" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/07/echo/12-setup-admin.png668840ae94f0a.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="474" loading="lazy" src="data:image/svg xml,” width=”750″>

Now enter information to create the first tracking with Matomo and click Next.

<img alt="create first tracking" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/07/echo/13-first-tracking.png668840aeb6250.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="453" loading="lazy" src="data:image/svg xml,” width=”750″>

Copy the generated tracking code and click Next.

<img alt="tracking code" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/07/echo/14-tracking-code.png668840af048b8.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="412" loading="lazy" src="data:image/svg xml,” width=”750″>

After the installation is complete, you’ll see the following messages:

<img alt="install complete" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/07/echo/15-install-complete.png668840af25447.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="316" loading="lazy" src="data:image/svg xml,” width=”750″>

If you visit the Matomo home page, you’ll be redirected to the matomo login page. Enter your admin user, and password, and click SIGN IN.

<img alt="logging in" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/07/echo/16-login.png668840af4865f.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="675" loading="lazy" src="data:image/svg xml,” width=”692″>

After logging in, you’ll see the following Matomo administration dashboard:

<img alt="dashboard" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/07/echo/17-dasboard.png668840af742ce.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="379" loading="lazy" src="data:image/svg xml,” width=”750″>

Conclusion

Congratulations! You’ve completed the installation of Matomo web analytics on the Debian 12 server. Matomo web analytics is up and running with the Apache web server, MariaDB database server, and PHP 8.3. You also secure Matomo with HTTPS through Certbot and Letsencrypt.