How to Install Matomo Web Analytics on Ubuntu 22.04

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

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

In this tutorial, you will set up and secure your web analytics with Matomo on a Ubuntu 22.04 server. You will install Matomo web analytics with PHP 8.1, Apache2 web server, and MariaDB database and secure the deployment with SSL/TLS certificates via Certbot and Letsencrypt. Once you have finished this tutorial, you will be able to track, and monitor traffics on your websites, and you will get details tracking results on your self-hosted Matomo installation.

Prerequisites

Below are the requirements that you must have to complete this tutorial:

  • A Ubuntu 22.04 server - This example uses a Ubuntu server with the hostname 'matomo-server'.
  • A non-root user with sudo/root administrator privileges.
  • A domain name pointed to the server IP address - This example will use the sub-domain 'analytics.hwdomain.io'.

Once all requirements in place, you can now get started.

Installing and Configuring MariaDB Server

Matomo is an open-source web analytics that used MySQL or MariaDB as the database backend. At the time of this writing, Matomo required at least the MySQL Server v8 or MariaDB v10.x. In this guide, you will install and use MariaDB as the database for Matomo web analytics.

In the first step, you will install the MariaDB server, set up the MariaDB deployment, and create a new MariaDB database and user.

Before you get started, run the below apt command to update and refresh your package index.

sudo apt update

Now install the MariaDB server via the apt command below. When prompted, input y and press ENTER to proceed.

sudo apt install mariadb-server

install mariadb

After the MariaDB server is installed, run the below systemctl command utility to verify and ensure that the MariaDB server is running.

sudo systemctl is-enabled mariadb
sudo systemctl status mariadb

You should receive an output similar to this - The MariaDB server is enabled and running by default. Also, the MariaDB server will start automatically upon the bootup.

verify mariadb

Next, you will secure the MariaDB server deployment via the command 'mariadb-secure-installation', which is provided by the MariaDB server package.

Run the below command to secure the MariaDB server deployment.

sudo mariadb-secure-installation

You will now be asked about the following MariaDB 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.

Now the MariaDB server deployment should be secured and you've added the password for the MariaDB root user.

Next, you will create a new MariaDB database and user for the Matomo installation via the MariaDB shell.

Run the below 'mariadb' command to log in to the MariaDB shell via the MariaDB root user. When asked for the password, input your MariaDB root password.

sudo mariadb -u root -p

Now run the following queries to create a new MariaDB database and user. In this example, you'll create the database and user 'matomo' for the Matomo installation. Also, be sure to replace the default password on the following query.

CREATE DATABASE matomo;
CREATE USER 'matomo'@'localhost' IDENTIFIED BY 'p4ssw0rddb';
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, INDEX, DROP, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES ON matomo.* TO 'matomo'@'localhost';
FLUSH PRIVILEGES;

create datbase and user

Next, run the below query to verify the list of privileges of the '[email protected]' user.

SHOW GRANTS FOR 'matomo'@'localhost';
quit

You should get an output similar to this - The MariaDB user '[email protected]' has all privileges to access the 'matomo' database.

show privileges

In this section, you have installed the MariaDB server, secured the MariaDB deployment via the 'mariadb-secure-installation' command, and created a new database and user for Matomo. Next, you'll install the Apache2 web server and PHP 8.x.

Installing Apache2 Web Server and PHP 8

Matomo is an open-source web analytics written in PHP. For the installation, the latest version of Matomo is fully supported with PHP 8.x. Also, Matomo can be run with Apache2 and Nginx web servers.

In this step, you'll now install the Apache2 web server and PHP 8.1, configure the PHP installation, then verify the Apache2 service and PHP version.

The default Ubuntu repository provides both Apache2 and PHP packages. The Ubuntu 22.04 repository provides the latest PHP version PHP8.1, which is fully supported for Matomo installation.

Run the below apt command to install Apache2 and PHP 8.1 packages.

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

When prompted, input y to confirm and press ENTER to proceed.

install apache2 php 8

After Apache2 and PHP is installed, run the below systemctl command utility to verify the Apache2 service and ensure that the service is running.

sudo systemctl is-enabled apache2
sudo systemctl status apache2

You'll receive an output like this - The Apache2 service is enabled and will be run automatically upon the bootup. And the status of the Apache2 service is running.

verify apache2 service

Next, open the 'php.ini' file for Apache2 via the below nano editor command.

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

Change the default php.ini configuration with the following lines. Adjust the date.timezone and memory_limit value with your current environment.

date.timezone = Europe/Stockholm
memory_limit = 256M
upload_max_filesize = 16M
max_execution_time = 300

Save and exit the file when finished.

Now run the below systemctl command utility to restart the Apache2 service and apply the changes.

sudo systemctl restart apache2

The Apache2 web server should now be running with the new php.ini config file.

Lastly, run the below php command to verify the PHP version that is installed on your system. Then verify the list of PHP extensions that are enabled.

sudo php --version
sudo php -m

You should receive an output similar to this - PHP 8.1 is installed on your Ubuntu system. Also, ensure that PHP extensions that are required for Matomo are enabled.

verify php version

Now, you have finished installing dependencies for Matomo web analytics, including the MariaDB server, Apache2 web server, and PHP 8.1.

In the next step, you will download the Matomo source code and set up the proper permission for the Matomo installation directory so the PHP and Apache2 can read and execute the Matomo application.

