Free web analytics usually come in two flavors, Google Analytics or Piwik.

While both solutions offer cloud hosting, Google is free to use and Piwik has paid cloud hosting. On one hand, Piwik guarantees that you have full control and ownership over your data and privacy (see Terms of Service). On the other hand, Google Analytics is held by US Law in terms of confidentiality.

To really have control of my data, I choose to host Piwik on a Raspberry Pi 3. It's well suited for a website with moderate hits. Here is how to install Piwik using Nginx. The following applies also to Debian and can probably be used for Red Had by using yum instead of apt-get.

Install dependencies

Connect to your Raspberry Pi using ssh for the following commands.

MySQL

sudo apt-get install -y MySQL-server

You will be prompted to create a master password for MySQL.

Create Piwik database

mysql -u root -p

Execute the following SQL commands. Don't forget to change the password.

create database piwik character set utf8;
create user 'piwik'@'localhost' identified by 'PIWIK-PASSWORD';
grant all privileges on piwik.* to 'piwik'@'localhost';
exit

Install PHP

Follow the instructions on installing PHP7 on a Raspberry Pi.

Install Piwik

Download Piwik

cd /var/www
wget http://builds.piwik.org/latest.zip
unzip latest.zip

Change Piwik folder ownership and rights

chmod -R 770 /var/www/piwik
chown -R www-data:www-data /var/www/piwik

Configure Nginx

Check that /etc/nginx/conf.d/*.conf files are included by Nginx. File /etc/ngin/nginx.conf should contain the following line.

include /etc/nginx/conf.d/*.conf;

Create Nginx configuration for Piwik

sudo vi /etc/nginx/conf.d/piwik.conf

Insert the following content

server {
    listen 80 ssl http2;
    listen [::]:80 ssl http2;

    server_name  localhost;

    root         /var/www/piwik;
    access_log /var/log/nginx/stats.access.log;
    error_log /var/log/nginx/stats.error.log;

    # Static
    location / {
        index  index.html index.htm index.php;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass   unix:/var/run/php/php7.0-fpm.sock;
        fastcgi_index  index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    # Security
        location ~ /\.ht {
        deny  all;
    }
    location /version {
        return 404;
    }
    location ~ /data/configuration/* {
        return 404;
    }

    location = /favicon.ico {
        access_log  off;
        return  204;
    }

    location ~* ^.+\.(jpg|jpeg|gif|css|png|js|xml)$ {
        expires  30d;
        #access_log off;
    }
}

Change server_name to your DNS name if you have one.

Check Nginx configuration

nginx -t

If everything is correctly configured, you can restart services.

sudo service php5-fpm restart
sudo service nginx restart

Configure Piwik

Connect to your Raspberry Pi address depending on the server_name you used in the Nginx configuration. You should see the installation landing page.

Piwik Install First Page

Follow the instructions, which depend on your version of Piwik. Use MySQL user piwik, the password you chose, and use piwik database.

You should get a tracking code to use on your website in order to follow visitors.

Privacy

Privacy is a big issue. Google Analytics has a free offer and a very decent one, established in 2005 and used by 10 million sites worldwide representing 80% of analytics usage. It's extremely good but comes at the cost of privacy.

French regulations

In order to be compliant with CNIL regulations, it is necessary to edit the tracker tag to let the user accept being tracked.

Mobile access

One of the good things about Piwik is that it has a mobile app to follow your website analytics.

Final words

Piwik seems to me like the best solution there is for tracking your website analytics while having control over them. It's also lightweight enough to be hosted on a Raspberry Pi. The obvious downsides of having a hosted solution being administration, uptime, and backup.