How to install mod_geoip on WHM/cPanel server

Mod_geoip Apache module helps to find the geolocation of IP addresses of visitors. This module allows you to get the visitor’s country, organization, and location.

This post describes the procedure to install Mod_geoip in WHM/cPanel server.

Login to your server via SSH

Then run the following commands, To download and install the GeoIP database provided by MaxMind

mkdir /usr/share/GeoIP && cd /usr/share/GeoIP 
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz gzip -d GeoIP.dat.gz

Now run the following command to download and install Mod_GeoIP for cPanel servers:

wget https://documentation.cpanel.net/download/attachments/2326651/custom_opt_mod-mod_geoip.tar.gz?version=29&modificationDate=1460059677554&api=v2
tar -C /var/cpanel/easy/apache/custom_opt_mods -xzf custom_opt_mod-mod_geoip.tar.gz

Now Recompile the Apache to activate mod_geoip, For this use cPanel’s easy apache script so the Geo_modip becomes an active apache module.

Go to:

WHM > Software > EasyApache > Start customizing based on profile > Activate / Check ‘Mod GeoIP’ > then ‘Save and Build’.

This will start the apache compilation process to add mod_geoip to CPanel/WHM server.

Now we have to enable mod_geoip for cPanel :

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

And copy and paste the following details into your text area:

LoadModule geoip_module /usr/local/apache/modules/mod_geoip.so
GeoIPEnable On
GeoIPDBFile /usr/share/GeoIP/GeoIP.dat MemoryCache

At this time mod_geoip should be installed and working as expected on cPanel/WHM server.
However, now you need to install and add PHP GeoIP extension. Run the following command:

pecl install geoip

Then add the following code to your php.ini file, usually located at /usr/local/lib/php.ini

extension=geoip.so

Now restart the apache to apply changes:

service httpd restart

Now you can test your GeoIP module  by running:

php -i |grep geo

Its output should be like:

php -i |grep geo
geoip
geoip support => enabled
geoip extension version = 1.0.7
geoip library version = 1004008
geoip.custom_directory = no value = no value