How to install Matomo (Piwik) Web Analytics on CentOS 8

How to install Matomo (Piwik) Web Analytics on CentOS 8

Matomo (previously known as Piwik) is an open source analysis application for the Linux operating system. It is very similar to Google Analytics, which helps you track and display where users visit. It is developed by a group of international developers and they run on PHP/MySQL web servers. It provides many functions, some of which are listed below:

  • Flexibility, reliability and safety
  • Self-hosted, easy to use
  • 100% data ownership
  • Comply with GDPR
  • Network and mobile analysis

In this tutorial, we will show you how to install and set up the Piwik analysis application on a CentOS 8 server.

prerequisites

  • Server running CentOS 8.
  • A valid domain name pointing to the server IP.
  • A root password is configured on your server.

Install LAMP server

Piwik runs on a LAMP server, so you need to install Apache, MariaDB, PHP and other PHP extensions on your system. You can install all components with the following command:

dnf install httpd mariadb-server php php-mysqlnd php-fpm unzip wget php-json php-dom php-gd php-mbstring -y

After installing all the packages, edit the php.ini file and set some required values:

nano /etc/php.ini

Change the following values:

upload_max_filesize = 10M
post_max_size = 10M
max_execution_time = 300
max_input_time = 300
memory_limit = 256M

Save and close the file, then start the Apache and MariaDB services and enable them to start when the system reboots:

systemctl start mariadbsystemctl start httpdsystemctl enable mariadbsystemctl enable httpd

Create a database for Piwik

Next, you will need to create a database and users for Piwik. First, log in to MariaDB using the following command:

mysql

After logging in, use the following commands to create the database and user:

mysql> CREATE DATABASE matomo;mysql> CREATE USER `matomo`@`localhost` IDENTIFIED BY 'password';

Next, use the following command to grant all privileges to the database:

mysql> GRANT ALL ON matomo.* TO `matomo`@`localhost`;

Next, refresh the privileges and exit from MariaDB using the following command:

mysql> FLUSH PRIVILEGES;mysql> EXIT;

Install Piwik

Next, you need to download the latest version of Piwik from its official website. First, use the following command to change the directory to the Apache web root directory:

cd /var/www/html

Next, download Piwik using the following command:

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

After the download is complete, use the following command to decompress the downloaded file:

unzip matomo-latest.zip

Next, use the following command to set appropriate permissions and ownership for the web root directory:

chown -R apache:apache /var/www/html/matomochmod -R 775 /var/www/html/matomo

Once completed, you can proceed to the next step.

Configure SELinux and firewall

Next, you will need to allow ports 80 and 443 to pass through the firewall. You can allow them with the following command:

firewall-cmd --permanent --zone=public --add-service=httpfirewall-cmd --permanent --zone=public --add-service=https

Next, reload firewalld to apply the changes:

firewall-cmd --reload

Next, you also need to set up SELinux for Piwik. You can use the following command to set:

chcon -R -t httpd_sys_rw_content_t /var/www/html/matomo/setsebool httpd_can_network_connect on -P

Once completed, you can proceed to the next step.

Configure Apache for Piwik

Next, you will need to configure the Apache web server to host the Piwik website. You can do this by creating a new Apache virtual host configuration file:

nano /etc/httpd/conf.d/piwik.conf

Add the following line:

<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot "/var/www/html/matomo"
ServerName piwik.example.com
<Directory "/var/www/html/matomo/">
Options MultiViews FollowSymlinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
TransferLog /var/log/httpd/matomo_access.log
ErrorLog /var/log/httpd/matomo_error.log

</VirtualHost>

Save and close the file when you are done. Then, restart the Apache web server to apply the changes:

systemctl restart httpd

Now, you can check the status of Apache with the following command:

systemctl status httpd

You should get the following output:

? httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
  Drop-In: /usr/lib/systemd/system/httpd.service.d
           ??php-fpm.conf
   Active: active (running) since Sun 2020-12-27 05:38:29 EST; 5s ago
     Docs: man:httpd.service(8)
 Main PID: 4228 (httpd)
   Status: "Started, listening on: port 80"
    Tasks: 213 (limit: 12523)
   Memory: 36.8M
   CGroup: /system.slice/httpd.service
           ??4228 /usr/sbin/httpd -DFOREGROUND
           ??4229 /usr/sbin/httpd -DFOREGROUND
           ??4230 /usr/sbin/httpd -DFOREGROUND
           ??4231 /usr/sbin/httpd -DFOREGROUND
           ??4232 /usr/sbin/httpd -DFOREGROUND

Dec 27 05:38:28 centos8 systemd[1]: Stopped The Apache HTTP Server.
Dec 27 05:38:28 centos8 systemd[1]: Starting The Apache HTTP Server...

Once completed, you can proceed to the next step.

Access Piwik web interface

Now, open your web browser and use the URL to access the Piwik web interface. http://piwik.example.com. You will be redirected to the following page:

Click on next, You should see the system check page:

System check

How to install Matomo (Piwik) Web Analytics on CentOS 8

Click on next, You should see the database settings page:

Database settings

Provide your database user name, database name, password, and click next, You should see the following page:

Create database tableadvertising

Click on next, You should see the admin user creation page:

Add an administrator user account

Provide your administrator username, password, email, and click next. You should see the website settings page:

Build a website

Provide your website details and click next. You should see your tracking code on the following page:

Piwik's JS tracking code

Log analysis

Click on next. After the installation is complete. You should see the following page:

The installation is complete

Click on Continue to friends. You should see the MATOMO login page:

Matomo login

Provide your administrator username and password, and click Sign in. You should see the MATOMO dashboard on the following page:

Statistics Information Center

Let’s encrypt SSL to protect Matomo

Next, you will need to install the Certbot utility in your system to download and install “Let’s Encrypt SSL” for the Piwik website.

You can install the Certbot client using the following command:

wget https://dl.eff.org/certbot-automv certbot-auto /usr/local/bin/certbot-autochown root /usr/local/bin/certbot-autochmod 0755 /usr/local/bin/certbot-auto

Next, use the following command to obtain and install an SSL certificate for your Mantis website:

certbot-auto --apache -d piwik.example.com
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator apache, Installer apache
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): [email protected]

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v02.api.letsencrypt.org/directory
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(A)gree/(C)ancel: A

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about our work
encrypting the web, EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y 


Obtaining a new certificate
Performing the following challenges:
http-01 challenge for piwik.example.com
Waiting for verification...
Cleaning up challenges
Deploying Certificate to VirtualHost /etc/httpd/conf.d/piwik.conf

Next, you will need to choose whether to redirect HTTP traffic to HTTPS, as shown below:

Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2

Type 2 and press Enter to continue. After the installation is complete, you should see the following output:

Redirecting all traffic on port 80 to ssl in /etc/httpd/conf.d/piwik.conf

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations! You have successfully enabled https://piwik.example.com

You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=piwik.example.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/piwik.example.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/piwik.example.com/privkey.pem
   Your cert will expire on 2020-03-23. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot-auto
   again with the "certonly" option. To non-interactively renew *all*
   of your certificates, run "certbot-auto renew"
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le

At this point, your website has been protected with “Let’s Encrypt SSL”.

Conclusion Congratulations! You have successfully installed and set up Piwik using Let’s Encrypt SSL on Apache and CentOS 8. You can now add tracking code to your website and start monitoring website visitors from the Piwik dashboard. If you have any questions, feel free to ask me.

Sidebar