Using GeoIP with PHP - Part 2

Continue from part 1. It seems the default php5-geoip package is unstable and core dumped on me. Let’s try the PHP Extension Community Library (PECL) installation method.

Let’s remove the existing php5-geoip package.
$ sudo apt-get --purge remove php5-geoip

Install the necessary packages for PECL installation and compilation.
$ sudo apt-get install php-pear php5-dev libgeoip-dev geoip-database

Download, compile, install, and enable the geoip extension. The tee command is to solve the sudo file permission error.
$ sudo pecl install geoip
$ echo "" | sudo tee -a /etc/php5/conf.d/geoip.ini
$ sudo service apache2 restart

Double check the geoip extension is really loaded properly
$ php -m | grep geoip
$ php -r "echo extension_loaded('geoip');"

Test the method that gave us core dump in Part 1. No core dump and the geoip extension is stable using PECL installation method. Why?
$ php -r "print_r(geoip_db_get_all_info());"
    [1] => Array
            [available] => 1
            [description] => GeoIP Country Edition
            [filename] => /usr/share/GeoIP/GeoIP.dat

Weird, what the difference between default Ubuntu package and PECL? Let’s check the version.
$ apt-cache show php5-geoip | grep Version
Version: 1.0.7-8

$ pecl info geoip | grep "Release Version"
Release Version       1.0.8 (stable)

So it seems both are different version. Let’s read the latest changelog which shows us one very interesting fix.
* Fix segfault with newer geoip libraries and geoip_db_get_all_info() (bug #60066)

What is bug #60066? The reason why PHP segfault is GeoIPDBFileName is not defined. Still remember the backtrace and apport crash report title line “Title: php5 crashed with SIGSEGV in add_assoc_string_ex()” we did in Part 1. Read the fix in the Subversion.

Download the source code for php5-geoip from the Ubuntu repository.
$ apt-get source php5-geoip
$ tree -L 1 .
├── php-geoip-1.0.7
├── php-geoip_1.0.7-8.debian.tar.gz
├── php-geoip_1.0.7-8.dsc
└── php-geoip_1.0.7.orig.tar.gz

1 directory, 3 files

Prepare the extension for compilation but run the test cases instead.
$ cd php-geoip-1.0.7/geoip-1.0.7/
$ phpize5
$ php -f run-tests.php
ERROR: environment variable TEST_PHP_EXECUTABLE must be set to specify PHP executable!

$ whereis php
php: /usr/bin/php /usr/bin/X11/php /usr/share/php /usr/share/man/man1/php.1.gz

$ export TEST_PHP_EXECUTABLE=/usr/bin/php
$ php -f run-tests.php
Calling geoip_db_filename() with a non-existant database type within bound. [tests/008.phpt]
Calling geoip_database_info() with a non-existant database type within bound. [tests/011.phpt]
Checking timezone info with (some) empty fields [tests/014.phpt]

Since we install the geoip extension using PECL, is better to run the test cases that came with that same version as well. Download the source, configure, and run tests.
$ wget
$ tar zxvf geoip-1.0.8.tgz
$ cd geoip-1.0.8
$ phpize5
$ export TEST_PHP_EXECUTABLE=/usr/bin/php
$ php -f run-tests.php
Checking timezone info with (some) empty fields [tests/014.phpt]

No comments:

Post a Comment