Ditch Google Analytics and respect user privacy with this open source analytics platform.


Note: Piwik has changed it's name to Mamoto as of January 2018. They are the same project however don't confuse this with Piwik Pro.

If you read the nginx specific Piwik installation instructions on Github you will notice the process will destroy your existing nginx configuration which is totally not acceptable for those of us running multiple virtual hosts on a single nginx service. We took part in the conversation in bug #27 and after revewing several other sites' suggestions and a few local experiments the solution is pretty easy.

Since Piwik/Mamoto can track multiple sites from a single install things are kept tidy with a single administrative location that can be locked down later with junior users and can/should be segregated from your working databases.

Just to be clear, this is for a single server running nginx resolving multiple domains. We are assuming you already have a working SSL cert setup using Let's Encrypt for the domain in question. If you don't get that up and running prior and save some headaches!

First, decide what domain you would like to install Piwik/Mamoto on. For us, gripfastistech.com was the natural choice because 1) we want to honor visitors privacy before all else, 2) we only want to use open source software whenever possible, 3) Open Web Analytics was ok but not great and 4) Google is evil.

We're not sure where your chosen site's public folder is but generally its something like:

cd /var/www/yourexistingsite/html/

You'll need to download the latest version of Piwik/Mamoto and unzip it from https://builds.matomo.org/piwik.zip. At the time of writing this article the original piwik download location (included in the official install guide) was still viable at:

wget https://builds.piwik.org/piwik.zip && unzip piwik.zip

You'll now have a piwik folder in your public directory.

This is where things deviate from the official steps a little bit. Remember, you'll definitely want to update the permissions of the new piwik folder after you have successfully ran the installer but first we need to create a new database and user to manage piwik for us. Let's get into MariaDB:

mysql -u root -p

(then enter your password)

CREATE DATABASE piwikdb;

CREATE USER piwikadmin@localhost IDENTIFIED BY somepasswordyouwanttouse;

GRANT ALL PRIVILEGES ON piwik.* TO piwikadmin@localhost;

FLUSH PRIVILEGES;

exit

Now we have a database and rights to permit piwik's installation. Go to your domain /piwik.

You should see the installation pre-check. This is a thing of beauty. Our setup only required a single additional dependency to install on our server (it was apt-get install php-mbstring) for the installation to begin. Simply fill out the required information and process the installer.

You will be asked to create a super user to administrate piwik, it's sites and options.

Once you have your own tracking code (be sure to check the respect Do Not Track browser settings) its time to dump it into a site. For some that means you need to convert the JavaScript code so it can be run inside of php (just backslash escape all the quotations in the tracking code like this: \') or for others simply dump the Javascript into the header of pages you wish to track. Sadly the tracking code will not function if the code isn't in the head section rather than the more ideal place before the closing body tag (for website speed optimization).

At this point you need to check if the tracker is working. The easiest way is to just visit your own site and check the visitor log in the piwik dashboard for activity. If you blacklisted your IP in the tracking code you can use Pingdom Website Speed Test to generate some traffic on demand. Its especially great because you can choose a location to test from like Australia (which can then be easily found and deleted later in the piwik database).

After installation you MUST change the permissions of the piwik folder to 755 and the files to 644. The /tmp directory supposedly needs to remain writable but ours is also set to the former. We have since used the update function without issue from the Piwik/Mamoto control panel.

Also, we strongly recommend blacklisting all public access to your /piwik location in your virtual hosts file only permitting your own IP to access the page. Lock it down by default is the first step to good security practices.

Do you like to have 3rd party mega-corps storing your information? Do you think your visitors like it? No they don't. Respect your customers. Use analytics that respect your freedom. Use open source software. Use Grip Fast Information Services & Technology to build it. Contact us to get started!

About the Writer
Chris Lessley
Author: Chris Lessley
A server admin, dev ops warrior and website designer since 2002, Chris is a lover of all things Linux and open-source! Each blog topic has been tested by fire in the real world and shared with the hope to help others. Need more help? Hire me! Chris' other interests include fine art and the humanities in the classical tradition and can be found writing for our friends over at gripfastart.works. If you like this content, kindly consider donating to keep this website free to all, without ads.

Comments powered by CComment

Member of The Internet Defense LeagueOpen Source Initiative