• Home
  • About Us
  • Contact
  • Privacy Policy

LinuxAdmin.io

  • Home
  • Tutorials
    • System Administration
    • Linux Tutorials
    • Web Hosting
    • WordPress
    • Virtualization
    • Troubleshooting
    • OpenStack

Piwik Analytics on Nginx

piwik-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 [email protected] 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.

Jun 5, 2017LinuxAdmin.io
0 0 vote
Article Rating
Blocking Bad UserAgents with ModSecurity and Fail2banNginx Directory Protection
You Might Also Like
 
PHP-FPM With PHP7 From Source
 
KVM Virsh Console Access on CentOS 7
Subscribe
Notify of
guest
guest
8 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
eDOKj1lE
eDOKj1lE
5 years ago

Yay google is my king assisted me to locate this outstanding site !

0
Reply
LinuxAdmin.io
Author
LinuxAdmin.io
5 years ago
Reply to  eDOKj1lE

Glad we could help!

0
Reply
Ralphelo
Ralphelo
5 years ago

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?

0
Reply
LinuxAdmin.io
Author
LinuxAdmin.io
5 years ago
Reply to  Ralphelo

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?

0
Reply
Ralphelo
Ralphelo
5 years ago
Reply to  LinuxAdmin.io

See here: https://github.com/perusio/piwik-nginx/issues/27#issuecomment-355383221

0
Reply
LinuxAdmin.io
Author
LinuxAdmin.io
5 years ago
Reply to  Ralphelo

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.

0
Reply
Ralphelo
Ralphelo
5 years ago
Reply to  LinuxAdmin.io

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.

0
Reply
LinuxAdmin.io
Author
LinuxAdmin.io
5 years ago
Reply to  Ralphelo

Glad to hear you got it working!

0
Reply
5 years ago 8 Comments Linux Tutorials, Nginx, Web Hosting analytics, centos, linux1,508
Recent Posts
  • Laravel Installation Guide For CentOS
  • Openstack Services Explanation And Overview
  • OSSEC Intrusion Detection Installation On Centos 7
  • Configure ProFTPd for SFTP on CentOS
  • How To Check And Repair MyISAM Tables In MySQL
Most Commented
Hot Clone Linux Server with Rsync
Hot Clone A CentOS Server With Rsync
14 Comments
ngx_cache_purge module
Install The ngx_cache_purge Module In Nginx
8 Comments
piwik-nginx
Piwik Analytics on Nginx
8 Comments
Tags
linuxcentosLinux Performance Tuningsysadminkvmnetworkingmemcachedanalyticssystemd
About

We love Linux and are dedicated to creating Linux administration tutorials for System Administrators since 2016.

Most Viewed
Default Gateway
How To Configure A Default Gateway on CentOS
62,997 views
Zend Opcache
Setup and Optimize Zend OpCache
46,714 views
Install ffmpeg
FFMpeg Install On CentOS 7
20,912 views
Archives
Email subscription

Sign up for our newsletter to receive the latest news and event postings.

2018 © LinuxAdmin.io
wpDiscuz