Install & 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.
Change current working directory to the installation directory.
Download the GeoIP country and city databases.
Unzip these downloaded archives with gunzip command
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.
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.
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.
GeoIPDBFile /usr/local/share/GeoIP/GeoIP.dat Standard
GeoIPDBFile /usr/local/share/GeoIP/GeoLiteCity.dat Standard
Now restart Apache for the changes to take effect.
You can verify if mod_geoip is installed by creating a PHP info page.
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.
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.
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
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 🙂