GeoIP2

Article Topics: Setup

GeoIP2 Country Database

Download and install the GeoIP2 Country database from MaxMind.

You can either download the freearrow-up-right version of the database or purchasearrow-up-right the commercial version.

The main file you need is the 'Country' .mmdb file

You will then need to either installarrow-up-right and configure the geoip2 apache module or use the PHPlibrary in NATS.

NATS Setup for GeoIP2 Apache module

Install the geoip2 apache module according to the setup directions herearrow-up-right

You will need to specify where the country db file is located and set the environment variable name as 'COUNTRY_CODE' and 'GEOIP_COUNTRY_CODE' in your apache config, for example:

MaxMindDBEnable On
MaxMindDBFile COUNTRY_DB /usr/local/share/GeoIP/GeoLite2-Country.mmdb
MaxMindDBEnv COUNTRY_CODE COUNTRY_DB/country/iso_code
MaxMindDBEnv GEOIP_COUNTRY_CODE COUNTRY_DB/country/iso_code

Then go to Config Admin -> Surfer section -> IP_FILTER and set to 'Geo-IP2'

NATS Setup for GeoIP2 PHP library

circle-exclamation

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 setting in the same section to 'Geo-IP2 PHP' and save your changes.

Test Code for GeoIP2 PHP library

circle-exclamation

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.

Obtaining State from GeoIP2

To obtain the state from GeoIP2, enter the server path to GeoLite2-City.mmdb in the 'Geoip2 City DB Path' configuration setting found in the Surfer Section of the Configuration Admin underneath Tracking.

Test Code to output state:

Optional Extension for GeoIP2 PHP library

As stated in their docsarrow-up-right: "[This extension] dramatically increase the performance of lookups in GeoIP2 or GeoLite2 databases"

Downloadarrow-up-right this as either standalone or via composer.

Then download thisarrow-up-right 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).

Once the two packages are installed, you will want to run the commands in the "Installing Extension" section

NATS Default GeoIP country code

circle-exclamation

You can set a default/fallback country for NATS to use when GeoIP fails to detect a surfer's country via config 'IP_FILTER_DEFAULT_COUNTRY' located in Configuration Admin, 'Surfer' dropdown.

Config supports ISO-3166 two-character country codes: ISO-3166arrow-up-right

Siteschevron-right

Last updated

Was this helpful?