Install Matomo on Ubuntu 20.04

Updated on November 21, 2023
Install Matomo on Ubuntu 20.04 header image

Introduction

Matomo, formerly Piwik, is a free and open-source web analytics software based on PHP. It keeps track of visitors to your website and generates reports of metrics such as the number of visitors within a given period, the web browser they used, what they accessed, and so on. This is crucial information for businesses like e-commerce sites that require knowing the activity of buyers to improve their services. Matomo offers an option of self-hosting the software; this gives you a great deal of control and power over your data as you access it solely. Some important features of Matomo are:

  • It offers support from experts by providing guidance on how best to use Matomo.
  • Customization and extended functionality via widget configuration.
  • Advanced e-commerce analytics tools to improve online business.
  • Event tracking by measuring users interaction on websites and apps.
  • Geolocation to locate your visitors.
  • Import data from Google Analytics to your website.
  • Tracking personal data without violating privacy laws.
  • Detailed reporting from collected data for analysis.

This article explains how to install Matomo on Ubuntu 20.04 server.

Prerequisites

  • Deploy a fully updated Vultr Ubuntu 20.04 Server.
  • Create a non-root user with sudo access.
  • PHP version 7.2.5 or greater.
  • MySQL version 5.5 or greater, or MariaDB.
  • Web server such as Apache or Nginx.

1. Install Required Packages

  1. SSH to your server as a non-root user with sudo access.

  2. Update the system package list to update all packages to the latest available versions.

     $ sudo apt update
  3. By default, Ubuntu 20.04 contains PHP version 7.4 in the repository list. Install PHP 7.4 and more modules.

     $ sudo apt install apache2 mysql-server php7.4 libapache2-mod-php7.4 php7.4-json php7.4-curl php7.4-mysql php7.4-xml php7.4-zip php7.4-intl php7.4-gd php7.4-cli php7.4-mbstring wget unzip -y

2. Install Matomo

  1. Download the latest version of Matomo.

     $ wget https://builds.matomo.org/matomo-latest.zip
  2. Create the installation directory /var/www/matomo.

     $ sudo mkdir -p /var/www/matomo
  3. Extract the downloaded archive.

     $ sudo unzip matomo-latest.zip
  4. Move the extracted files to the installation directory.

     $ sudo mv matomo/* /var/www/matomo
  5. Change ownership of the installation directory.

     $ sudo chown -R www-data:www-data /var/www/matomo
  6. Change access permissions for the directory.

     $ sudo chmod -R 755 /var/www/matomo

3. Create Matomo Database

  1. Log in to MySQL shell. At the password prompt, just press Enter to continue.

     $ sudo mysql -u root -p
  2. Create a MySQL database named matomo.

     CREATE DATABASE matomo;
  3. Create a database user named matomouser with a password.

     CREATE USER 'matomouser'@'localhost' IDENTIFIED BY 'StrongPassword';
  4. Grant the user full access to the database.

     GRANT ALL ON matomo.* TO 'matomouser'@'localhost' WITH GRANT OPTION;
  5. Save the changes made on the database.

     FLUSH PRIVILEGES;
  6. Exit the MySQL shell.

     exit;

4. Configure Apache2

  1. Create an Apache virtual host configuration file named matomo.conf.

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

    Add the following code to the file. Save and close the file.

     <VirtualHost *:80>
     ServerAdmin admin@example.com
     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>
    
         ErrorLog ${APACHE_LOG_DIR}/matomo_error.log
         CustomLog ${APACHE_LOG_DIR}/matomo_access.log combined
    
     </VirtualHost>
  2. Disable Apache default configuration file.

     $ sudo a2dissite 000-default.conf
  3. Enable Matomo Apache configuration file.

     $ sudo a2ensite matomo.conf
  4. Enable Apache rewrite mode.

     $ sudo a2enmod rewrite
  5. Restart Apache service.

     $ sudo systemctl restart apache2

5. Access Matomo Web Interface

To access the Matomo Web Interface, go to your browser and visit http://Server_IP/. For example:

http://192.0.2.11/

Conclusion

You have installed Matomo on your server. First, access the Installation Wizard screen to complete installation by connecting to your database, creating an administrator account, and more settings. You can now access the Dashboard and configure it to begin adding a website to collect analytics data.

More Information

To learn more about using Matomo, go to the official documentation page.