New comment on reddit

If you've got normal* Apache logs, you can do this:

cd /var/log/httpd cut -d ' ' -f1 access_log | sort -u | wc -l 

This uses a space as a delimiter, grab the first field, which is the visiting IP address, pipe into sort -u to get unique values, pipe into wc -l to get a line count

It's not a perfect count – this aggregates IP addresses across all the days of your log file (my logrotate configs ensure I never have more than a week's worth of data in a given log file), and doesn't differentiate between "real" visits and crawlers, but it's A metric that's quick and likely not more than an order of magnitude too large

Here's a version that'll do the same – but on unique GET requests:

cut -d '"' -f2 access_log | grep GET | sort -u | wc -l 

This one uses the double-quote mark as a delimiter, and selects the second field (which should be the actual HTTP request (GET or POST))

Personally, I'd recommend running Splunk in free mode, ingest your web server logs, and see what you can do with them 🙂


* by "normal", I mean your log lines look something like this:

 62.210.215.113 - - [21/Jun/2020:03:20:17 +0000] "GET /2011/08/11/connexions/feed/ HTTP/1.1" 200 812 "-" "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.66 Safari/537.36" 

June 26, 2020 at 09:58AM
via reddit https://www.reddit.com/r/selfhosted/comments/hfrbx6/best_way_to_get_a_view_count_on_a_selfhosted_blog/fw29r2k?utm_source=ifttt