Get Started With IP2location Apache Module
Dependencies
This module requires IP2Location BIN database to function. You may download the BIN database at
IP2Location LITE BIN Data (Free): https://lite.ip2location.com
IP2Location Commercial BIN Data (Comprehensive): https://www.ip2location.com
Requirements
Apache 2.0x.
GNU make or any compatible make utility.
Installation
You can install the IP2Location Apache Module in either Linux or Windows.
Linux
Create a new folder called ip2location.
-
Install the development libraries in Debian.
apt install apache2-dev
Download IP2Location C library from here into the ip2location folder.
-
Decompress C library.
unzip master.zip && rm master.zip
-
Get into working directory.
cd IP2Location-C-Library-master
-
Compile and install IP2Location-C-Library.
autoreconf -i -v --force ./configure make make install
Note
You may need to run ldconfig to refresh the shared libraries, if needed.
-
Download IP2Location Apache Module into the ip2location folder.
-
Decompress IP2Location Apache Module.
unzip master.zip && rm master.zip
-
Get into IP2Location Apache Module directory.
cd ip2location-apache-master
-
Compile the IP2Location Apache Module.
apxs2 -i -a -L ../IP2Location-C-Library-master/libIP2Location/ -I ../IP2Location-C-Library-master/libIP2Location/ -l IP2Location -c mod_ip2location.c
-
Run the below command and make sure you see IP2Location_module (shared) entry.
apache2ctl -M
-
Add following lines into /etc/apache2/apache2.conf.
<IfModule mod_ip2location.c> IP2LocationEnable On IP2LocationDetectProxy <On|Off> IP2LocationSetmode ALL IP2LocationDBFile <ip2location_binary_db_file_with_fully_qualified_path> </IfModule>
Windows
Create a new folder called ip2location.
Download IP2Location C library from here into the ip2location folder.
Decompress C library using 7-zip or other compression tools.
-
Get into working directory.
cd IP2Location-C-Library-master
-
Start compilation.
nmake /f Makefile.win
-
Download and decompress Apache module into the ip2location folder.
-
Navigate to ip2location-apache-master folder.
-
Edit Makefile.win to change the path for C library.
IP2LOCATION_CSRC_PATH = C:/ip2location/IP2Location-C-Library-master/libIP2Location IP2LOCATION_CLIB_PATH = C:/ip2location/IP2Location-C-Library-master/libIP2Location APACHE_INSTALL_PATH = "C:/Program Files/Apache Software Foundation/Apachex.x"
-
Start compilation.
nmake /f Makefile.win
-
Copy IP2Location_apache.dll generated to the Apache modules directory.
-
Add following lines into httpd.conf.
<IfModule mod_ip2location.c> IP2LocationEnable <ON|OFF> IP2LocationDetectProxy <ON|OFF> IP2LocationSetmode ALL IP2LocationDBFile <ip2location_binary_db_file_with_fully_qualified_path> </IfModule>
Sample Codes
Apache .htaccess
RewriteEngine On
RewriteCond %{ENV:IP2LOCATION_COUNTRY_SHORT} ^GB$
RewriteRule ^(.*)$ http://www.google.co.uk [L]
Apache Mod Security
SecRule ENV:IP2LOCATION_COUNTRY_SHORT "NG|VN" "deny,status:500,id:5000888,msg:'Block visitor from Nigeria and Vietnam'"