Downloading Matomo Source Code

In this step, you will download the Matomo source code and set up the proper permission for the Matomo installation directory.

Change your current working directory to '/var/www/' and download the MAtomo source code via the wget command.

cd /var/www/
wget https://builds.matomo.org/matomo.zip

After the matomo source code is downloaded, extract the Matomo source via unzip command below. And you should get the new directory 'matomo' on your current working directory.

unzip matomo.zip

With this, your Matomo installation directory should be '/var/www/matomo'.

Run the below command to change the ownership of the Matomo installation directory '/var/www/matomo' to the 'www-data' user and group.

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

After you've configured the Matomo installation directory and downloaded the Matomo source code. Next, you'll create a new virtual host configuration that will be used to run Matomo web analytics.

Setting up Virtual Host Matomo

Before you get started, ensure that you have the domain name pointed to your server IP address. This example uses the sub-domain 'analytics.hwdomain.io' for the Matomo installation.

Create a new Apache2 virtual host file '/etc/apache2/sites-available/matomo.conf' using the below nano editor command.

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

Add the following lines to the file and ensure to change the domain name with your domain.

<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName analytics.hwdomain.io
    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>

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

</VirtualHost>

Save the file and exit the editor when you're finished.

Next, run the below command to activate the new virtual host configuration 'matomo.conf'. Then verify the Apache2 configurations to ensure that you have the proper configuration has no error.

sudo a2ensite matomo.conf
sudo apachectl configtest

You should receive the output such as 'Syntax OK' when you have proper Apache2 configurations.

setup virtual host matomo

Lastly, run the below systemctl command utility to restart the Apache2 service and apply the changes.

sudo systemctl restart apache2

With this, you have now finished the configuration of the Apache2 virtual host for the Matomo web analytics. This means that your Maomo is now accessible from outside the network and you can access it via the web browser. But, this still uses insecure HTTP connections.

In the next step, you'll secure your Matomo web analytics installation with SSL/TLS certificates via Certbot and Letsencrypt.

Securing Matomo with SSL/TLS via Certbot and Letsencrypt

In this step, you will install the Certbot tool and plugin for the Apache2 web server. Then, you'll generate new SSL/TLS certificates for the Matomo web analytics.

Before you start, ensure that your domain name is pointed to your server IP address. Also, you need an email address that will be used for registering to Letsencrypt.

Now run the below apt command to install the Certbot tool and the Certbot plugin for the Apache2 web server.

sudo apt install certbot python3-certbot-apache2

Input y when prompted and press ENTER to proceed.

install certbot

Next, run the below certbot command to generate SSL/TLS certificates for your domain name. Ensure to change the domain name and the email address in the following command.

sudo certbot --apache2 --agree-tos --redirect --hsts --staple-ocsp --email [email protected] -d analytics.hwdomain.io

After the process is finished, your Mato web analytics should now be running with secure HTTPS enabled on top of your Apache web server. Also, you have the auto-redirect configured from HTTP to HTTPS and the OSCP staple enabled.

Installing Matomo Web Analytics

Open up your web browser and visit the domain name of your matomo installation (i.e: https://analytics.hwdomain.io). You should now get the welcome page to Matomo as below screenshot.

Click Next to start the installation.

welcome to matomo

Now the Matomo installer will be checking the server environments for the installation. Ensure that all requirements and configured is is checked with green color as below.

Matomo system checks

Also, you must ensure that the main requirements is met with Matomo system requirements.

setup database

Click Next to continue.

You'll now set up the MariaDB database details for your Matomo web analytics. Ensure to input the correct database username and password. Then, click Next again.

database setup

The matomo web installer should automatically create tables when your database details is correct.

Click Next to continue.

tables created

Next, you'll need to create a new admin user for your Matomo web analytics installation. Be sure to use a strong password for your Matomo admin user. Then click Next again.

create admin

In this, you will set up the first tracking for your website. In the domain name of your website that you want to track, select the timezone, then click Next.

setup first tracking

Now you should see the JavaScript code that you must input into your websites. Click Next again to continue.

tracking code JavaScript

You should now get the congratulations message that you have finished the Matomo installation. Click the button CONTINUE TO MATOMO at the bottom of the page.

installation finished

You'll now be redirected to a secure HTTPS connection for the Matomo administration login page. Input your Matomo username and password, then click SIGN IN.

Matomo login page

If you have the correct admin user and password, you should get Matomo web analytics web administration.

matomo dashboard

Now click the All Websites menu on the top right to get your website's list of active tracking. You should see the first website tracking that you created during the Matomo installation.

Matomo list sites

You have now finished the installation of Matomo Web Analytics. You add more websites and start tracking via self-hosted Matomo web analytics.

Conclusion

In this article, you have installed the Matomo open-source web analytics with MariaDB database server, Apache2 web server, and PHP 8.1 on a Ubuntu 22.04 server. You have also secured the Matomo with SSL/TLS certificates generated via Certbot and Letsencrypt.

Now that you have got the basic Matomo web analytics is installed, you can now integrate Matomo with CMS (Content Management System) such as WordPress, TYPO3, Joomla, Drupal, etc. Also, you can integrate with eCommerce applications such as Magento, Prestashop, Woocomeerce, and OpenCart.

Share this page:

Suggested articles

0 Comment(s)

Add comment