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
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.
Yay google is my king assisted me to locate this outstanding site !
Glad we could help!
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?
Hello Ralphelo,
This would certainly work on a Nginx server with multiple domains, its a single server block in the Nginx configuration so I cant really see any issues. I am not familiar with the particular github issue you are referencing, what did it state?
See here: https://github.com/perusio/piwik-nginx/issues/27#issuecomment-355383221
Yes our configuration will work for a multi site nginx config. Based on that github issue, they just provided a sample nginx config file for only the piwik install.
I ended up installing a specific piwk database and user, dropped the piwik folder in the main html folder in the “main site” and then proceeded to install piwik for two other sites on the same server. i didn’t change any existing nginx virtual hosts file at all. i appreciate your discussion.
Glad to hear you got it working!