robynover
By:
robynover

Alternatives to Google Analytics?

June 9, 2017 1.1k views
Logging Ubuntu 16.04

What web analytics package do you recommend as an alternative to Google Analytics? I see there is a tutorial on piwik, but it is 4 years old and out of date. Specifically, I need something that will work with nginx on Ubuntu (my site is running Node). Any chance you'd create a tutorial?

What do you use to track web traffic on your site?

3 Answers
jtittle1 June 9, 2017
Accepted Answer

@robynover

Awesome, you're 90% there already :-).

Essentially, what we'd do is create a similar server block and modify it to work with PHP-FPM. I'll use stats.domain.com in this example, but you're free to modify it.

First thing you want to do is install your PHP packages. Since you're using Ubuntu, that can be done using apt-get.

I generally install most all available packages, so the initial install command would be:

apt-get -y install php7.0-bcmath php7.0-bz2 php7.0-cli php7.0-common php7.0-curl php7.0-dev php7.0-enchant php7.0-fpm php7.0-gd php7.0-gmp php7.0-imap php7.0-interbase php7.0-intl php7.0-json php7.0-mbstring php7.0-mcrypt php7.0-mysql php7.0-opcache php7.0-pspell php7.0-readline php7.0-recode php7.0-soap php7.0-sqlite3 php7.0-tidy php7.0-xml php7.0-xmlrpc php7.0-xsl php7.0-zip

That'll handle pretty much everything, including what's required by PiWik.

With PHP-FPM installed, now we need to focus on the server block itself. Since we should be using SSL, I'll be setting up a near identical server block as what you're already using and modifying it for PHP.

server{
    listen 80;
    listen [::]:80;
    server_name stats.domain.com www.stats.domain.com;

    return 301 https://$server_name$request_uri;
}

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    server_name stats.domain.com www.stats.domain.com;

    include snippets/ssl-stats.domain.com.conf;
    include snippets/ssl-params.conf;

    root /var/www/html/stats;

    index index.html index.htm index.nginx-debian.html;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ /.well-known {
        allow all;
    }

    location ~ [^/]\.php(/|$) {
        fastcgi_split_path_info ^(.+?\.php)(/.*)$;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;

        include fastcgi_params;
    }
}

I recommend wiping fastcgi_params and adding the configuration below. To do that, we can run:

truncate -s 0 /etc/nginx/fastcgi_params

Then:

nano /etc/nginx/fastcgi_params

Now copy and paste the following to that file:

fastcgi_connect_timeout 60;
fastcgi_send_timeout 180;
fastcgi_read_timeout 180;
fastcgi_buffer_size 512k;
fastcgi_buffers 512 16k;
fastcgi_busy_buffers_size 1m;
fastcgi_temp_file_write_size 4m;
fastcgi_max_temp_file_size 4m;
fastcgi_intercept_errors off;

fastcgi_param SCRIPT_FILENAME   $request_filename;
fastcgi_param PATH_INFO         $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED   $document_root$fastcgi_path_info;
fastcgi_param QUERY_STRING      $query_string;
fastcgi_param REQUEST_METHOD    $request_method;
fastcgi_param CONTENT_TYPE      $content_type;
fastcgi_param CONTENT_LENGTH    $content_length;
fastcgi_param SCRIPT_NAME       $fastcgi_script_name;
fastcgi_param REQUEST_URI       $request_uri;
fastcgi_param DOCUMENT_URI      $document_uri;
fastcgi_param DOCUMENT_ROOT     $document_root;
fastcgi_param SERVER_PROTOCOL   $server_protocol;
fastcgi_param REQUEST_SCHEME    $scheme;
fastcgi_param HTTPS             $https if_not_empty;
fastcgi_param HTTP_PROXY        "";
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE   nginx/$nginx_version;
fastcgi_param REMOTE_ADDR       $remote_addr;
fastcgi_param REMOTE_PORT       $remote_port;
fastcgi_param SERVER_ADDR       $server_addr;
fastcgi_param SERVER_PORT       $server_port;
fastcgi_param SERVER_NAME       $server_name;
fastcgi_param REDIRECT_STATUS   200;

...

Now you'd create a MySQL database and user for PiWik and extract the PiWik files to the directory you've defined. I used /var/www/html/stats but you could use anything.

Once the files are uploaded, you need to set their ownership to www-data. To do that, we can run:

chown -R www-data:www-data /var/www/html/stats

