Debian/Ubuntu下安装GeoIP

本文详细介绍了在Ubuntu系统中如何安装GeoIP,包括通过APT安装PHP的GeoIP支持模块,下载并处理GeoIP数据库,以及源码安装和启用GeoIP PHP模块的方法。测试表明,正确安装后,GeoIP可以定位到访客的国家/地区、城市等位置信息。

Ubuntu下安装GeoIP

  什么是GeoIP ?
  所谓GeoIP,就是通过来访者的IP, 定位他的经纬度,国家/地区,省市,甚至街道等位置信息的一个数据库。GeoIP有两个版本,一个免费版,一个收费版本。收费版本的准确率和数据更好一些。
  GeoIP如何使用?
  GeoIP支持多种语言调用,这里我们以PHP为例。
方法一

通过APT安装PHP对GeoIP的支持模块

apt-get install php5-geoip libgeoip1

下载GeoIP数据库

wget -N -q http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz

wget -N -q http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
解压数据库
gzip -d GeoLiteCity.dat.gz
复制GeoIP数据库到数据目录
cp GeoLiteCity.dat /usr/share/GeoIP/GeoIPCity.dat

  注:这里的路径和编译GeoIP C API所使用的–with-dbdir参数有关。
  测试GeoIP
  重启Apache

/etc/init.d/apache2 restart

  在Web目录下新建一个测试的PHP文件,这里以geoip.php为例:

vi /var/www/geoip.php

<?php

$country = geoip_record_by_name('61.128.128.68'); if ($country) { echo 'This host is located in: '; print_r($country); }

print geoip_db_filename(GEOIP_COUNTRY_EDITION);

?>

访问这个文件,如出现下面类似信息,则表示成功了。


This host is located in: Array ( [continent_code] => AS [country_code] => CN [country_code3] => CHN [country_name] => China [region] => 33 [city] => Chongqing

[postal_code] => [latitude] => 29.562799453735 [longitude] => 106.55280303955 [dma_code] => 0 [area_code] => 0 )

方法二

通过源码方式安装
安装GeoIP C API
wget http://geolite.maxmind.com/download/geoip/api/c/GeoIP.tar.gz
tar xvzf GeoIP.tar.gz
cd GeoIP-1.4.8
./configure
make
make install
通过pecl安装geoip模块。
pecl install geoip
启用GeoIP PHP模块
vi /etc/php5/conf.d/geoip.ini

extension=geoip.so
复制GeoIP数据库到数据目录
cp GeoLiteCity.dat /usr/local/share/GeoIP/GeoIPCity.dat

  注:这里的路径和编译GeoIP C API所使用的–with-dbdir参数有关。

  测试GeoIP

  重启Apache

/etc/init.d/apache2 restart

  在Web目录下新建一个测试的PHP文件,这里以geoip.php为例:

vi /var/www/geoip.php



<?php

$country = geoip_record_by_name('61.128.128.68'); if ($country) { echo 'This host is located in: '; print_r($country); }

print geoip_db_filename(GEOIP_COUNTRY_EDITION);

?>

  访问这个文件,如出现下面类似信息,则表示成功了。

This host is located in: Array ( [continent_code] => AS [country_code] => CN [country_code3] => CHN [country_name] => China [region] => 33 [city] => Chongqing

[postal_code] => [latitude] => 29.562799453735 [longitude] => 106.55280303955 [dma_code] => 0 [area_code] => 0 )

  一些编译中常见的错误

  如果你的系统中的libtool中的版本低于2.2.6b,可能会出现以下错误提示:

libtool: Version mismatch error. This is libtool 2.2.6 Debian-2.2.6a-4, but the

libtool: definition of this LT_INIT comes from libtool 2.2.6b.

libtool: You should recreate aclocal.m4 with macros from libtool 2.2.6 Debian-2.2.6a-4

  解决方法:先执行以下语句后,再重新编译。

autoreconf --force
./configure
make
make install
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值