Thursday, May 16, 2019

How to install GeoLite2 and mod_maxminddb for Apache 2 on Centos 6, 7 with SeLinux enabled

As GeoLite Legacy databases were discontinued on January 2, 2019, your new option is to use  MaxMind's GeoLite2 Free Downloadable Databases. Currently there is no available rpm package to install mod_maxminddb easily for Apache 2.2 on Centos 6 with SeLinux enabled. So, here are the necessary steps to achive it.

Install pre-requisites:
# yum groupinstall 'Development Tools'
# yum install tar gzip wget httpd-devel
Download and decompress current free GeoLite2 IP country database from maxmind.com website. (You may need to modify the last command line according to the current file name!)

UPDATE on 2020 spring:
Due to new data privacy regulations, there were significant changes to how to access free GeoLite2 databases starting December 30, 2019. Since then, it requires a free registration to download the GeoLite2 databases. See the instructions at https://dev.maxmind.com/geoip/geoip2/geolite2/ for obtainig the userID and LicenceKey.
After registration you will get personalized permanent links to download the GeoLite2 databases. It requires quite long time that new licence key becomes effective, so you may need to download the GeoLite2-country database from the website in browser instead of wget command.
$ mkdir -p /usr/share/GeoIP
$ cd /tmp
$ wget https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-Country&license_key=YOUR_LICENSE_KEY&suffix=tar.gz
$ tar xvzf GeoLite2-Country.tar.gz
# cp GeoLite2-Country_20200314/GeoLite2-Country.mmdb /usr/share/GeoIP/
Obtaining the latest up-to-date IP database, you may also install a package from Epel repository:
$ yum -y install geoipupdate
$ geoipupdate -v
geoipupdate requires you to put your previously mentioned LicenceKey and userId into /etc/GeoIp.conf
geoipupdate downloads, decompress and install the latest IP databases for you from MaxMind website.
Because IP data are always changing it is a good idea to update your local IP data periodically by running geoipupdate in a cron job on monthly bases. (Currently the GeoLite2 Country and City databases are updated on the first Tuesday of each month.) Put the followings into your crontab file to run on IP updating on each wednesday at noon:
0 12 * * 3 /usr/bin/geoipupdate
To have more info about geoipupdate config options run man geoipupdate.

Next step is to install libmaxminddb. On their github page there is a detailed description about installation. The current release is the version 1.4.2, but I have tested the older version 1.3.2 also.
$ wget  https://github.com/maxmind/libmaxminddb/releases/download/1.4.2/libmaxminddb-1.4.2.tar.gz
$ tar xvzf libmaxminddb-1.4.2.tar.gz
$ cd libmaxminddb-1.4.2
$ ./configure
$ make
$ make check
# make install
# ldconfig
The newly created libmaxminddb.so file is installed into /usr/local/lib folder. By default this folder is not in ld search path in Centos 6, so check it if this folder is in it:
$ ldconfig -v | grep -e "libmaxminddb.so.0"
If the output does not contain  the desired line, create a new ld config file having this folder in it:
# echo "/usr/local/lib" >> /etc/ld.so.conf.d/local.conf
# ldconfig
If you have Epel repo installed there is an easier way because the following will also install libmaxminddb, but an older version:
# yum -y install libmaxminddb-devel
Next step is to install mod_maxminddb. On their github page there is a detailed description about installation. The current version is 1.2.0 but I have successfully tested version 1.1.0 also.
$ wget https://github.com/maxmind/mod_maxminddb/releases/download/1.2.0/mod_maxminddb-1.2.0.tar.gz
$ tar xvzf mod_maxminddb-1.2.0.tar.gz
$ cd mod_maxminddb-1.2.0
$ ./configure
$ make
# make install
# chcon --reference /usr/lib64/httpd/modules/mod_dir.so /usr/lib64/httpd/modules/mod_maxminddb.so
Check if installer puts the following line into your httpd.conf:
LoadModule maxminddb_module /usr/lib64/httpd/modules/mod_maxminddb.so
Because this is the default directory for modules you may abbreviate it to
LoadModule maxminddb_module modules/mod_maxminddb.so
To have the well-known GEOIP_COUNTRY_CODE environment variable containing the 2 letter ISO country codes put the followings into the httpd.conf:
MaxMindDBEnable On
MaxMindDBFile DB /usr/share/GeoIP/GeoLite2-Country.mmdb
MaxMindDBEnv GEOIP_COUNTRY_CODE DB/country/iso_code
Check the apache httpd config syntax by:
$ httpd -t
If everything is well done, reload the current apache httpd configuration:
# service httpd reload