How to install Matomo (formerly Piwik) web analytics software on our Apache server

botond published 2019/09/01, v - 16:01 time

Content

 

The 1. page content

 

 

Introductory

I finally had some time to write this longer description, where I can introduce the Matomo (formerly Piwik) is a web analytics software preparation, steps, and use for installation. I have been using this web statistics system for about a year and a half, it does a very good service. For me, for example, it has almost completely replaced Google Analytics. Here are some of the main reasons I recommend everyone to use it:

  • Points
  • overview
  • It has a convenient interface
  • It contains spectacular panels
  • Highly skilled (includes a variety of queries, variable cross sections, etc.)
  • The data I collect remains with me, not on other companies' servers
  • You can track statistics for any web page
  • Regular updates that can be installed by clicking 2.
  • Integrates with a wide range of CMS systems and web shop engines

So if you run a website, whether you own it or your client, this is a must! Their official website: here.

Matomo's system can be used in two ways: they provide the background on a cloud basis - similar to Google Analytics and other services - or we install the free downloaded software package on our server and run it ourselves to measure our websites. In this description, we will use the latter way: we will install it on an existing server, which will then provide us with accurate statistics about the traffic of the websites running on the server. The software is installed in one Apache web server we need it.

Long description!
This description is not only about installing the Matomo (Piwik) system, but also about its preparations and the precise execution of the necessary settings (eg cron, log file management, GeoIP configuration, building custom tracking codes, PHP-FPM configuration, etc.), therefore very long. I have tried to compile all my own experience and information in this multi-page post, which I also gathered during my nearly two years of using Matomo, so that I can provide more than just a simple installation guide, as you would normally find on the internet. This statistical system is constantly doing me an excellent service, so I want it to benefit others as well.

 

basic Conditions

You will need one of the following (pre-built) servers to complete this guide:

So these servers are all installed with Apache web server and contain the components needed by Matomo, so any one of them is suitable for writing this description.

Matomo (Piwik) System Requirements

And the software to be installed is you need the following, if you do not choose the base machine from the above servers:

  • Apache or Nginx Web Server (this is done with Apache, so you will only need it now).
  • PHP 5.5.9 or later with the following extensions: pdo and pdo_mysql vagy mysqli, gd, xml, curl, and mbsting. PHP version 7.x is recommended.
  • MySQL 5.5 or later or equivalent MariaDB. MySQL 5.7+ recommended.
  • In addition, it is a good idea to have a web page (of any kind) where you can link your Matomo tracking code to test web stats.

So you need at least one Debian 8 LAMP server or a machine with a similar installation.

Of the above, I am doing this on version 9 of the bold-highlighted Debian 1.0 (Stretch) Perfect Server (which was the latest at the time), so the next version of the server will already include this installation.

 

 

Preparations

Luggage database and package updates

We don't do such in-depth work on the server right now, but it's always a good idea to upgrade to our luggage and our packages so we can use the latest version of everything. To do this, run shellin the appropriate apt-get commands as root:

apt-get update
apt-get -y upgrade

Creating a database and user

Although I'm one ISPConfig I chose the perfect server with a control panel, but not the operating panel create the database and its user. On the one hand, that listed above is simpler On LAMP servers you can do the same in the same way, and on the other hand, you can only create webpage-related databases in ISPConfig, which would not be in place right now, as this database is not linked to any webpage that might be running on the server; .

There are still two ways to create a database: from the command line or if one exists phpMyAdmin on the server, then with that. Now we are doing the command line method, which is faster than phishing in phpMyAdmin. To do this, run the mysql command as root or sudo and also log in to mySQL as root:

sudo mysql -u root -p

You will be prompted to enter your MySQL root password and enter it.

After successful login, we find ourselves in the console of our database server. Here we do the following:

  • Create a new database called "matomo"
  • Assign all database privileges to the newly created user named "matomo" with the password "matomo_db"
  • Clear authorization cache
  • Exit

For these, execute the following four command lines one by one in the console, of course you can choose the database name, user and password as desired (but it is advisable to choose the appropriate names consistently):

create database matomo;
GRANT ALL ON matomo.* TO 'matomo' IDENTIFIED BY 'matomo_db';
FLUSH PRIVILEGES;
exit;

Don't forget the semicolons at the end of the lines!

Normally, it looks like this:

Create a Matomo database and user in the console

Alternatively, if you want to verify the success of this operation from phpMyAdmin, please log in with the user you just created:

phpMyAdmin - Login with matomo

And you can see that the user you created can log in and the associated database is displayed on the left.

Hosting

In this step, we create the physical storage for the Matomo (Piwik) system from which the system will run. This is created in a directory that is also available in any server configuration.

As root, create a subdirectory, say under / opt:

mkdir /opt/matomo

Create a php file in your directory to test PHP and the required PHP plugins and settings:

nano /opt/matomo/phpinfo.php

And add the following lines:

1
2
3
<?PHP
    phpinfo();
?>

Save it, we'll have more work on that soon.

 

 

Configuring Apache Configuration

In this section, we set up a global Apache configuration that will allow the Matomo web analytics system to be accessed from under the web pages on the server. Thus, even customers can release their own domain their statistical accesses under their names.

Create a new Apache configuration file as root:

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

Then, include the following content:

Alias /matomo /opt/matomo

<Directory /opt/matomo>
        Options +FollowSymLinks
        Options -Indexes
        AllowOverride All
        Require all granted
        DirectoryIndex index.php index.html

