Get Started With IP2location Apache Module

Dependencies

This module requires IP2Location BIN database to function. You may download the BIN database at


Requirements

  1. IP2Location C API library.

  2. Apache 2.0x.

  3. GNU make or any compatible make utility.


Installation

You can install the IP2Location Apache Module in either Linux or Windows.

Linux

  1. Create a new folder called ip2location.

  2. Install the development libraries in Debian.

    apt install apache2-dev
    
  3. Download IP2Location C library from here into the ip2location folder.

  4. Decompress C library.

    unzip master.zip && rm master.zip
  5. Get into working directory.

    cd IP2Location-C-Library-master
  6. 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.

  7. Download IP2Location Apache Module into the ip2location folder.

  8. Decompress IP2Location Apache Module.

    unzip master.zip && rm master.zip
  9. Get into IP2Location Apache Module directory.

    cd ip2location-apache-master
  10. 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
  11. Run the below command and make sure you see IP2Location_module (shared) entry.

    apache2ctl -M
  12. 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

  1. Create a new folder called ip2location.

  2. Download IP2Location C library from here into the ip2location folder.

  3. Decompress C library using 7-zip or other compression tools.

  4. Get into working directory.

    cd IP2Location-C-Library-master
  5. Start compilation.

    nmake /f Makefile.win
  6. Download and decompress Apache module into the ip2location folder.

  7. Navigate to ip2location-apache-master folder.

  8. 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"
  9. Start compilation.

    nmake /f Makefile.win
  10. Copy IP2Location_apache.dll generated to the Apache modules directory.

  11. 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'"

IP2Location Sample Database (BIN)

The sample BIN files have a limited scope and are only valid for the following IP ranges:

  1. IPv4 : 0.0.0.0 - 7.255.255.255
  2. IPv6 : 2a04:0000:0000:0000:0000:0000:0000:0000 - 2a04:ffff:ffff:ffff:ffff:ffff:ffff:fff

Articles & Tutorials