Shurn the Awesomer

Piwik: Private Analytics

Written on Fri, 13 May 2016

Piwik is easily the best analytics for organisations who have a privacy policy that doesn't allow them to share information with third party. If you've tried Google Analytics, who will find that Piwik offers very similar functionalities. The biggest downside is probably that Google actually integrates its services seamlessly, while piwik is a purely standalone product. But the biggest advantage is that Piwik is entirely open sourced, so you can go ahead to modify or extend its functionalities.

Requirements


Minimum:

  • Webserver such as Apache, Nginx, IIS, etc
  • PHP5.3 or greater
  • MySQL version 4.1 or greater, or MariaDB
  • PHP extension pdo and pdo_mysql, or the mysqli extension

Recommended:

  • Webserver such as Apache, Nginx, IIS, etc
  • PHP7
  • Latest MySQL or MariaDB
  • PHP extension pdo and pdo_mysql, or the mysqli extension

Tip: Check out my other blog post for more information on setting up a web hosting with PHP7 and infinitely growing hard disk space.

Preparing the Server


Now I assume you have followed my previous blog post and installed PHP7 on Ubuntu 16.04. If not, do check it out! Let's prepare the server for Piwik installation.

Starting with the required libraries:

apt-get install php7.0-curl php7.0-gd php7.0-cli php7.0-dev libgeoip-dev

Interestingly, at the time of writing, GeoIP extension is not supported in PHP7. So we are going to need some help. Fortunately, Zakay from GitHub has compiled a solution. Let's hope there will be an official release of GeoIP extension for PHP7 soon.

We are going to get the codes from github:

git clone https://github.com/Zakay/geoip.git
cd geoip

Next, we compile it:

phpize
./configure --with-php-config=/usr/bin/php-config
make
make install

Now it is ready to be used. Let's put the settings in php.ini so that it will during runtime.

touch /etc/php/7.0/mods-available/geoip.ini
echo "extension = geoip.so"> /etc/php/7.0/mods-available/geoip.ini
phpenmod geoip
service apache2 restart

I hope you still have your phpinfo.php file in your webroot. Run the script in your browser and you should see GeoIP installed.

Next we configure your mysql so that it has a username for itself. It's always a good security measure that you assign each app to a single user and not shared.

mysql -p


Then enter your root password. If you have another username, do use the following command instead:

mysql -u anotherusername -p

You should now be at the mysql console. Enter the following:

CREATE DATABASE piwik_db_name_here;
CREATE USER 'piwik'@'localhost' IDENTIFIED BY 'password';
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES ON piwik_db_name_here.* TO 'piwik'@'localhost';
exit


Do substitute the names accordingly. Otherwise, your future administrators might think you're lazy.

Once you are done with that, you are absolutely good to proceed to piwik installation.

Installing Piwik


Now you are ready to download the piwik installation. You can download it to your computer, unzip, and upload it via ftp or sftp. But for pure linux sake, here's the commands to do it:

apt-get install unzip
wget http://builds.piwik.org/piwik.zip
unzip piwik.zip
mv piwik/* /var/www/html
rmdir piwik
rm How\ to\ install\ Piwik.html piwik.zip


The last few commands to remove is for cleaning up no longer needed files. It's always a good habit to clean up after yourself. Keep servers neat and tidy is important. The piwik is installed in the default webroot. If you are using it as a virtual host, you must configure apache accordingly, which is not covered in this tutorial.

If you browse to your website in your web browser, you should see your piwik installation. If the page shows an error /tmp/cache/tracker, you need to ensure that the tmp folder has enough write permission. It's also recommended that you keep the entire installation writable by apache so that piwik can do self-upgrade.

Go ahead to install piwik by following the instructions. And you're done! Congratulations!