Debian 10 – PHP / Matomo GeoIP2 Installation

The old GeoIP legacy release (aka. GeoIPCity.dat) has been deprecated and doesn’t receive updates for quit some time. It will get removed from Matomo with the next major release.
If you would like to have accurate location detection in Matomo / Piwik the new GeoIP2 module should be used.

Since Debian Buster doesn’t ship it with it’s php packages, there are some manual steps needed to achieve that.

First of all install the needed library, build-tools and the php-dev package:

apt-get install libmaxminddb-dev php-dev git build-essential

Next step is to clone the MaxMind-DB-Reader-php repository in order to compile the PHP module.

cd /usr/local/src
git clone https://github.com/maxmind/MaxMind-DB-Reader-php
cd MaxMind-DB-Reader-php/ext
phpize
./configure
make && make install

After that the new PHP module needs to be enabled and Apache or PHP (when using FPM) restarted:

echo "extension=maxminddb.so" > /etc/php/7.3/mods-available/maxminddb.ini
phpenmod maxminddb

# The folder /usr/share/GeoIP needs to be added
# to the PHP open_basedir setting in your php.ini

apache2ctl graceful
service php7.3-fpm restart

Now the database GeoIP2-City.mmdb needs to be installed locally. Since the free version is updated once a month, this is done via a download cronjob.

apt-get install python3-requests

mkdir -p /usr/share/GeoIP

cat >> /usr/local/sbin/geoip2_update.py << EOF
#!/usr/bin/env python3
import requests
import datetime
import gzip
import sys

db = '/usr/share/GeoIP/GeoIP2-City.mmdb'
url = 'https://download.db-ip.com/free/'
now = datetime.datetime.now()
date = now.strftime('%Y-%m')

r = requests.get(url + 'dbip-city-lite-{}.mmdb.gz'.format(date))
if r.status_code == requests.codes.ok:
  with open(db, 'wb') as f:
    f.write(gzip.decompress(r.content))
  sys.exit(0)
else:
  print('GeoIP2 download failed: ' + str(r.status_code))
  sys.exit(1)
EOF

chmod 700 /usr/local/sbin/geoip2_update.py

cat >> /etc/cron.d/geoip2-update << EOF
MAILTO=root
0 7 28 * *     root  /usr/local/sbin/geoip2_update.py
EOF

Now the GeoIP2 database gets updated once a month automatically and the script should be executed once to get the current version right now and to test the setup as well.

/usr/local/sbin/geoip2_update.py

In order to use GeoIP2 in Matomo two settings need to be done. First of all a database Symlink needs to be created in order to use the GeoIP2 database in Matomo.

# adjust path to your matomo misc folder
ln -s /usr/share/GeoIP/GeoIP2-City.mmdb /var/www/matomo/misc/

After that the Plugin GeoIp2 needs to be activated. It’s a core package and should be already listed on the Matomo Plugins page in the Admin UI.

Now it should be possible to activate the new module under Geolocation. If your location is listed on the right side, enable the GeoIP2 box and click Save.

That’s it. The results are much more accurate and quicker.

If you don’t use Matomo a PHP GeoIP2 testscript could be used instead.

<?php

use MaxMind\Db\Reader;
$reader = new Reader('/usr/share/GeoIP/GeoIP2-City.mmdb');
$record = $reader->get($_SERVER['REMOTE_ADDR']);

print($record['continent']['names']['en'] . "\n");
print($record['country']['names']['en'] . "\n");
print($record['subdivisions'][0]['names']['en'] . "\n");
print($record['city']['names']['en'] . "\n");

?>

Your continent, country, subdivision and city should be listed after executing the script via the webserver.

  1. Bisher keine Kommentare
  1. Bisher keine Trackbacks