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

Matomo formerly known as Piwik is an open-source analytics application for the Linux operating system. It is very similar to Google Analytics that helps you to tracks and display the location of user visits. It was developed by a team of international developers that runs on a PHP/MySQL webserver. It offers a lot of features, some of them are listed below:

  • Flexibility, Reliability & security
  • Self-hosted, Simple, and easy to use
  • 100% Data Ownership
  • GDPR compliance
  • Web and mobile analytics

In this tutorial, we will show you how to install and setup Piwik analytics application on CentOS 8 server.

Prerequisites

  • A server running CentOS 8.
  • A valid domain name pointed with your server IP.
  • A root password is configured on your server.

Install LAMP Server

Piwik runs on a LAMP server so you will need to install the Apache, MariaDB, PHP and other PHP extensions to your system. You can install all of them 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

Once all the packages are installed, edit the php.ini file and set some desired 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 service and enable them to start at system reboot:

systemctl start mariadb
systemctl start httpd
systemctl enable mariadb
systemctl enable httpd

Create a Database for Piwik

Next, you will need to create a database and user for Piwik. First, login to MariaDB with the following command:

mysql

Once login, create a database and user with the following command:

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

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

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

Next, flush the privileges and exit from the MariaDB with the following command:

mysql> FLUSH PRIVILEGES;
mysql> EXIT;

Install Piwik

Next, you will need to download the latest version of Piwik from their official website. First, chnage the directory to the Apache web root with the following command:

cd /var/www/html

Next, download the Piwik using the following command:

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

Once the download is completed, unzip the downloaded file with the following command:

unzip matomo-latest.zip

Next, set proper permission and ownership to the web root with the following command:

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

Once you are finished, you can proceed to the next step.

Configure SELinux and Firewall

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

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

Next, reload the firewalld to apply the changes:

firewall-cmd --reload

Next, you will also need to setup SELinux for Piwik. You can setup it with the following command:

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

Once you are finished, you can proceed to the next step.

Configure Apache for Piwik

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

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

Add the following lines:

<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 finished. Then, restart the Apache web server to apply the changes:

systemctl restart httpd

You can now check the status of the 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 you are finished, you can proceed to the next step.

Access Piwik Web Interface

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

Piwik Installer

Click on the NEXT, you should see the system check page:

System check

Click on the NEXT, you should see the database setup page:

Database setup

Provide yur database username, database name, password and click on the NEXT, you should see the following page:

Creating database tables

Click on the NEXT, you should see the admin user creation page:

Add admin user account

Provide your admin username, password, email then click on the NEXT. You should see the website setup page:

Setup a website

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

JS Tracking code for Piwik

Log analytics

Click on the NEXT. Once the installation has been finished. You should see the following page:

Installation finished

Click on the CONTINUE TO MATOMO. You should see the MATOMO login page:

Matomo Login

Provide your admin username, password and click on the SIGN IN. You should see the MATOMO dashboard in the following page:

Statistics dashboard

Secure Matomo with Let's Encrypt SSL

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

You can install the Certbot client with the following command:

wget https://dl.eff.org/certbot-auto
mv certbot-auto /usr/local/bin/certbot-auto
chown root /usr/local/bin/certbot-auto
chmod 0755 /usr/local/bin/certbot-auto

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

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 or not 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 hit Enter to continue. Once the installation has been finished, 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 is secured with Let's Encrypt SSL.

Conclusion Congratulations! you have successfully installed and setup Piwik with Apache and Let's Encrypt SSL on CentOS 8. You can now add your tracking code in your website and start monitoring your website visitors from the Piwik dashboard. Feel free to ask me if you have any question.

Share this page:

0 Comment(s)