Matomo

Install your own web analytics software using Matomo

published on

Most people these days are using Google Analytics to track the number of people viewing their site, what they do on the site etc.

If you would like to keep track of the same kind of things that you can with Google Analytics but you want control over your own data (perhaps for GDPR purposes) then why not give Matomo a try?

This is how you can install Matomo on your own server using Apache, PHP 8.1 and MariaDB.

Install PHP 8.1 and a few other tools

sudo add-apt-repository -y ppa:ondrej/php
sudo apt update
sudo apt install php8.1-{cli, fpm, mysql, gd, soap, mbstring, bcmath, common, xml, curl, imagick, bz2, intl, zip, ldap, opcache, imap, imagick, gd, mcrypt, ssh2}
sudo apt install unzip ghostscript imagemagick

Now we need to make a few edits to two configuration files. The first can be edited by running sudo nano /etc/php/8.1/fpm/pool.d/www.conf and making the necessary changes to the lines below (mainly uncommenting lines):

listen = /run/php/php8.1-fpm.sock
listen.owner = www-data
listen.mode = 0660

The second configuration file can be edited by running sudo nano /etc/php/8.1/fpm/php.ini and adding or editing the below lines:

output_buffering = Off
expose_php = off
max_execution_time = 30
max_input_time = 1000
max_input_vars = 3000
memory_limit = 512M       
post_max_size = 128M
upload_max_filesize = 128M
date.timezone = Europe/London
opcache.enable=1
opcache.memory_consumption=128
opcache.max_accelerated_files=10000
opcache.revalidate_freq=200

Finally, restart the PHP service with sudo systemctl restart php8.1-fpm.

Now install the MariaDB database

The following two commands will install MariaDB and tools necessary for backing databases up. The second of the commands will secure the MariaDB installation.

sudo apt-get install mariadb-server mariadb-backup
sudo mysql_secure_installation

There are two other changes which I will make to a configuration file by running sudo nano /etc/mysql/mariadb.conf.d/50-server.cnf and making sure the two lines below appear as shown here.

bind-address = 127.0.0.1
local-infile = 0

and in the [sqld] section add, or edit, the following line to read as follows:

max_allowed_packet = 128MB

Finally restart the MariaDB service by running sudo systemctl restart mariadb

.Set up the Matomo database

Enter sudo mysql -u root -p at the command line and type the following commands at the MariaDB command prompt.

CREATE DATABASE matomodb charset=utf8mb4;
CREATE USER 'matomo'@'localhost' IDENTIFIED BY 'my_password';
GRANT ALL PRIVILEGES ON matomodb.* to 'matomo'@'localhost';
FLUSH PRIVILEGES;
exit

Make sure to change the database name, user name and password to something more secure.

Install the Apache web server

sudo add-apt-repository ppa:ondrej/apache2
sudo apt-get update
sudo apt-get install apache2 apache2-utils
sudo a2enmod mime http2 rewrite deflate expires headers ssl
sudo apt-get install libapache2-mod-fcgid
sudo a2enmod mpm_event
sudo a2enmod proxy_fcgi setenvif
sudo a2enconf php8.1-fpm
sudo systemctl restart php8.1-fpm

Now install Matomo

sudo apt install wget unzip
cd ~
wget https://builds.matomo.org/matomo-latest.zip
sudo mkdir -p /var/www/matomo
sudo unzip matomo-latest.zip
sudo mv matomo/* /var/www/matomo
sudo chown -R www-data:www-data /var/www/matomo
sudo chmod -R 755 /var/www/matomo

Create the Apache virtual host configuration

To do this run sudo nano /etc/apache2/sites-available/matomo.conf and add the following lines

<VirtualHost *:80>
ServerAdmin [email protected]
ServerName example.com
DocumentRoot /var/www/matomo/

    <Directory /var/www/matomo>
       DirectoryIndex index.php
       Options FollowSymLinks
       AllowOverride All
       Require all granted
    </Directory>

    <Files "console">
       Options None
       Require all denied
    </Files>

    <Directory /var/www/matomo/misc/user>
       Options None
       Require all granted
    </Directory>

    <Directory /var/www/matomo/misc>
       Options None
       Require all denied
    </Directory>

    <Directory /var/www/matomo/vendor>
       Options None
       Require all denied
    </Directory>

      <FilesMatch "\.php$">
        SetHandler "proxy:unix:/run/php/php8.1-fpm.sock|fcgi://localhost"
      </FilesMatch> 

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

</VirtualHost>

Now run the following commands to disable the default site configuration and set up the one above.

sudo a2dissite 000-default.conf
sudo a2ensite matomo.conf
sudo systemctl restart apache2

Secure your site with a SSL / TLS certificate

sudo apt install snapd
sudo snap install core; sudo snap refresh core
sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbot
sudo certbot -d [domain] -d www.[domain] --apache

Finish off the installation online

Visit your domain name in your browser to finish off the installation.

Once this is done, back on your server, set up a cron job to archive data every hour by running sudo nano /etc/cron.d/matomo-archive and adding the following lines

    MAILTO="your email address"
    # Runs at 5 minutes past the hour every hour
    5 * * * * www-data /usr/bin/php /var/www/matomo/console core:archive --url=https://mystats.site > /dev/null

Final thoughts

Like all other installations you will need to adapt your installation and configuration for your own use.

This guide should give you a good working copy of Matomo to put to work in your own websites though.

You can find some free videos at https://matomo.org/web-analytics-training/ to get you started and there is a lot of information at https://matomo.org/help/?menu if you get stuck.