#        <IfModule mod_php5.c>
        <IfModule mod_php7.c>
                AddType application/x-httpd-php .php
                php_flag display_errors On
                php_value session.gc_maxlifetime 3600
                # A következő beállítás a Matomo-nak kell:
                #php_value always_populate_raw_post_data -1
                # Ha gondok vannak a működéssel (php hibák) javasolják az opcache kikapcsolását.
                #php_flag opcache.enable Off
        </IfModule>

</Directory>

The configuration file - which is almost like one anyway Virtual hosting file - its lines mean the following:

  • Alias: This directive allows content placed in a local file structure outside the root of the document to be accessed from the outside. In this case, it will serve the content in the / opt / matomo / directory from the / matomo (virtual) directories under the web pages.
  • : Here we define a block in which the settings apply to the specified directory.
    • Options + FollowSymLinks: Symbolic links tracking in web content. 
    • Options -Indexes: This disables the list of directories that do not contain index.php, index.html, etc. files. This increases safety.
    • AllowOverride All: This allows the settings in the .htaccess files in the directories to override the global Apache configuration settings. Many systems use .htaccess files, so you should enable it.
    • Require all granted: This allows anyone to access this web content from the outside (for example, we do not require HTTP authentication, etc.).
    • DirectoryIndex index.php index.html: Load order of index file types. It looks for index.php first, then for html.
    • # : Define another block in which the settings are evaluated when the mod_php5 module is loaded. On systems with PHP5 (eg Debian 8 based systems) we use this and comment on the following.
    • : Same as the previous one, except this monitors the existence of the PHP7 Apache module. On newer systems (like Debian 9 or Ubuntu 18.04) we use this and comment on the previous one.
      • AddType application / x-httpd-php .php: We assign the .php extension to the appropriate MIME type. Based on this, Apache will know what to do with the .php files.
      • php_flag display_errors On: PHP Directive: Enable Error Display. We'll leave this at the beginning, and then once we've tested the Matomo and everything works properly, comment it out. While off, we can track PHP errors in the appropriate log files, but we can immediately notice errors in the browser.
      • php_value session.gc_maxlifetime 3600: PHP Global Variable: This is what I use to get exactly 1 hours of sessions. I do not like the factory default 20 minute setting. Of course, everyone should set the right value for them, there is no requirement here.
      • php_value always_populate_raw_post_data -1: PHP Global Variable: I found this in one of Matomo's descriptions that he needs this option. However, this global PHP variable has already been removed from PHP 7.0, so it only plays a role in PHP5.
      • #php_flag opcache.enable Off: PHP directive: By default it is commented out here. Earlier versions of Matomo had problems with the PHP opcache. In this case, error messages appeared in the AJAX panels of the statistics, and only an Apache restart solved the problem. If this happened, this directive could be used to disable PHP opcache at Matomo (documentation about this). It's been a long time since then, I think they could fix it, and I went through the PHP-FPM based Matomo configuration, which works differently (about this at the end of this tutorial). So in a live environment, since then, I haven't been able to re-trigger or test the problem. If the error persists during operation, remove the comment from the directive.

So these would be the settings for this Apache configuration, I hope I could provide some useful info to help everyone fine-tune their own settings if needed.

In essence, this is a simple configuration that runs PHP as an Apache module, which is the default run mode for virtually any Apache + PHP installation. At the end of the description, we’ll review how we can port all of this to PHP-FPM for greater performance and efficiency.

Once you have finished configuring and saving the file, enable this configuration below a2andconf command:

a2enconf matomo

If no fatal error occurs, it returns the following output:

Enabling conf matomo.
To activate the new configuration, you need to run:
  systemctl reload apache2
If you are running on a live server, it is highly recommended that you also check the syntax of your configuration, as Apache may not restart in the event of an error. In this case, the pages will remain idle until we resolve the error and restart Apache. To avoid this, run the config test on apache2ctl command:
apachectl configtest

For me on this ISPConfig server, it will output:

AH00548: NameVirtualHost has no effect and will be removed in the next release /etc/apache2/sites-enabled/000-ispconfig.conf:73
Syntax OK

Here at ISPConfig there is a warning that we are not dealing with (this does not appear on LAMP servers) and then write Syntax OK. So basically the enabled configurations are good, so now you can also restart Apache a systemctl command:

systemctl reload apache2

It does not output when successful. 

 

 

Testing your hosting

Finally, let's test our newly designed hosting, which includes the functionality of the Apache configuration and the appropriate PHP settings. Open the previously created phpinfo.php file in your browser.

There are several ways to access this storage on your server:

  • http://192.168.1.120/matomo/phpinfo.php
  • http://www.weboldal1.tld/matomo/phpinfo.php
  • http://www.weboldal2.tld/matomo/phpinfo.php
  • etc. ...

So on the one hand the server is available IP addresses / matomo directory, and if there are web pages on the server, it can be accessed from any of the / matomo subdirectories. After loading our php file, the output of the phpinfo () PHP function normally accepts us:

Testing your hosting

On this server before a WordPress page has been installed, whose domain name I have set in my hosts file, so this page comes to me.

Here you will see the "Apache 2.0 Handler" at the beginning of the Server API section, which means that this php file runs as an Apache module, and scroll down to check other PHP settings.

If you need to change the settings, you can do so in the php.ini file shown in the image: /etc/php/7.0/apache2/php.ini. For PHP5, the /etc/php5/apache2/php.ini file. Don't forget to restart Apache after making any changes!

 

A next page we continue with the installation of the Matomo (formerly known as Piwik) web analytics system.

 

 

Navigation

This description consists of several pages: