How to Install Matomo Web Analytics on Ubuntu 20.04

Install Matomo Web Analytics on Ubuntu 20.04

In this article, we will have explained the necessary steps to install and configure Matomo Web Analytics on Ubuntu 20.04 LTS. Before continuing with this tutorial, make sure you are logged in as a user with sudo privileges. All the commands in this tutorial should be run as a non-root user.

Matomo is an open-source PHP application that you can run yourself, putting you in control of your analytics platform. Plus, Matomo actually respects your users and doesn’t try to break tracking protection. Matomo/Piwik is an alternative to Google Analytics software that gives you full control of your own website analytics and data without using third-party solutions. It is designed for small and medium-sized businesses that can be used to track Key Performance Indicators such as visits, downloads, goal conversion rates, keywords, and many more.

Install Matomo Web Analytics on Ubuntu 20.04

Step 1. First, before you start installing any package on your Ubuntu server, we always recommend making sure that all system packages are updated.

sudo apt update
sudo apt upgrade

Step 2. Install LAMP Stack.

Matomo is built with PHP. You can host it like you would any other web app is written in PHP. So, you’re going to need to set Ubuntu up as either a LAMP server. If you haven’t done so already, use our traditional LAMP guide to set up Ubuntu to serve PHP before you continue.

Step 3. Install Matomo/Piwik on the Ubuntu system.

Download the latest version of Matomo on your server:

wget https://builds.matomo.org/matomo-latest.zip

Once downloaded, extract the archive:

sudo mkdir -p /var/www/
sudo unzip matomo-latest.zip -d /var/www/

Change the owner and set the correct permissions for these files, you need to run the following command:

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

Step 4. Create a Database for Matomo.

Matomo uses the MariaDB database to store all its data like posts, pages, users, plugins, and themes settings. Log in to your MariaDB server with the following command and enter your MariaDB root password:

mysql -u root -p

Once you’re in the MariaDB console, create a new database:

CREATE DATABASE matomo_db;
CREATE USER matomo_user@localhost IDENTIFIED BY 'your-strong-password';
GRANT ALL PRIVILEGES ON matomo_db.* TO matomo_user@localhost;
FLUSH PRIVILEGES;
exit;

Step 5. Configuring Apache for Matomo.

We can now create our virtual host files. Run the following command to create the virtual host configuration file for your domain, your_domain.com:

nano /etc/apache2/sites-available/mydomain.com.conf

And add the following content to the file:

<VirtualHost *:80>

ServerAdmin admin@my_domain.com
ServerName mydomain.com
ServerAlias www.mydomain.com.com
DocumentRoot /var/www/matomo/

<Directory /var/www/matomo>
     Options Indexes FollowSymLinks
     AllowOverride All
     Require all granted
</Directory>

ErrorLog ${APACHE_LOG_DIR}/your_domain.com_error.log 
CustomLog ${APACHE_LOG_DIR}/your_domain.com_access.log combined 
</VirtualHost>

To enable the virtual host we have just created, run the following command:

ln -s /etc/apache2/sites-available/mydomain.com.com.conf /etc/apache2/sites-enabled/mydomain.com.conf

For the new configuration to take effect, restart the Apache service by typing:

sudo a2ensite mydomain.com.conf
sudo systemctl restart apache2

Step 6. Enable HTTPS.

We can enable HTTPS by installing a free TLS certificate issued from Let’s Encrypt:

sudo apt install certbot
sudo apt install python3-certbot-apache

Then, run this command to obtain and install a TLS certificate:

sudo certbot --apache --agree-tos --redirect --hsts --staple-ocsp --email you@example.com -d example.com

Step 7. Accessing Matomo.

Finally, steps, open your web browser, and type the URL https://your-domain.com. You will be redirected to the Matomo welcome screen:

Install Matomo Web Analytics on Ubuntu 20.04

That’s all you need to do to install Matomo Web Analytics on Ubuntu 20.04 Focal Fossa. I hope you find this quick tip helpful. If you have questions or suggestions, feel free to leave a comment below.