Ubuntu下安装GeoIP

系统 3222 0

Ubuntu下安装GeoIP | 简单.生活

Ubuntu下安装GeoIP

Category : 工作学习 / Tags : , , , / Date : 2011.09.10 / 485 views /

什么是GeoIP ?

所谓GeoIP,就是通过来访者的IP, 定位他的经纬度,国家/地区,省市,甚至街道等位置信息的一个数据库。GeoIP有两个版本,一个免费版,一个收费版本。收费版本的准确率和数据更好一些。

GeoIP如何使用?

GeoIP支持多种语言调用,这里我们以PHP为例。  

方法一

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

                  1

                
                  
                    apt-get
                  
                  
                    install
                  
                   php5-geoip libgeoip1
                

下载GeoIP数据库

                  1
2

                
                  
                    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
                

解压数据库

                  1

                
                  
                    gzip
                  
                  
                    -d
                  
                   GeoLiteCity.dat.gz
                

复制GeoIP数据库到数据目录

                  1

                
                  
                    cp
                  
                   GeoLiteCity.dat 
                  
                    /
                  
                  usr
                  
                    /
                  
                  share
                  
                    /
                  
                  GeoIP
                  
                    /
                  
                  GeoIPCity.dat
                

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

测试GeoIP

重启Apaceh

                  1

                
                  
                    /
                  
                  etc
                  
                    /
                  
                  init.d
                  
                    /
                  
                  apache2 restart
                

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

                  1
2
3
4
5
6

                
                  
                    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
                  
                    )
                  
                  ;
?
                  
                    >
                  
                

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

                  1
2

                
                  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

                  1
2
3
4
5
6

                
                  
                    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模块。

                  1

                
                  pecl 
                  
                    install
                  
                   geoip
                

启用GeoIP PHP模块

                  1
2

                
                  
                    vi
                  
                  
                    /
                  
                  etc
                  
                    /
                  
                  php5
                  
                    /
                  
                  conf.d
                  
                    /
                  
                  geoip.ini

                  
                    extension
                  
                  =geoip.so
                

复制GeoIP数据库到数据目录

                  1

                
                  
                    cp
                  
                   GeoLiteCity.dat 
                  
                    /
                  
                  usr
                  
                    /
                  
                  local
                  
                    /
                  
                  share
                  
                    /
                  
                  GeoIP
                  
                    /
                  
                  GeoIPCity.dat
                

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

测试GeoIP

重启Apaceh

                  1

                
                  
                    /
                  
                  etc
                  
                    /
                  
                  init.d
                  
                    /
                  
                  apache2 restart
                

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

                  1
2
3
4
5
6

                
                  
                    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
                  
                    )
                  
                  ;
?
                  
                    >
                  
                

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

                  1
2

                
                  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,可能会出现以下错误提示:

                  1
2
3

                
                  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
                  
                

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

                  1
2
3
4

                
                  autoreconf 
                  
                    --force
                  
                  
.
                  
                    /
                  
                  configure

                  
                    make
                  
                  
                    make
                  
                  
                    install
                  
                

参考文档

http://www.google.com

http://blog.chinaunix.net/space.php?uid=642374&do=blog&cuid=1944521

http://stackoverflow.com/questions/3096989/libtool-version-mismatch-error

Ubuntu下安装GeoIP


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论