Objective
Install Matomo analytics on Ubuntu 18.04 Bionic BeaverDistributions
Ubuntu 18.04Requirements
A working install of Ubuntu 18.04 with root privilegesDifficulty
EasyConventions
- # - requires given linux commands to be executed with root privileges either directly as a root user or by use of
sudo
command - $ - requires given linux commands to be executed as a regular non-privileged user
Introduction
Analytics are important for nearly every website, but if you're a fan of free and open source software, the idea of letting Google monitor all of your site's traffic might not sound too good. There is, however, another option. You can install and host your own analytics with Matomor(formerly Piwik). 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.Install the PHP Packages
Sure, you'll be basing this on either a standard LAMP or LEMP stack, but Matomo is a fairly large application with its own requirements. Before you get started, install these PHP dependencies.$ sudo apt install php-curl php-gd php-cli php-geoip php-mysql php-mbstring php-xml unzip
Set Up LAMP/LEMP
Matomo is a PHP application. You can host it like you would any other web app written in PHP. So, you're going to need to set Ubuntu up as either a LAMP or LEMP server. If you haven't done so already, use our traditional LAMP guide, our MariaDB LAMP guide, or our LEMP guide to set up Ubuntu to serve PHP.Subscribe to Linux Career NEWSLETTER and receive latest Linux news, jobs, career advice and tutorials.
Create A Database
Unless you created a database specifically for Matomo during your LAMP/LEMP setup, you're going to need to create a new one for Matomo to use. Sign in to MySQL as your root user.$ mysql -u root -pOnce you're in the MySQL console, create a new database.
mysql> CREATE DATABASE matomo;Create a new user for it too.
mysql> CREATE USER `matomo_admin`@`localhost` IDENTIFIED BY 'yourpass';Grant your new user permissions on the DB.
mysql> GRANT ALL ON matomo.* TO `matomo_admin`@`localhost`;Flush your privileges and exit.
mysql> FLUSH PRIVILEGES;
Get Matomo
Matomo is free and open source, but it's better to get it directly from the developers than from any distribution repositories. It just ensures that the version that you get is current. Grab the latest release withwget
. $ wget https://builds.matomo.org/piwik.zipThat link might change to reflect the name change from Piwik to Matomo in the near future. Be sure to look out for that. Unzip your file and copy it into your web root directory.
$ unzip piwik.zip
$ sudo cp -r piwik /var/www/
The result should be a piwik
directory at /var/www/piwik
. Change ownership of it to the web server. $ sudo chown -R www-data:www-data /var/www/piwik
Configure The Web Server
Your web server configuration is going to depend on whether you're using Apache or Nginx. Either one will assume that you're going to host on a server with more than one site using virtual hosts.Apache
You're going to need to create a new virtual host for your site. Start by copying either the default configuration or a previous configuration to modify to host Matomo.sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/matomo.confOpen your configuration and modify the
DocumentRoot
to point at where you unpacked the piwik
directory. DocumentRoot /var/www/piwikSet the
ServerName
to your site's domain(or localhost if you're just testing). This would most likely be the subdomain that you've chosen for your analytics. ServerName analytics.your-site.comWhen you're done, save your file and exit. Enable your site in Apache.
$ sudo a2ensite matomo.confReload Apache.
$ sudo systemctl reload apache2
Nginx
Create a new site configuration for Matomo in the/etc/nginx/sites-available
directory. Open that file. Everything here is completely standard for a PHP configuration. Create a new server block for your analytics site. It should look similar to this one. server {
listen 80;
listen [::]:80;
server_name analytics.your_site.com;
index index.php;
root /var/www/piwik;
access_log /var/log/nginx/analytics.your-site.com.access_log;
error_log /var/log/nginx/analytics.your-site.com.error_log;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
}
}
If you're using SSL(like Matomo recommends), include that and the 301 redirect as well. Don't forget to link your site configuration and restart Nginx. $ sudo ln -s /etc/nginx/sites-available/matomo /etc/nginx/sites-enabled/matomo
$ sudo systemctl restart nginx
Matomo Setup
Now that you have your web server and database configured, you can start to set up Matomo with it's web based installer. Navigate to the address where you configured your server to host Matomo.
The first screen will welcome you to Matomo and prompt you to begin the install process. After that, Matomo will perform a full system check for its requirements. This should be fine, since you installed them at the beginning of the process.


Next, Matomo will ask you to connect to the database. Enter the information that you used to set up your database earlier. It will take a couple of seconds to connect and tell you when it has done so successfully.


After your superuser, Matomo will ask to set up a website. This will add a site to the roster that Matomo will monitor and provide analytic data for. It will use the information that you provide to generate JavaScript tracking code.

Matomo will give you a block of JavaScript to insert into your site. Paste the code into your website's source in a place that will appear on every page. When you're done inserting the JavaScript into your site, you can click through the rest of the setup. Matomo will congratulate you on completing it when you're done.

It'll then send you to the login screen. Use the account that you created for yourself to sign in. When you do, you'll get a message that Matomo hasn't collected any data yet. That's fine. You just set it up. Tell Matomo not to show the message again for the next hour, and you can progress through to your dashboard. Matomo is running successfully on your server!