Maxmind GeoIP mit PHP unter FreeBSD
Vorbereitung
Um die GeoIP2 Datenbank von Maxmind nutzen zu können benötigt man neu einen (kostenlosen) MaxMind account
- Register for an account: https://www.maxmind.com/en/geolite2/signup
- Get a license key: https://www.maxmind.com/en/accounts/155743/license-key
Installation Maxmind GeoIP Package
Als erstes installieren wir die PHP Maxmind Datenbank inkl. Library (dabei werden die beiden Pakete libmaxminddb
und php73-maxminddb
installiert)
Ich installiere das Package nur für php73 (da ich mehrere php Versionen parallel betreibe)
pkg install php73-maxminddb
jetzt noch das GeoIpUpdate:
pkg install geoipupdate
Nun hat es mir aber de extension ins php5 (was noch default ist auf dem Server) Verzeichnis kopiert, statt ins extensions Verzeichnis von php73. Daher einfach kurz moven:
mv /usr/local/etc/php/ext-20-maxminddb.ini /usr/local/php73/etc/php
Konfiguration
vi /usr/local/etc/GeoIP.conf
Hier nun die AccountID
und LicenseKey
eintragen.
Ausserdem möchte ich die Datenbank Files global für alle meine Server zugänglich machen. Daher speichere ich sie auf der global shared Partition
# Replace YOUR_ACCOUNT_ID_HERE and YOUR_LICENSE_KEY_HERE with an active account
# ID and license key combination associated with your MaxMind account. These
# are available from https://www.maxmind.com/en/my_license_key.
AccountID xxx
LicenseKey xxx
# Enter the edition IDs of the databases you would like to update.
# Multiple edition IDs are separated by spaces.
EditionIDs GeoLite2-ASN GeoLite2-City GeoLite2-Country
# The remaining settings are OPTIONAL.
# The directory to store the database files. Defaults to /usr/local/share/GeoIP
DatabaseDirectory /global/db/geoip
Datenbanken installieren / updaten
Die Files aktualisiert man nun ganz einfach mit dem Befehl:
/usr/local/bin/geoipupdate -v
Cronjob
#download geoip data - alle 2 wochen um 23h 0 23 * * */2 /usr/local/bin/geoipupdate -v > /dev/null 2>&1
php.ini (alt für php5)
vi /usr/local/etc/php.ini
[geoip] geoip.custom_directory = /global/db/geoip/
Testen
nun kurz php auf segfault testen:
php -v
sowie
/usr/local/php73/bin/php -v
Wenn alles i.o. ist, php neu starten:
/usr/local/etc/rc.d/php73-fpm restart
Verwendung in Matomo
misc
Verzeichnis von Matomo kopieren. Ich habe einfach darauf verlinkt:cd /<pathtomatomo>/misc
ln -s ln -s /global/db/geoip/*.mmdb .
System -> Standorterkennung -> DBIP / GeoIP 2 (Php)