We’ve recently switched to using Easy Apache 4 (EA4) on our cPanel installs as it’s ‘basically not beta’ now. And we ran into a problem where a site needed to have GeoIP installed but it wasn’t available to us through the WHM control panel.

After some extensive searching and trial and error, we were able to come up with instructions that worked for our particular install which I’ll document below incase someone else needs these.

Note, these assume you already have EasyApache 4 and php installed and configured — a working environment.

Instructions for getting mod_geoip2 working on easyapache 4 (ea4)

  1. Step one login to your server. I personally just ‘su root’ instead of ‘sudo’ all the time, but I’m lady, and confident I won’t rm -rf /lib or something equally as asinine.
  2. Now you’re going to want to issue this command;
# yum install GeoIP GeoIP-devel GeoIP-data zlib-devel
  1. Then, we’re going to want to issue the following to create the GeoIP directory, and enter it
# mkdir /usr/share/GeoIP && cd /usr/share/GeoIP
  1. Now we’ll want to grab the data files from MaxMind. These commands with download the city and country info.
# wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
# wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
# gunzip GeoIP.dat.gz
# gunzip GeoLiteCity.dat.gz
  1. Now we’re going to want to install some requirements for everything to build. If you already have something installed from this list it will just ignore it and install the rest.
# yum install httpd-devel apr-develea-apache24-devel.x86_64 ea-apache24-tools.x86_64 wget nano
  1. You should still be in the same GeoIP directory but if you’re not just enter these commands to enter the directory you need to be in.
# cd /usr/share/GeoIP
  1. Now you’re going to want to grab the GeoIP source for apache module. Then, uncompress it, and, enter the directory.
# wget ftp://ftp.netbsd.org/pub/pkgsrc/distfiles/mod_geoip2_1.2.5.tar.gz
# tar xvzf mod_geoip2_1.2.5.tar.gz && cd mod_geoip2_1.2.5 
  1. Now we have to make a change due to apache 2.4 and some name changes, so, run this command now.
# sed s/remote_ip/client_ip/g -i mod_geoip.c**
  1. And finally we can go ahead and compile the module.
# apxs -i -a -L/usr/lib64 -I/usr/include -lGeoIP -c mod_geoip.c
  1. Now we just have to modify the httpd.conf file for Apache to load the module # nano /etc/apache2/conf.modules.d/mod_geoip.conf**

  2. Change the content of this file so instead of looking like this;

LoadModule geoip_module    /usr/lib64/apache2/modules/mod_geoip.so

instead, make sure it looks like this;

LoadModule geoip_module /usr/lib64/apache2/modules/mod_geoip.so

<strong><IfModule mod_geoip.c>
<strong>GeoIPEnable On
<strong>GeoIPDBFile /usr/share/GeoIP/GeoIP.dat Standard
<strong>GeoIPDBFile /usr/share/GeoIP/GeoLiteCity.dat Standard
<strong></IfModule>

Then, exit and save the file.

  1. Now you’ll want to restart apache.

    # service httpd restart && apachectl restart
    
  2. Then you can go ahead and create an info file and check via the web if geoip is supported.

    # nano /var/www/info.php
    
  3. Put these contents in it

    <?php phpinfo(); ?>
    
  4. Close and exit the file, then visit your site at

http://YOUR_DOMAIN.COM/info.php

and you should see something similar to the following.

Congrats, you’re done!

Let me know if there’s any changes that need to be made to this or if this helped you in your quest to get mod_geoip2 working with cpanel/whm and easyapache 4 in the comments below!

Comments

Comment by Javier on 2016-08-25 06:29:37 -0500

Hello, nice tutorial !!

I get an error when I run the command:

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

cannot open /usr/local/apache/build/config_vars.mk: No such file or directory at /usr/bin/apxs line 217.

I have edited the apxs file to change the line:

my $installbuilddir = “/etc/apache2”;

to

my $installbuilddir = “/home/cpeasyapache/src/httpd-2.4/build”

But I get another error:

sh: /usr/local/apache/bin/envvars: No such file or directory
apxs:Error: Sorry, no shared object support for Apache.
apxs:Error: available under your platform. Make sure.
apxs:Error: the Apache module mod_so is compiled into.
apxs:Error: your server binary `/usr/local/apache/bin/httpd’.

I think that the apxs get the old values of Apache path. Any hint to solve that?

Thanks

Comment by Matt G on 2016-08-27 11:13:20 -0500

Hm, I’m not entirely sure here – You might have to go into WHM and recompile apache, but, with DSO support?

*EDIT* – I found this on serverfault, may be worth a look?

http://serverfault.com/questions/646873/how-to-configure-lib-path-for-apache-utilities

Comment by Brad on 2016-08-30 19:49:17 -0500

Worked really well…. had a conflict with

file /usr/share/aclocal/find_apr.m4 from install of ea-apr-devel-1.5.2-3.3.2.cpanel.x86_64 conflicts with file from package apr-devel-1.3.9-5.el6_2.x86_64

Sorted it with

# yum remove apr-devel

Then ran

# yum install ea-apache24-devel.x86_64 ea-apach24-tools.x86_64 wget nano

Great tutorial.

many Thanks

Comment by NOV Piseth on 2016-11-10 21:53:15 -0500

I was used cloudlinux and I use compiled mod_geoip as below command.

apxs -i -a -L/opt/geoip/lib/ -I/opt/geoip/include/ -lGeoIP -c mod_geoip.c

enjoy

Comment by Manuel A. Garcia on 2017-05-05 15:48:14 -0500

Thanks, Matt.

For those who it works to, there is an easier way without need of compiling.

Just download the module from here:
wget http://www.sohailriaz.com/downloads/custom_opt_mod-mod_geoip.tar.gz

Then, untar it into this directory:
/var/cpanel/easy/apache/custom_opt_mods/

And now, EasyApache 4 willl be displaying the new custom module for you from WHM. Just enable it.

I found it here:
https://forums.cpanel.net/threads/how-to-install-a-custom-module.423201/

And in here it is the same process, but running the EasyApache script from CLI at the end instead of doing it from WHM:
http://www.sohailriaz.com/mod_geoip-custom-module-for-cpanel-whm/

Maybe you could add this easy solution at the beginning of your post.
Greetings!

Comment by Manuel A. Garcia on 2017-05-05 16:23:02 -0500

Oops! Sorry for my prior message.

I shouldn’t have published it without having tested it before. I don’t want to confuse anyone.

It’s not as easy as it seemed. It’s not running for me – EA4 is not showing the module.

Finally, I had to compile it anyway 🙁

Comment by Mike on 2019-01-09 22:42:55 -0500

Worked like a charm on my KnownHost VPS, thanks for the awesome writeup!

Comment by Somesh Zade on 2020-04-04 10:41:20 -0500

It worked perfectly fine. Please consider changing the GEOIP Source download URL.

btw once again thank you for the article mate.

(Sorry for above comment, I mistakenly published one website URL :P)