Install GeoIP for Apache and PHP

From Brian Nelson Ramblings
Jump to: navigation, search

Install GeoIP for Apache and PHP/PHP-FPM on Centos

GeoIP is a C library that enables the user to find the country that any IP address or hostname originates from. It uses a file based database that is accurate as of February 2009. This database simply contains IP blocks as keys, and countries as values. This database should be more complete and accurate than using reverse DNS lookups.

Install GeoIP Data

The epel yum repo has the GeoIP Data

yum install GeoIP{,-devel}

This will install the data files in /usr/share/GeoIP

Install GeoIP for Apache

The epel yum repo also has mod_geoip for apache

yum install mod_geoip

Now to verify that it was installed run:

httpd -M | grep geoip

Install GeoIP for PHP/PHP-FPM

This is available via PECL

pecl install geoip && echo 'extension = geoip.so' > /etc/php.d/geoip.ini

Then you will want to restart apache and/or php-fpm.

Setting PHP to use the Data Files

You will want to add the following line to /etc/php.d/geoip.ini

geoip.custom_directory=/usr/share/GeoIP/

You can do this with the following command

echo 'geoip.custom_directory=/usr/share/GeoIP/' >> /etc/php.d/geoip.ini

Now you have to restart apache and/or php-fpm.