X

Piwik Analytics on Nginx

Piwik is a open source web site analytics software. It is free and opensource and can be used to track Nginx requests as well as Apache. This guide covers the Nginx configuration and installation of Pikwik. You can read more about Piwik here.

If you do not already have Nginx and PHP-PFM installed, please see the following guides

How To Install Nginx

How To Install PHP-FPM

 

Install Pikwik

Create a new directory to contain the piwik analytics data

mkdir /etc/nginx/stats.domain.com

Go to that directory

cd /etc/nginx/stats.domain.com

Download the latest version of Piwik

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

Un-compress it

unzip piwik.zip

Create a new database:

mysql -e "create database pikwik;"

Create a new database user:

mysql -e "grant all on pikwik.* to pikwik@localhost idenfied by 'PASSWORD';"

Configure Nginx

Create a new server configuration for stats.domain.com

nano /etc/nginx/stats.domain.com.conf

Insert the following, updating references to stats.domain.com with your domain name.

server {
 listen 80;
 server_name stats.domain.com;

access_log /etc/nginx/logs/stats.domain.com_access.log;
 error_log /etc/nginx/logs/stats.domain.com_error.log;

# Disable all methods besides HEAD, GET and POST.
 if ($request_method !~ ^(GET|HEAD|POST)$ ) {
 return 444;
 }

root /etc/nginx/stats.domain.com/;
 index index.php index.html;

# Disallow any usage of piwik assets if referer is non valid.
 location ~* ^.+\.(?:jpg|png|css|gif|jpeg|js|swf)$ {
 # Defining the valid referers.
 valid_referers none blocked *.domain.com;
 if ($invalid_referer) {
 return 444;
 }
 expires max;
 break;
 }

# Support for favicon. Return a 204 (No Content) if the favicon
 # doesn't exist.
 location = /favicon.ico {
 try_files /favicon.ico =204;
 }

# Try all locations and relay to index.php as a fallback.
 location / {
 try_files $uri /index.php;
 }
 #location ~* ^/(?:index|piwik)\.php$ {
location ~ \.php$ {
 try_files $uri =404;
 fastcgi_pass 127.0.0.1:9000;
 fastcgi_index index.php;
 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
 include fastcgi_params;


 }

# Any other attempt to access PHP files returns a 404.
 #location ~* ^.+\.php$ {
 # return 404;
 #}

# Return a 404 for all text files.
 location ~* ^/(?:README|LICENSE[^.]*|LEGALNOTICE)(?:\.txt)*$ {
 return 404;
 }

} # server

Edit the main nginx configuration file:

nano /etc/nginx/nginx.conf

Insert the following line at the end:

Include /etc/nginx/stats.domain.com.conf;

Restart nginx

service nginx restart

Once you have done that go ahead and visit stats.domain.com it will ask you to input the MySQL credentials you created earlier. After it has populated the database, it will provide you with code to insert into the site you wish to track. Once that is in place it will start generating the data for you to view inside the pikwik installation.

LinuxAdmin.io
3 1 vote
Article Rating
LinuxAdmin.io:

View Comments (8)

  • Does this work for nginx configurations with virtual hosts, ie, a nginx server providing many sites on a single host? It appears the official way is quite different (and doesn't work and not supported by piwik per github issue) or is this just for a single site on a single host?

Related Post