That'll only change permissions on stats and all files and directories within it, it'll leave /var/www/html alone (which is what we want).

Now, once you've setup an SSL certificate for the sub-domain and created the proper A and CNAME entries to make sure it points to your installation, all you need to do is restart NGINX and load the sub-domain to get the installer started.

If you run in to any errors, check the error log:

tail -20 /var/log/nginx/error.log

You can post any errors as a reply and I'll do my best to help you troubleshoot them.

...

That said, I would recommend changing your second server block and swapping:

server_name _;

with

server_name mysite.com;

so that it matches your first block. That'll work for NGINX when there's only one server block, but it can potentially cause issues when you start adding multiple domains, sub-domains, etc.

Always make sure your Port 80 server blocks match your Port 443 server blocks. It'll save you a ton of troubleshooting down the road.

  • Thank you so much! Just to make sure I'm clear on this, the 2 server blocks you provided are in addition to the ones I have, right?

  • @jtittle if i may ask your assistance again, I have everything set up but can't get PHP to display the piwik index.php script. I'm somewhat familiar with PHP configuration but have only done it on Apache. These are the errors from my logs:

    2017/06/09 08:16:26 [error] 3395#3395: *5791 FastCGI sent in stderr: "PHP message: PHP Warning:  Unknown: failed to open stream: Success in Unknown on line 0
    Unable to open primary script: /var/www/html/stats (Success)" while reading response header from upstream, client: 74.90.251.106, server: stats.communitycred.com, request: "GET /index.php HTTP/1.1", upstream: "fastcgi://unix:/run/php/php7.0-fpm.sock:", host: "stats.communitycred.com"
    
    • UPDATE: got it working! I changed the php settings in the config to what was listed in the article you linked

@robynover

If you want something that's fairly simple to use, but offers advanced functionality, I'd go with PiWik. As far as how you'd install it, on NGINX, you'd start with the basics. I'd recommend taking a look at the following guide to get a feel for how to setup a LEMP stack (NGINX, PHP, MySQL):

https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-in-ubuntu-16-04

From there, additional setup really depends on whether you want to install PiWik to a sub-domain or sub-directory, or even a different domain.

The LEMP software stack is a group of software that can be used to serve dynamic web pages and web applications. This is an acronym that describes a Linux operating system, with an Nginx web server. The backend data is stored in the MySQL database and the dynamic processing...
  • Thank you. One of the things that's tripping me up is how to integrate PHP into an Nginx server that uses Node with a reverse proxy. I'm assuming the server config settings are different than what is outlined in the tutorial, and I'm concerned about getting my existing settings out of whack. Maybe that just means finding instructions on setting up a subdomain? (probably would install piwik to a subdomain)

    • @robynover

      How you'd go about it really depends on how you want to set things up.

      Are you currently running a NodeJS app with NGINX and want to integrate PHP, or are you currently running a PHP app and want to integrate a NodeJS app?

      Are you wanting to separate them by domain, or would both be on the same domain?

      Those details will ultimately define how you'd go about setting up your server block(s), so if you can share that, we can help you to get things straightened out :-).

      If you already have a working server block and can share it, then we could simply modify it to do what you want, or tell you what you'd need to do to split things up.

      • I'm currently running a Node app with NGINX. I'll only need PHP for Piwik. I thought I would run piwik on a subdomain, something like stats.mysite.com. I already have the Nginx config set up for a reverse proxy to Node, and for SSL.

        Here is my current config, with some of the longer comment blocks removed:

        server{
                listen 80 default_server;
                listen [::]:80 default_server;
                server_name mysite.com;
                return 301 https://$server_name$request_uri;
        }
        
        server {
                # SSL configuration
                #
                listen 443 ssl default_server;
                listen [::]:443 ssl default_server;
                include snippets/ssl-mysite.com.conf;
                include snippets/ssl-params.conf;
        
                root /var/www/html;
        
                # Add index.php to the list if you are using PHP
                index index.html index.htm index.nginx-debian.html;
        
                server_name _;
        
                location / {
                        proxy_pass http://localhost:3333;
                        proxy_http_version 1.1;
                        proxy_set_header Upgrade $http_upgrade;
                        proxy_set_header Connection 'upgrade';
                        proxy_set_header Host $host;
                        proxy_cache_bypass $http_upgrade;
                }
        
                location ~ /.well-known {
                        allow all;
                }
        
        }
        
Have another answer? Share your knowledge.