Friday, July 19, 2024
HomeHow toHow to Install Matomo Web Analytics on Debian 12

How to Install Matomo Web Analytics on Debian 12

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.

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

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

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

php -v

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;

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

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

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.

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

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

Once migrated, click Next again.

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

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

Copy the generated tracking code and click Next.

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

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.

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

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.


RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here


Most Popular