Difference between revisions of "GeoIP2"
Line 9: | Line 9: | ||
You can either download the [https://dev.maxmind.com/geoip/geoip2/geolite2/ free] version of the database or [https://www.maxmind.com/en/geoip2-country-database purchase] the commercial version | You can either download the [https://dev.maxmind.com/geoip/geoip2/geolite2/ free] version of the database or [https://www.maxmind.com/en/geoip2-country-database purchase] the commercial version | ||
− | The main file you need is | + | The main file you need is the 'Country' .mmdb file |
You will then need to either [https://github.com/maxmind/mod_maxminddb install] and configure the geoip2 apache module or use the php library in NATS. | You will then need to either [https://github.com/maxmind/mod_maxminddb install] and configure the geoip2 apache module or use the php library in NATS. |
Revision as of 14:53, 13 February 2019
GeoIP2 Country Database
Download and install the GeoIP2 Country database from Maxmind.
You can either download the free version of the database or purchase the commercial version
The main file you need is the 'Country' .mmdb file
You will then need to either install and configure the geoip2 apache module or use the php library in NATS.
NATS Setup for GeoIP2 Apache module
Install the geoip2 apache module according to the setup directions here
Then go to Config Admin -> Surfer section -> IP_FILTER and set to 'Geo-IP2'
NATS Setup for GeoIP2 PHP library
The .mmdb file needs to be on your server in a path that is accessible to NATS
The path will need to be specified in Config Admin -> Surfer section -> GEOIP2_PHP_DATABASE_LOCATION
You will then switch the IP_FILTER to 'Geo-IP2 PHP' and store your changes
Test Code for GeoIP2 PHP library
The following PHP code will help you test GeoIP. Put it in a file, put the file on your webserver in the nats www directory, and load its URL. It should print your country's name.
<?php include_once('../vendor_includes/geoip2.phar'); $reader = new GeoIp2\Database\Reader('<PATH_TO_DB_FILE.mmdb>'); $record = $reader->country($_SERVER['REMOTE_ADDR']); $country = $record->country->isoCode; echo"$country is country";
Note: Please be sure to perform this test after you install or update Maxmind's GeoIP.
Optional Extension for GeoIP2 PHP library
As stated in their docs: "[This extension] dramatically increase the performance of lookups in GeoIP2 or GeoLite2 databases"
Download this as either standalone or via composer.
Then download this and follow the instructions on the site to configure this library. (Note you will need to have a compiler such as GCC installed on your server).
$ ./configure $ make $ make check $ sudo make install $ sudo ldconfig
Once the two packages are installed, you will want to run the commands in the "Installing Extension" section
cd ext phpize ./configure make make test sudo make install
Finally, add "extension=maxminddb.so" to your php.ini file and restart PHP