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/phpsudo apt updatesudo 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.socklisten.owner = www-datalisten.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 = Offexpose_php = offmax_execution_time = 30max_input_time = 1000max_input_vars = 3000memory_limit = 512M post_max_size = 128Mupload_max_filesize = 128Mdate.timezone = Europe/Londonopcache.enable=1opcache.memory_consumption=128opcache.max_accelerated_files=10000opcache.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-backupsudo 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.1local-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/apache2sudo apt-get updatesudo apt-get install apache2 apache2-utilssudo a2enmod mime http2 rewrite deflate expires headers sslsudo apt-get install libapache2-mod-fcgidsudo a2enmod mpm_eventsudo a2enmod proxy_fcgi setenvifsudo a2enconf php8.1-fpmsudo systemctl restart php8.1-fpm
Now install Matomo
sudo apt install wget unzipcd ~wget https://builds.matomo.org/matomo-latest.zipsudo mkdir -p /var/www/matomosudo unzip matomo-latest.zipsudo mv matomo/* /var/www/matomosudo chown -R www-data:www-data /var/www/matomosudo 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.comDocumentRoot /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.confsudo a2ensite matomo.confsudo systemctl restart apache2
Secure your site with a SSL / TLS certificate
sudo apt install snapdsudo snap install core; sudo snap refresh coresudo snap install --classic certbotsudo ln -s /snap/bin/certbot /usr/bin/certbotsudo 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.
