Category: Apache

Compiling Mod_Geoip2 for use with EasyApache 4 (EA4) and cPanel via command line (CLI)

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
  3. 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
    
  4. 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
  5. 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-devel ea-apache24-devel.x86_64 ea-apache24-tools.x86_64 wget nano
  6. 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
  7. 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 
  8. 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
  9. And finally we can go ahead and compile the module.
    # apxs -i -a -L/usr/lib64 -I/usr/include -lGeoIP -c mod_geoip.c
  10. 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
  11. 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
    
    <IfModule mod_geoip.c>
    GeoIPEnable On
    GeoIPDBFile /usr/share/GeoIP/GeoIP.dat Standard
    GeoIPDBFile /usr/share/GeoIP/GeoLiteCity.dat Standard
    </IfModule>

    Then, exit and save the file.

  12. Now you’ll want to restart apache.
    # service httpd restart && apachectl restart
  13. Then you can go ahead and create an info file and check via the web if geoip is supported.
    # nano /var/www/info.php
  14. Put these contents in it
    <?php phpinfo(); ?>
  15. 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.geoip-easyapache4

  16. 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!