A Complete Guide on How to Install Matomo Web Analytics on Ubuntu 22.04
Posted on April 5th, 2024
Tracking website visits and displaying reports for data and audience analysis are both capabilities of Matomo, which was formerly known as Piwik. Matomo is a web analytics software that is open-source and free to use. Under the GPL Licence, Matomo is available for use. PHP was used to write it, and either MySQL or MariaDB can be used as the database server to run it.
User tracking on your websites can be accomplished with Matomo, which provides insightful reports. This encompasses the referrer search engine as well as the keywords, languages, page visits such as file visitors downloading, and a substantial number of other elements. As an alternative to analytics services such as Google Analytics, Matomo is gaining more and more popularity as a result.
This tutorial will walk you through the process of installing Matomo on a server running Ubuntu 22.04 and securing your web analytics services. Matomo web analytics will be installed along with PHP 8.1, Apache2 web server, and MariaDB database. Additionally, SSL/TLS certificates will be secured through the use of Certbot and Letsencrypt to ensure the deployment is secure. After you have completed this tutorial, you will be able to track and monitor the traffic that is coming to your websites. Additionally, you will receive detailed tracking results on your Matomo installation that is hosted on your server.
Essential requirements/Prerequisites
Check that all of the following conditions are met before continuing with this tutorial:
- The Ubuntu server used in this case is Ubuntu 22.04 with the hostname “matomo-server.”
- You must be a non-root user with sudo/root administrator privileges.
- When the server’s IP address is used in a domain name, the subdomain “analytics.hwdomain.io” is used.
Upon verifying that every prerequisite has been fulfilled, you are now able to begin.
Configuring and Installing MariaDB Server
As a database backend, Matomo, an open-source web analytics application, utilized either MariaDB or MySQL. As of the time of writing, Matomo mandated a minimum system requirement of MySQL Server v8 or MariaDB v10.x. You will install and utilize MariaDB as the database for Matomo web analytics in this tutorial.
Installing the MariaDB server, configuring the MariaDB deployment, and creating a new MariaDB database and user comprise the initial step.
Execute the apt command below to update and refresh your package index prior to beginning.
sudo apt update
Apply the MariaDB server using the apt command provided below. To proceed, enter y and press the ENTER key when prompted.
sudo apt install mariadb-server
Please run the following systemctl command to make sure that the MariaDB server is up and running after it has been loaded.
sudo systemctl enabled mariadb
sudo systemctl status mariadb
This is what the results should look like: The MariaDB server is functional and turned on by default. The MariaDB server will also start up instantly when the system does.
The MariaDB server package includes the command ‘mariadb-secure-installation,’ which you will then use to secure the MariaDB server deployment.
Run the following command to secure the MariaDB server deployment.
sudo mariadb-secure-installation
You’ll be asked about the following MariaDB configurations:
Replace local authentication with the unix_socket parameter. Enter n.
Establish a new root password for MariaDB. Enter y followed by the new password for your MariaDB server deployment in order to verify.
Eliminate the anonymous visitor? Enter y to verify.
Eliminate the database test by default from the deployment. Enter y to verify.
Prohibit root login to MariaDB from remote connections. Enter y to verify.
Modify the table privileges and reload them. After entering y, press ENTER.
The MariaDB server deployment should now be secured with the password entered for the MariaDB root user.
Create a new MariaDB database and user for the Matomo installation using the MariaDB shell.
Run the mariadb
command to access the MariaDB shell as the MariaDB root user. When prompted for the password, enter your MariaDB root password.
sudo mariadb -u root -p
Now, use the following queries to create a new MariaDB database and user. In this example, you will set up the database and user ‘matomo’ for the Matomo installation. Also, make sure to replace the default password in 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;
Proceed by executing the following query in order to validate the privileges list of the matomo@localhost user.
SHOW GRANTS FOR 'matomo'@'localhost';
quit
You need to obtain an output comparable to this: The MariaDB user ‘matomo@localhost’ has all the privileges to access the ‘matomo’ database.
The mariadb-secure-installation
command was utilized to secure the MariaDB server, while a new Matomo database and user were created in this section. Subsequently, configure PHP 8.x and the Apache2 web server.
PHP 8.x and the Apache2 web server should then be configured after finishing.
Installation of PHP 8 and Apache2 Web Server
Matomo is an open-source PHP-based internet analytics tool. The most recent version of Matomo is fully compatible with PHP 8.x. Furthermore, Matomo is compatible with a variety of web servers, including Apache2 and Nginx.
This step installs the Apache2 web server and PHP 8.1. Furthermore, the PHP installation script is executed, and the Apache2 and PHP versions are confirmed.
By default, Apache2 and PHP are installed from the Ubuntu repository. Matomo’s implementation approach is fully compatible with the framework’s current version, PHP8.1. This version can be obtained in the Ubuntu 22.04 repository.
sudo apt install apache2 php php-cli libapache2-mod-php
sudo apt install php-common php-curl php-gd php-mbstring php-mysql php-xml php-intl php-zip wget unzip
Enter y to confirm the input, then ENTER when prompted.
Once Apache2 and PHP have been installed, run the following systemctl command to confirm that the Apache2 service is running.
sudo systemctl is-enabled apache2
sudo systemctl status apache2
The following message will appear: “Azure2 is now enabled and will be launched automatically during system startup.” Additionally, the Apache2 service is working
Use the following nano editing command to open the Apache2 ‘php.ini’ file.
sudo nano /etc/php/8.1/apache2/php.ini
Add the following lines to the usual setup of php.ini. Change the date.Memory_limit and timezone should be checked against what is happening now.
date.timezone = Europe/Stockholm
memory_limit = 256M
upload_max_filesize = 16M
max_execution_time = 300
Save and exit the file when finished.
To use the changes, use the systemctl command tool to restart the Apache2 service.
sudo systemctl restart apache2
The Apache2 web server should now use the changed php.ini setup file.
Running the following PHP command will tell you what version of PHP is running on your computer. Check the file to see if the allowed PHP extensions are still there.
sudo php --version
sudo php -m
This display should show up: There is now PHP 8.1 on the Ubuntu server. Also, make sure that the PHP features that Matomo needs are turned on.
You are now done with the installation of Matomo web analytics. The requirements you installed were the MariaDB server, the Apache2 web server, and PHP 8.1.
To move on to the next step, you will need to download the Matomo source code and make sure that the Matomo installation location has the correct permissions. This will let PHP and Apache2 read the Matomo app and run it.
Downloading Matomo Source Code
What you will do in this last step is download the Matomo source code and make sure that the Matomo startup location has the correct permissions.
Now change your working location to “/var/www/,” and then use the wget command to get the source code for the Mozart software.
cd /var/www/
wget https://builds.matomo.org/matomo.zip
With the unzip command, which is given below, you can remove the Matomo source code after you have gotten it. Besides that, you need to add the new directory “matomo” to the working directory you are in at the moment.
unzip matomo.zip
After you finish this, the location where you installed Matomo should be “/var/www/matomo.”
Use the following command to give the ‘www-data’ user and group control of the Matomo installation directory, which is at /var/www/matomo.
sudo chown -R www-data:www-data /var/www/matomo
This is what you need to do after downloading the Matomo source code and setting up the place where the installation will happen. The next step is to create a new setup for the virtual host that will be used to run Matomo web analytics.
Setting up Virtual Host Matomo
Before you begin, double-check that the domain name refers to the IP address of your server. In this scenario, the Matomo download is performed on the subdomain “analytics.hwdomain.io.”
Follow these nano editing procedures to create a new Apache2 virtual host file named ‘/etc/apache2/sites-available/matomo.conf‘.
sudo nano /etc/apache2/sites-available/matomo.conf
Add the following lines to the file, making sure to replace the domain name with your domain.
ServerAdmin webmaster@localhost
ServerName analytics.hwdomain.io
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
When you are completed, save the file and quit the editor.
Next, use the following command to activate the new virtual host configuration’matomo.conf’. Then, validate the Apache2 settings to confirm that you have the correct setup with no errors.
sudo a2ensite matomo.conf
sudo apachectl configtest
When you have appropriately configured Apache2, you should get an output like ‘Syntax OK.’
Finally, use the systemctl command below to restart Apache2 and make the changes take effect.
sudo systemctl restart apache2
This is the last step in setting up the Apache2 virtual host for the Matomo web analytics plugin. This means that you can now use an online browser to connect to your Maomo from outside the network. But this still uses HTTP links that aren’t safe.
In the next step, you’ll use Certbot and Letsencrypt to protect your Matomo web analytics system with SSL/TLS certificates.
Securing Matomo with SSL/TLS Via Certbot and Letsencrypt
For the Apache2 web server, this step is where you will add the Certbot tool and plugin. After that, you’ll make new SSL/TLS certificates for Matomo’s web statistics.
Make sure that your domain name points to your server’s IP address before you start. Also, you need an email address that you can use to sign up for Letsencrypt.
After that, run the following apt command to add the Certbot tool and the Apache2 patch for Certbot.
sudo apt install certbot python3-certbot-apache2
When asked, type “y” and press “Enter” to move on.
After that, run the following certbot script to make SSL/TLS certificates for your domain name. In the following command, make sure to change the site name and the email address.
sudo certbot --apache2 --agree-tos --redirect --hsts --staple-ocsp --email [email protected] -d analytics.hwdomain.io
Once the process is done, your Apache web server should now run your Mato web analytics with safe HTTPS turned on. You have also set up the auto-redirect from HTTP to HTTPS and turned on the OSCP standard.
Installing Matomo Web Analytics
Open your web browser and go to https://analytics.hwdomain.io, which is the domain name for your Matomo installation. The welcome page for Matomo should now appear, as seen below.
It will begin after you click “Next.”
The Matomo installer will now examine the server environments before proceeding with the installation. Ensure that all prerequisites and configurations are verified in green, as shown below.
You must also check that the primary criteria are satisfied in accordance with Matomo system specifications.
Click Next to proceed.
You will now configure the MariaDB database details for your Matomo web analytics. Make sure you provide the proper database login and password. Then click Next again.
If your database information is accurate, the Matomo online installer should automatically generate tables.
Click Next to proceed.
The next step is to add a new master user to your Matomo web analytics account. For your Matomo manager account, make sure you choose a strong password. After that, click Next again.
Here is where you will set up your website’s first tracking. Now, choose the time zone for the website’s domain name, and then click “Next.”
The JavaScript code you need to put on your pages should now be shown to you. To move on, click Next again.
Now you should see a sign saying “Congratulations!” This means that the Matomo installation is done. At the bottom of the page, click the button that says “CONTINUE TO MATOMO.”
Right now, you’ll be taken to a safe HTTPS link for the Matomo management area. Put in your Matomo login name and password, and then click “SIGN IN.”
You can get to the Matomo web analytics web management if you know the admin account and password.
Now, go to the upper right area and click on “All Websites.” This will show you how your website is being monitored right now. You should be able to see the page tracking that you set up when you installed Matomo.
It’s now time to finish setting up Matomo Web Analytics. Using self-hosted Matomo online tracking, you add more websites and start keeping an eye on them.
Conclusion
In this article, you installed Matomo open-source web analytics on an Ubuntu 22.04 server using the MariaDB database server, Apache2 web server, and PHP 8.1. You’ve also secured Matomo with SSL/TLS certificates generated by Certbot and Letsencrypt.
Now that you’ve installed the basic Matomo web analytics, you can integrate it with CMS (Content Management Systems) like WordPress, TYPO3, Joomla, and Drupal. You can also integrate with eCommerce applications like Magento, Prestashop, WooCommerce, and OpenCart.