Difference between revisions of "GeoIP2"

From TMM Wiki
Jump to navigationJump to search
 
(38 intermediate revisions by 6 users not shown)
Line 1: Line 1:
[https://dev.maxmind.com/geoip/geoip2/downloadable/ Download] and install the GeoIP database from Maxmind.
+
{{NATS4 Manual
 +
| show_configuration_admin_section = true
 +
}}
  
There is a [https://dev.maxmind.com/geoip/geoip2/geolite2/ free] one available here
+
== GeoIP2 Country Database ==
  
You will need to download the 'Country' .mmdb file for both the paid and free one
+
Download and install the GeoIP2 Country database from Maxmind.  
  
This file needs to be on your NATS installation in a path that is accessible to NATS
+
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 path will need to be specified in config->surfer->tracking GEOIP2_PHP_DATABASE_LOCATION
 
  
You will then switch the IP_FILTER to GeoIP2_PHP and store your changes
+
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.
  
== Test Code ==
+
==NATS Setup for GeoIP2 Apache module==
The following PHP code will help you test GeoIP.  Put it in a file, put the file on your webserver, and load its URL. It should print your country's name.
 
  
 +
Install the geoip2 apache module according to the setup directions [https://github.com/maxmind/mod_maxminddb here]
 +
 +
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:
 
<pre>
 
<pre>
<?
+
MaxMindDBEnable On
echo 'According to geoip apache module, you are from '.apache_note("GEOIP_COUNTRY_NAME");
+
MaxMindDBFile COUNTRY_DB /usr/local/share/GeoIP/GeoLite2-Country.mmdb
echo '<br>According to geoip pecl php module, you are from '.geoip_country_code_by_name($_SERVER['REMOTE_ADDR']);
+
MaxMindDBEnv COUNTRY_CODE COUNTRY_DB/country/iso_code
?>
+
MaxMindDBEnv GEOIP_COUNTRY_CODE COUNTRY_DB/country/iso_code
 +
</pre>
 +
 
 +
 
 +
Then go to Config Admin -> Surfer section -> IP_FILTER and set to 'Geo-IP2'
 +
 
 +
==NATS Setup for GeoIP2 PHP library(Must have NATS version 5.0.1.6 or above)==
 +
 
 +
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
 +
</div>
 +
 
 +
== 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.
 +
 
 +
<pre>
 +
<?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";
 
</pre>
 
</pre>
 
'''Note: Please be sure to perform this test after you install or update Maxmind's GeoIP.
 
'''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 [https://github.com/maxmind/GeoIP2-php docs]: "[This extension] dramatically increase the performance of lookups in GeoIP2 or GeoLite2 databases"
 +
 +
[https://github.com/maxmind/MaxMind-DB-Reader-php Download] this as either standalone or via composer.
 +
 +
Then download [https://github.com/maxmind/libmaxminddb 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).
 +
 +
Once the two packages are installed, you will want to run the commands in the "Installing Extension" section
 +
 +
== NATS Default GeoIP country code ==
 +
''' This feature is currently in development'''
 +
 +
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: [https://tmmwiki.com/index.php/ISO-3166 ISO-3166]
  
 
== See Also ==
 
== See Also ==
 
* [[Geo-Targeting Cascades]]
 
* [[Geo-Targeting Cascades]]
 +
* [[Geoip2 Load Balancer]]
  
 
[[Category:Not Applicable to NATS4]]
 
[[Category:Not Applicable to NATS4]]

Latest revision as of 13:59, 21 November 2024

NATS 4
Members Admin
The Members Admin
View Member Details
Add Member
MySQL Auth
Mod Authn DB
Multisite Access
Member Logging
Member Password Retrieval
OpenID Connect
Mod Auth OpenIDC
ID Numbers
NATS Configuration Admin
The Configuration Admin
Log Admin Activity
IP Address Filtering
MEMBER_EXPIRE_PAD
SKIP COUNTRIES
Fraud Configuration
Email Configuration
Affiliate Signup Email
Affiliate Postback
Affiliate Postback NEW
Affiliate Analytics
Throttling
NATS4 Terms of Service feature
GeoIP2

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

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(Must have NATS version 5.0.1.6 or above)

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

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).

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

NATS Default GeoIP country code

This feature is currently in development

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-3166

See Also