Matomo Webpage Analytics
So I am running this website for a while now but never cared if anyone else than people I tell about my website visit it. Yesterday, when talking to a friend, the question arised how many views this blog has.
As is have direct access to my /var/log/nginx/access.log
, I can directly look into it.
But it is much better having this done by something.
That’s where Matomo comes into play.
Setting things up
I used the compose file and configuration from GitHub
matomo docker-compose.yml
version: "3"
services:
db:
image: mariadb:10
command: --max-allowed-packet=64MB
restart: always
volumes:
- ./data/db:/var/lib/mysql
environment:
- MYSQL_PASSWORD=matomo
- MYSQL_DATABASE=matomo
- MYSQL_USER=matomo
- MARIADB_ROOT_PASSWORD=matomo
app:
image: matomo:fpm-alpine
restart: always
links:
- db
volumes:
- ./data/matomo:/var/www/html
environment:
- MATOMO_DATABASE_HOST=db
- PHP_MEMORY_LIMIT=2048M
- MATOMO_DATABASE_ADAPTER=mysql
- MATOMO_DATABASE_TABLES_PREFIX=matomo_
- MATOMO_DATABASE_USERNAME=matomo
- MATOMO_DATABASE_PASSWORD=matomo
- MATOMO_DATABASE_DBNAME=matomo
web:
image: nginx:alpine
restart: always
volumes:
- ./data/matomo:/var/www/html:ro
# see https://github.com/matomo-org/matomo-nginx
- ./matomo.conf:/etc/nginx/conf.d/default.conf:ro
ports:
- 8080:80
and started it without a lot of configuration. I am not having this open to the public, because I am not going to use a Cookie or JS-Script to analyze visitors behavior but just the page access to my webserver. Therefore I don’t need to change anything on my website.
I only need to pass the nginx-logs to matomo on a regular basis which I will describe shortly.
Sending logs
To do this, matomo allows to send logs to the running matomo service using the Matomo Server Log Analytics.
As I host multiple webpages on this server I am exactly using the vhosts
nginx log format which has to be edited in /etc/nginx/nginx.conf
Finally one can import old logs using the following call to the script:
python3 data/matomo/misc/log-analytics/import_logs.py --log-format-name=common_complete --url 127.0.0.1:8080 --login matomo --password matomo --add-sites-new-hosts /var/log/nginx/access.log.1
But how will we do this regularly? The docs say that we need to add some log rotation. Fortunately, nginx has a default log-rotation installed
$ cat /etc/logrotate.d/nginx
/var/log/nginx/*.log {
daily
missingok
rotate 14
compress
delaycompress
notifempty
create 0640 www-data adm
sharedscripts
prerotate
if [ -d /etc/logrotate.d/httpd-prerotate ]; then \
run-parts /etc/logrotate.d/httpd-prerotate; \
fi \
endscript
postrotate
invoke-rc.d nginx rotate >/dev/null 2>&1
endscript
}
which is run daily, and deletes logs older than 14 days, and leaves yesterdays logs without compression (delaycompress).
So we have a full day to import yesterdays log at /var/log/nginx/access.log.1
using a cron like this:
10 0 * * * python3 data/matomo/misc/log-analytics/import_logs.py --log-format-name=common_complete --url 127.0.0.1:8080 --login matomo --password matomo --add-sites-new-hosts /var/log/nginx/access.log.1
Aaand we are done and can see that our blog has only a handful of visits. But at least I have my projects documented and can forward it to people in need (mostly future me).