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
- 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 admin@example.com 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:
