Installation & Configuration of GeoIP via CLI & cPanel/WHM

GeoIP stands for geographic information of IP Addresses. It is a PHP extension used to find the geographic location of an IP Address (eg:- Country, state, latitude) and other information like ISP, connection type etc.

You need to install the following dependencies for installing GeoIP extension.

yum install GeoIP GeoIP-devel GeoIP-data zlib-devel

Create a custom installation directory for installing GeoIP package.

mkdir /usr/local/share/GeoIP

Change current working directory to the installation directory.

cd /usr/local/share/GeoIP

Download the GeoIP country and city databases.

wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz

wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz

Unzip these downloaded archives with gunzip command

gunzip GeoIP.dat.gz

gunzip GeoLiteCity.dat.gz

Now with the GeoIP databases are in place, we can go ahead and install the PHP extension mod_geoip2 as an Apache module.

As a pre-requisite for installing mod_geoip2, you need to install the following dependencies:

yum install httpd-devel apr-devel

httpd-devel is used to build Dynamic Shared Objects (DSOs) for Apache and apr-devel provides library of C data structures and routines.

Download the mod_geoip2 package from the available repo.

wget ftp://ftp.netbsd.org/pub/pkgsrc/distfiles/mod_geoip2_1.2.5.tar.gz

This downloads the archive of the package and you need to untar or extract it.

tar xvzf mod_geoip2_1.2.5.tar.gz && cd mod_geoip2_1.2.5

Change working directory to mod_geoip directory.

cd mod_geoip2_1.2.5

Install the mod_geoip extension as an Apache module using apxs which is a tool used to build and install extension modules for Apache.

apxs -i -a -L/usr/lib64 -I/usr/include -lGeoIP -c mod_geoip.c

Once this done, add the following lines in the Apache configuration file /etc/httpd/conf/httpd.conf.

<IfModule mod_geoip.c>

GeoIPEnable On

GeoIPDBFile /usr/local/share/GeoIP/GeoIP.dat Standard

GeoIPDBFile /usr/local/share/GeoIP/GeoLiteCity.dat Standard

</IfModule>

Now restart Apache for the changes to take effect.

/etc/init.d/httpd restart

You can verify if mod_geoip is installed by creating a PHP info page.

mod_geoip

If you can find mod_geoip among the Loaded Modules list, be sure the said module is installed.

Installing mod_geoip with WHM

You can install custom modules via easyapache utility. For installing mod_geoip with easy apache, just download the package into the easyapache custom installation directory and run easyapache with mod_geoip enabled.

Download the custom mod_geoip module package.

wget http://www.sohailriaz.com/downloads/custom_opt_mod-mod_geoip.tar.gz

Extract the package archive into the easyapache custom modules directory.

tar -C /var/cpanel/easy/apache/custom_opt_mods -xzf custom_opt_mod-mod_geoip.tar.gz

Next step is to rebuild Apache with mod_geoip module.

Run EasyApache to rebuild Apache with mod_geoip module.

For this, go to : WHM >> Software >> EasyApache (Apache Update) >> Start customizing based on profile

Check ‘Mod GeoIP‘ option (on Short / Exhaustive Options List page) and click on Save and Build as shown in the screenshot below.

mod_geoip2

This will rebuild Apache. After this has been done, we need to specify the following lines in Apache configuration file to load the module every time Apache runs.

LoadModule geoip_module /usr/local/apache/modules/mod_geoip.so

GeoIPEnable On

GeoIPDBFile /usr/share/GeoIP/GeoIP.dat MemoryCache

It is not advised to edit the Apache configuration file directly. So you can use the WHM functionality to edit the Apache config file. For this go to:

WHM >> Service Configuration >> Apache Configuration >> Include Editor >> Pre Main Include >> Select the current Apache version

You can see a text box there and you can specify the lines mentioned above there.

This completes GeoIP and mod_geoip installation. Cheers 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *