Piwik is free and open source web analytics tool written in PHP. Piwik uses MySQL/MariaDB to store its data. Piwik is an open source, self hosted alternative of Google analytics providing nearly as many features as Google analytics provides. Piwik helps you gather and analyse important information about the visitors of your website providing important performance indicators such as visits, goal conversion rates, downloads, keywords and many more. Piwik also provides extended analytics for ecommerce software such as orders, conversion rates, average order values and detailed product statistics. Piwik is used by more than 1 million websites including Wikimedia, Forbes, Sharp and T mobiles. Piwik also provides hundreds of plugins to easily integrate Piwik with different CMS and frameworks.
A few of the features provided by Piwik are :-
- Open Source software with no data storeage limits.
- Available in more than 53 languages and supports all time zones.
- Provides real time data updates with geolocation and customisable dashboard.
- Provides goal conversion, event and content tracking with site search analysis.
- Tracks traffic from search engines and provides a more accurate measure of the time spent by visitor on website.
- Provides scheduled PDF and Email reports and free Android and IOS application to track the website on the go.
In this tutorial we will learn to install Piwik on CentOS 7 server. We will also learn to add a new website to track along with setting up auto archiving.
Requirements
Piwik does not require any special hardware requirements. All the required dependencies will be installed throughout the tutorial. You will only need a root account or sudo access on your server. If you are logged in as non root user, run sudo -i
to switch to root user or you can also use sudo
command before all administrative commands.
Installing Piwik
Before installing any package it is recommended that you update the packages and repository using the following command.
yum -y update
Once you have your system updated, you can proceed to install the LAMP stack. Start the LAMP installation by installing Apache web server and MariaDB, which is a fork of MySQL using the following command.
yum -y install httpd mariadb-server mariadb
Piwik supports PHP versions greater than 5.5.9, but it is recommended that you install PHP 7 as it is more faster and memory efficient than older PHP versions. To install PHP 7 you will need to add Webtatic and EPEL repository in your system as PHP 7 is not available in the default YUM repository. To add EPEL repository run the following commands.
yum -y install epel-release
yum -y update
clean all
Now add the Webtatic repository in your system using the following commands.
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
yum -y update
yum clean all
Now install PHP 7 and the required modules using the following commands.
yum -y install php70w php70w-curl php70w-gd php70w-cli php70w-mysql php70w-mbstring php70w-dom
You can now check the PHP version using the following command.
php -v
You should see following output.
[root@ip-172-31-11-92 ~]# php -v
PHP 7.0.12 (cli) (built: Oct 15 2016 19:03:00) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
Now start Apache web server and enable it to start at boot time using following command.
systemctl start httpd
systemctl enable httpd
To start MariaDB and enable it to start at boot time using the following commands.
systemctl start mariadb
systemctl enable mariadb
Now run the following commands to secure your MySQL or MariaDB installation.
mysql_secure_installation
It will run a small script which which ask you to set a root password for your MariaDB installation. Most of the questions are self explanatory and you should answer yes for all the questions. The output of the command will look as shown below.
[root@ip-172-31-11-92 ~]# mysql_secure_installation
/bin/mysql_secure_installation: line 379: find_mysql_client: command not found
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
Set root password? [Y/n] Y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] Y
... Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] Y
... Success!
By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] Y
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] Y
... Success!
Cleaning up...
All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!
As we have all the dependencies installed, we can proceed to create a database to store the data of Piwik installation. To create a database we will need to login to MySQL command line first. Run the following command for same.
mysql -u root -p
This will prompt you for password, provide the root password of MySQL which you have set earlier. Now run the following query to create a new database for your Piwik installation.
CREATE DATABASE piwik_data;
The above query will create a database named piwik_data
. For the database you can use any name you prefer in the place of piwik_data
. Make sure that you use semicolon at the end of each query as a MySQL query always ends with a semicolon. Once the database is created you can create a new user and grant the required permissions to the user for the database. Using root user is not recommended for the databases which are exposed online. To create a new database user, run the following query.
CREATE USER 'piwik'@'localhost' IDENTIFIED BY 'password';
The above query will create a user with username piwik
. You can use any preferred username instead of piwik
. Replace password
with a strong password. Now provide the appropriate privileges to your database user over the database you have created. Run the following query to do so.
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES ON piwik_data.* TO 'piwik'@'localhost';
Now run the following query to immediately apply the changes on the database privileges.
FLUSH PRIVILEGES;
Now you can exit from MySQL prompt using following command.
exit
You will see the output similar to this.
[root@ip-172-31-11-92 ~]# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or g.
Your MariaDB connection id is 10
Server version: 5.5.50-MariaDB MariaDB Server
Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
MariaDB [(none)]> CREATE DATABASE piwik_data;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> CREATE USER 'piwik'@'localhost' IDENTIFIED BY 'password';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES ON piwik_data.* TO 'piwik'@'localhost';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> exit
Bye
Finally you will need to configure the system’s SELinux setting so that Apache user can get access to the database. Run the following commands for same.
setsebool -P httpd_unified on
setsebool -P httpd_can_network_connect_db on
Once you have the database and other dependencies ready you can now move to the default document root directory of Apache and download the Piwik files using the following commands.
cd /var/www/html
wget https://builds.piwik.org/piwik.zip
If you don’t have wget
installed, you can run yum -y install wget
to install it. Now extract the archive using the following command.
unzip piwik.zip
The above command will extract the files into piwik
sub directory. Now provide ownership of the files to apache user so that it can access the files using the following command.
chown -R apache:apache /var/www/html
You can also move all the files from piwik
sub directory to the document root directory so that the application can be accessed directly from the IP address of the server. Or you can also install choose to install the software in the subdirectory. To move all the files from the piwik directory to apache document root directory run the following command.
mv /var/www/html/piwik/* /var/www/html/
Or if you plan to add any other website to the document root, you can skip the last command. Your Piwik installation will be at http://your-server-ip/piwik
. In this tutorial we will consider that you have moved the files from piwik
directory to the document root directory and your Piwik installation is at http://your-server-IP
.
Now that we have everything ready, we will have to complete the installation of software through browser. If you have firewall running then you will have to run the following command so that port 80 and 443 is accessible on internet.
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
Once you access your Piwik installation using the browser, you will see following welcome screen.
HP_NO_IMG/data/uploads/users/1a010430-e647-458e-a84e-ec55c6ef8793/1660321323.png” alt=”” />
Click next to go to the system check page, the installer will now check if your server meets all the requirements necessary for installation of the software. As we have installed and enabled the requirements, you can click Next button to proceed further with installation.
HP_NO_IMG/data/uploads/users/1a010430-e647-458e-a84e-ec55c6ef8793/943486854.png” alt=”” />
In the next interface you will need to provide the database details. Leave the value of Database server as it is, provide the username, password and database name of the database you have created for storing the Piwik data. Click Next to proceed further.
HP_NO_IMG/data/uploads/users/1a010430-e647-458e-a84e-ec55c6ef8793/495213646.png” alt=”” />
If the database information and credentials are correct, the installer will automatically create the tables in the database and you will get a success message. If not then correct the database information you have provided.
HP_NO_IMG/data/uploads/users/1a010430-e647-458e-a84e-ec55c6ef8793/876065212.png” alt=”” />
In next interface you will need to create a superuser or administrator account. The superuser can perform administrative tasks such as adding new websites to monitor, adding users, changing user permissions, and enabling and disabling plugins. Provide the username, password and email according to your choice.
HP_NO_IMG/data/uploads/users/1a010430-e647-458e-a84e-ec55c6ef8793/1738482861.png” alt=”” />
In the next interface you will need to setup a website to track and analyse the data of website using Piwik. You can add more websites later when the software is successfully installed. Provide website’s name and URL, select the time zone and whether its e-commerce site or not. Click Next to proceed further.
HP_NO_IMG/data/uploads/users/1a010430-e647-458e-a84e-ec55c6ef8793/1249253699.png” alt=”” />
Now Piwik will generate the JavaScript tracking code for your website. If you want you can add the code immediately to your website. If you want to customise the tracking, you can choose to add the tracking code to your website later. If your website is built using a CMS, it is very easy to integrate the code on the website. There are hundreds of plugins built for almost every popular CMS including WordPress, Drupal, Joomla, Sharepoint etc. You will just need to install the plugin and add the JavaScript tracker code into the site. You can find the list of plugins here. If your website is not built on a CMS, you will have to add the code manually to every page you have to track. The code should be added just before the closing of “ tag. Click Next to finalize the installation.
HP_NO_IMG/data/uploads/users/1a010430-e647-458e-a84e-ec55c6ef8793/1086896366.png” alt=”” />
Finally you will see a congratulations message, At the end of the page you will find the few default Piwik settings, Enable Do Not Track support and Anonymize the last byte(s) of visitors IP addresses to comply with your local privacy laws/guidelines. If you enable Enable Do Not Track support then your Piwik installation will not track those users who has Do not Track enabled in their browser. If you enable Anonymize the last byte(s) of visitors IP addresses to comply with your local privacy laws/guidelines then the software will automatically anonymize the last two octet of the IP address before storing them to database. IP address anonymization is a mandatory law in privacy policies of few countries such as Germany. Choose the options accordingly. Finally click Continue to Piwik to go the Piwik sign in page.
HP_NO_IMG/data/uploads/users/1a010430-e647-458e-a84e-ec55c6ef8793/335560143.png” alt=”” />
Sign in with the superuser username as password which you have created earlier and you will taken to your Piwik dashboard. If you have setup the JavaScript tracking code to the website, it will show you the statistics recorded or it will show you the page at no data has been recorded.
HP_NO_IMG/data/uploads/users/1a010430-e647-458e-a84e-ec55c6ef8793/1073502454.png” alt=”” />
Installation is now finished, now you have your own analytical software up and running and collecting data.
Adding a New Website
To add a new website to track the analytics, click on All Websites link from the top bar. In All Websites dashboard you will see the list of websites that your Piwik installation is tracking. To add a new website, click on Add a new website link.
HP_NO_IMG/data/uploads/users/1a010430-e647-458e-a84e-ec55c6ef8793/998559518.png” alt=”” />
You will be taken to the Manage Websites section of administration page. You will get a form where you can add a new website.
HP_NO_IMG/data/uploads/users/1a010430-e647-458e-a84e-ec55c6ef8793/1946465172.png” alt=”enter image description here” />
Provide a name for your website. In URL field, enter the URLs of your website one per line. You can provides the subdomain and internal domain which you want to track. You do not have to add www
version and non-www version separately as Piwik considers both automatically. If you will select Only track visits and actions when the action URL starts with one of the above URLs. checkbox then Piwik will only track the insights and analytics of the internal links. Piwik will exclude the data of the page of which the URL does not starts with any of the URLs specified in URLs textbox.
HP_NO_IMG/data/uploads/users/1a010430-e647-458e-a84e-ec55c6ef8793/1144497478.png” alt=”” />
In the excluded IPs field, you can provide the IP address from which the web page will not be tracked, you can also use wildcard to add a range of IP address. In excluded Parameters enter the URL query parameters that should be excluded from URLs, for example session parameters. This will ensure your pages reports stay clean and easy to analyse. Piwik will automatically exclude the common session parameters. In Site search you can enable or disable the tracking of what your visitors are searching using your website’s internal search engine. You can use the default searching query parameters or you can specify your own. Finally select the timezone, currency and whether it is an ecommerce site or not. FInally click Save button and Piwik will add the site in the list of website.
HP_NO_IMG/data/uploads/users/1a010430-e647-458e-a84e-ec55c6ef8793/1315229118.png” alt=”” />
Now you can click on View tracking code link to view the tracking code, which you have to insert in your website.
Auto Archiving
If your website has more than a few hundreds visits per day, Piwik may take a few minutes to process the data. The best way to avoid the waiting time is to set up a cron job on your server so that your data is automatically processed every hour. You can also increase the PHP memory limit so that PHP can process the data much faster. To create a new crontab entry file run the following command.
crontab -e
It will open crontab file in VI editor. Go to insert mode by pressing i button. Now paste the following lines into the file.
5 * * * * /bin/php /var/www/html/console core:archive --url=http://your-server-IP > /var/log/piwik-archive.log
In MAILTO
parameter, enter your email address, if any error is occured during the execution of cron, an email will be sent to you. Next line tells cron to run every hour at 5 minutes past. /bin/php
is the path to PHP executable in CentOS 7. /var/www/html/console
is the path to the console as our Piwik files are in document root of Apache. --url
parameter tells the URL of the Piwik installation. Replace your-server-IP
with the public IP address of your server. /var/log/piwik-archive.log
is the path to the log file of cron job.
Save the file and exit from the editor by pressing esc button. and then entering :wq
. This will create a new cron job for your system.
To increase the PHP memory limit you will need to modify PHP configuration file. In CentOS 7, PHP configuration file can be found on /etc/php.ini
. Use your favorite text editor to edit the file.
nano /etc/php.ini
Scroll down to find the following lines in resource limits.
; Maximum amount of memory a script may consume (128MB)
; http://php.net/memory-limit
memory_limit = 128M
You can increase the limit to 256MB or more according to the memory available on your system. This will make the execution of the script faster.
Conclusion
In this detailed tutorial we have learned how to install Piwik Analytics on CentOS 7.x. We also learned how to add a new website on Piwik. We also learned how to auto archive the data every hour using cron jobs. You can now track the analytics of all the websites you have. You can also secure the Piwik installation by following How to secure your Apache using Certbot SSL.