CentOS 6.5 下安装php-geoip扩展

环境:CentOS 6.5 64bit

PHP版本:5.3.3


1.安装GeoIP

[root@localhost ~]# yum install GeoIP GeoIP-devel GeoIP-data
但是安装失败:

[root@localhost ~]# yum install GeoIP GeoIP-devel GeoIP-data
Loaded plugins: auto-update-debuginfo, fastestmirror, refresh-packagekit, security
Setting up Install Process
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
No package GeoIP available.
No package GeoIP-devel available.
No package GeoIP-data available.
Error: Nothing to do
解决方法参考: https://serverfault.com/questions/372978/installing-geoip-on-centos
安装epel,执行以下命令:

[root@localhost ~]# yum install epel-release


之后重新通过yum安装成功:

[root@localhost ~]# yum install GeoIP GeoIP-devel GeoIP-data

[root@localhost ~]# rpm -qi GeoIP
Name        : GeoIP                        Relocations: (not relocatable)
Version     : 1.6.5                             Vendor: Fedora Project
Release     : 1.el6                         Build Date: Tue 14 Apr 2015 03:28:46 PM CST
Install Date: Tue 23 Jan 2018 05:06:19 PM CST      Build Host: buildvm-09.phx2.fedoraproject.org
Group       : Development/Libraries         Source RPM: GeoIP-1.6.5-1.el6.src.rpm
Size        : 317185                           License: LGPLv2+
Signature   : RSA/8, Tue 14 Apr 2015 11:16:45 PM CST, Key ID 3b49df2a0608b895
Packager    : Fedora Project
URL         : http://www.maxmind.com/app/c
Summary     : Library for country/city/organization to IP address or hostname mapping
Description :
GeoIP is a C library that enables the user to find the country that any IP
address or hostname originates from.

It uses file based databases that can optionally be updated on a weekly basis
by installing the geoipupdate-cron (IPv4) and/or geoipupdate-cron6 (IPv6)
packages.
[root@localhost ~]# rpm -ql GeoIP
/usr/bin/geoiplookup
/usr/bin/geoiplookup6
/usr/lib64/libGeoIP.so.1
/usr/lib64/libGeoIP.so.1.6.5
/usr/share/doc/GeoIP-1.6.5
/usr/share/doc/GeoIP-1.6.5/AUTHORS
/usr/share/doc/GeoIP-1.6.5/COPYING
/usr/share/doc/GeoIP-1.6.5/ChangeLog
/usr/share/doc/GeoIP-1.6.5/NEWS.md
/usr/share/doc/GeoIP-1.6.5/README.md
/usr/share/man/man1/geoiplookup.1.gz
/usr/share/man/man1/geoiplookup6.1.gz
[root@localhost ~]# rpm -qi GeoIP-devel
Name        : GeoIP-devel                  Relocations: (not relocatable)
Version     : 1.6.5                             Vendor: Fedora Project
Release     : 1.el6                         Build Date: Tue 14 Apr 2015 03:28:46 PM CST
Install Date: Tue 23 Jan 2018 05:06:24 PM CST      Build Host: buildvm-09.phx2.fedoraproject.org
Group       : Development/Libraries         Source RPM: GeoIP-1.6.5-1.el6.src.rpm
Size        : 23331                            License: LGPLv2+
Signature   : RSA/8, Tue 14 Apr 2015 11:04:29 PM CST, Key ID 3b49df2a0608b895
Packager    : Fedora Project
URL         : http://www.maxmind.com/app/c
Summary     : Development headers and libraries for GeoIP
Description :
Development headers and static libraries for building GeoIP-based applications.
[root@localhost ~]# rpm -ql GeoIP-devel
/usr/include/GeoIP.h
/usr/include/GeoIPCity.h
/usr/lib64/libGeoIP.so
/usr/lib64/pkgconfig/geoip.pc

关于epel的介绍:

http://fedoraproject.org/wiki/EPEL

https://jingyan.baidu.com/article/c843ea0baaf40477931e4a19.html

安装epel-release后查看yum repolist:

[root@localhost ~]# yum repolist
Loaded plugins: auto-update-debuginfo, fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * epel: ftp.cuhk.edu.hk
 * epel-debuginfo: ftp.cuhk.edu.hk
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
repo id                    repo name                                                          status
base                       CentOS-6 - Base - mirrors.aliyun.com                                6,706
debug                      CentOS-6 - Debuginfo                                                5,431
epel                       Extra Packages for Enterprise Linux 6 - x86_64                     12,450
epel-debuginfo             Extra Packages for Enterprise Linux 6 - x86_64 - Debug              3,165
extras                     CentOS-6 - Extras - mirrors.aliyun.com                                 47
updates                    CentOS-6 - Updates - mirrors.aliyun.com                               871
repolist: 28,670

epel已经在repolist后列出,并且显示提供了2万个以上的软件包。

epel源被安装在/etc/yum.repos.d/epel.repo

[root@localhost ~]# rpm -ql epel-release
/etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6
/etc/rpm/macros.ghc-srpm
/etc/yum.repos.d/epel-testing.repo
/etc/yum.repos.d/epel.repo
/usr/share/doc/epel-release-6
/usr/share/doc/epel-release-6/GPL
[root@localhost ~]# ll /etc/yum.repos.d/
total 28
-rw-r--r--  1 root root 2572 Jul  2  2014 CentOS-Base.repo
-rw-r--r--. 1 root root 1926 Nov 27  2013 CentOS-Base.repo.bak
-rw-r--r--. 1 root root  638 Aug 16 14:44 CentOS-Debuginfo.repo
-rw-r--r--. 1 root root  630 Nov 27  2013 CentOS-Media.repo
-rw-r--r--. 1 root root 3664 Nov 27  2013 CentOS-Vault.repo
-rw-r--r--  1 root root  957 Nov  5  2012 epel.repo
-rw-r--r--  1 root root 1056 Nov  5  2012 epel-testing.repo


2.下载GeoIP的数据库,有精确到国家、城市,包括IPv6,自行选择

下载城市数据库如下:

[root@localhost Downloads]# wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
[root@localhost Downloads]# gzip -d GeoLiteCity.dat.gz
[root@localhost Downloads]# mkdir /var/lib/GeoIP
[root@localhost Downloads]# mv GeoLiteCity.dat /var/lib/GeoIP/GeoIPCity.dat


下载国家数据库如下:

[root@localhost Downloads]# wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
[root@localhost Downloads]# gzip -d GeoIP.dat.gz
[root@localhost Downloads]# mv GeoIP.dat /var/lib/GeoIP/GeoIP.dat

3.源码安装GeoIP pecl扩展

下载地址:http://pecl.php.net/package/geoip

[root@localhost Downloads]# wget http://pecl.php.net/get/geoip-1.1.1.tgz
[root@localhost Downloads]# tar zxvf geoip-1.1.1.tgz
[root@localhost Downloads]# cd geoip-1.1.1
[root@localhost geoip-1.1.1]# phpize
Configuring for:
PHP Api Version:         20090626
Zend Module Api No:      20090626
Zend Extension Api No:   220090626
[root@localhost geoip-1.1.1]# whereis php-config
php-config: /usr/bin/php-config /usr/share/man/man1/php-config.1.gz
[root@localhost geoip-1.1.1]# ./configure --with-php-config=/usr/bin/php-config --with-geoip
执行./configure后有WARNING:

configure: WARNING: You will need re2c 0.13.4 or later if you want to regenerate PHP parsers.
解决方案: http://blog.csdn.net/u014558668/article/details/79139874

WARNING处理后,重新

[root@localhost geoip-1.1.1]# ./configure --with-php-config=/usr/bin/php-config --with-geoip

[root@localhost geoip-1.1.1]# make
[root@localhost geoip-1.1.1]# make install
安装成功:

[root@localhost geoip-1.1.1]# make install
Installing shared extensions:     /usr/lib64/php/modules/


4.配置php支持geoip扩展

[root@localhost geoip-1.1.1]# ll /usr/lib64/php/modules/
total 5904
-rwxr-xr-x 1 root root   66448 Mar 22  2017 curl.so
-rwxr-xr-x 1 root root 1843120 Mar 22  2017 fileinfo.so
-rwxr-xr-x 1 root root   65975 Jan 23 17:37 geoip.so
-rwxr-xr-x 1 root root 1093629 Jan 23 10:46 imagick.so
-rwxr-xr-x 1 root root  170384 Mar 22  2017 intl.so
-rw-r--r-- 1 root root 1362440 Jan 22 20:17 ioncube_loader_lin_5.3.so
-rwxr-xr-x 1 root root   36200 Mar 22  2017 json.so
-rwxr-xr-x 1 root root  144580 Jan 18 11:36 mcrypt.so
-rwxr-xr-x 1 root root   98536 Jun 22  2012 memcache.so
-rwxr-xr-x 1 root root  136832 Mar 22  2017 mysqli.so
-rwxr-xr-x 1 root root   54408 Mar 22  2017 mysql.so
-rwxr-xr-x 1 root root   30112 Mar 22  2017 pdo_mysql.so
-rwxr-xr-x 1 root root  102456 Mar 22  2017 pdo.so
-rwxr-xr-x 1 root root   24640 Mar 22  2017 pdo_sqlite.so
-rwxr-xr-x 1 root root  261392 Mar 22  2017 phar.so
-rwxr-xr-x 1 root root  332896 Mar 22  2017 soap.so
-rwxr-xr-x 1 root root   44368 Mar 22  2017 sqlite3.so
-rwxr-xr-x 1 root root   49320 Mar 22  2017 tidy.so
-rwxr-xr-x 1 root root   83088 Mar 22  2017 zip.so
[root@localhost geoip-1.1.1]# whereis php.ini
php: /usr/bin/php /etc/php.ini /etc/php.d /usr/lib64/php /usr/include/php /usr/share/php /usr/share/man/man1/php.1.gz
[root@localhost geoip-1.1.1]# cd /etc/php.d/
[root@localhost php.d]# ll
total 68
-rw-r--r-- 1 root root   49 Mar 22  2017 curl.ini
-rw-r--r-- 1 root root   57 Mar 22  2017 fileinfo.ini
-rw-r--r-- 1 root root   55 Jan 23 11:01 imagick.ini
-rw-r--r-- 1 root root   49 Mar 22  2017 intl.ini
-rw-r--r-- 1 root root   49 Mar 22  2017 json.ini
-rw-r--r-- 1 root root   53 Jan 18 13:47 mcrypt.ini
-rw-r--r-- 1 root root 1329 Jun 22  2012 memcache.ini
-rw-r--r-- 1 root root   53 Mar 22  2017 mysqli.ini
-rw-r--r-- 1 root root   51 Mar 22  2017 mysql.ini
-rw-r--r-- 1 root root   47 Mar 22  2017 pdo.ini
-rw-r--r-- 1 root root   59 Mar 22  2017 pdo_mysql.ini
-rw-r--r-- 1 root root   61 Mar 22  2017 pdo_sqlite.ini
-rw-r--r-- 1 root root   49 Mar 22  2017 phar.ini
-rw-r--r-- 1 root root   49 Mar 22  2017 soap.ini
-rw-r--r-- 1 root root   55 Mar 22  2017 sqlite3.ini
-rw-r--r-- 1 root root   49 Mar 22  2017 tidy.ini
-rw-r--r-- 1 root root   47 Mar 22  2017 zip.ini
[root@localhost php.d]# echo "extension=geoip.so" >> geoip.ini
[root@localhost php.d]# ll
total 72
-rw-r--r-- 1 root root   49 Mar 22  2017 curl.ini
-rw-r--r-- 1 root root   57 Mar 22  2017 fileinfo.ini
-rw-r--r-- 1 root root   19 Jan 23 17:42 geoip.ini
-rw-r--r-- 1 root root   55 Jan 23 11:01 imagick.ini
-rw-r--r-- 1 root root   49 Mar 22  2017 intl.ini
-rw-r--r-- 1 root root   49 Mar 22  2017 json.ini
-rw-r--r-- 1 root root   53 Jan 18 13:47 mcrypt.ini
-rw-r--r-- 1 root root 1329 Jun 22  2012 memcache.ini
-rw-r--r-- 1 root root   53 Mar 22  2017 mysqli.ini
-rw-r--r-- 1 root root   51 Mar 22  2017 mysql.ini
-rw-r--r-- 1 root root   47 Mar 22  2017 pdo.ini
-rw-r--r-- 1 root root   59 Mar 22  2017 pdo_mysql.ini
-rw-r--r-- 1 root root   61 Mar 22  2017 pdo_sqlite.ini
-rw-r--r-- 1 root root   49 Mar 22  2017 phar.ini
-rw-r--r-- 1 root root   49 Mar 22  2017 soap.ini
-rw-r--r-- 1 root root   55 Mar 22  2017 sqlite3.ini
-rw-r--r-- 1 root root   49 Mar 22  2017 tidy.ini
-rw-r--r-- 1 root root   47 Mar 22  2017 zip.ini
[root@localhost php.d]# cat geoip.ini
; Enable geoip extension module
extension=geoip.so

指定GeoIP数据的路径:

[root@localhost php.d]# cat geoip.ini
; Enable geoip extension module
extension=geoip.so
geoip.custom_directory=/var/lib/GeoIP/

重启apache:

[root@localhost ~]# service httpd restart


5.测试是否安装成功

在网站目录下新建一个phpinfo.php测试页面,写上下面代码,保存。

 
在浏览器中打开phpinfo.php 会看到如下的信息,说明php-geoip扩展已经安装成功。

CentOS 6.5 下安装php-geoip扩展_第1张图片




php-geoip扩展也可以通过yum安装,安装方法如下:

[root@localhost ~]# yum list | grep geoip
geoipupdate.x86_64                      2.2.1-2.el6               @epel
php-pecl-geoip.x86_64                   1.0.8-3.el6               @epel
geoip-geolite.noarch                    2013.04-1.el6             epel
geoipupdate-cron.noarch                 2.2.1-2.el6               epel
geoipupdate-cron6.noarch                2.2.1-2.el6               epel
geoipupdate-debuginfo.x86_64            2.2.1-2.el6               epel-debuginfo
lighttpd-mod_geoip.x86_64               1.4.47-1.el6              epel
mod_geoip.x86_64                        1.2.10-1.el6              epel
mod_geoip-debuginfo.x86_64              1.2.10-1.el6              epel-debuginfo
nginx-mod-http-geoip.x86_64             1.10.2-1.el6              epel
opensips-mmgeoip.x86_64                 1.7.2-2.el6               epel
php-pecl-geoip-debuginfo.x86_64         1.0.8-3.el6               epel-debuginfo
python-pygeoip.noarch                   0.2.6-1.el6               epel
uwsgi-plugin-geoip.x86_64               2.0.15-1.el6              epel
[root@localhost ~]# yum install php-pecl-geoip.x86_64
Loaded plugins: auto-update-debuginfo, fastestmirror, refresh-packagekit, security
Setting up Install Process
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * epel: mirror.vinahost.vn
 * epel-debuginfo: mirror.vinahost.vn
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package php-pecl-geoip.x86_64 0:1.0.8-3.el6 will be installed
--> Processing Dependency: libGeoIP.so.1()(64bit) for package: php-pecl-geoip-1.0.8-3.el6.x86_64
--> Running transaction check
---> Package GeoIP.x86_64 0:1.6.5-1.el6 will be installed
--> Processing Dependency: GeoIP-data for package: GeoIP-1.6.5-1.el6.x86_64
--> Running transaction check
---> Package GeoIP-GeoLite-data.noarch 0:2017.07-1.el6 will be installed
--> Processing Dependency: GeoIP-GeoLite-data-extra = 2017.07-1.el6 for package: GeoIP-GeoLite-data-2017.07-1.el6.noarch
--> Running transaction check
---> Package GeoIP-GeoLite-data-extra.noarch 0:2017.07-1.el6 will be installed
--> Finished Dependency Resolution


Dependencies Resolved


===============================================================================================================================================================================================================
 Package                                                       Arch                                        Version                                             Repository                                 Size
===============================================================================================================================================================================================================
Installing:
 php-pecl-geoip                                                x86_64                                      1.0.8-3.el6                                         epel                                       14 k
Installing for dependencies:
 GeoIP                                                         x86_64                                      1.6.5-1.el6                                         epel                                      113 k
 GeoIP-GeoLite-data                                            noarch                                      2017.07-1.el6                                       epel                                      492 k
 GeoIP-GeoLite-data-extra                                      noarch                                      2017.07-1.el6                                       epel                                       24 M


Transaction Summary
===============================================================================================================================================================================================================
Install       4 Package(s)
[root@localhost ~]# rpm -ql php-pecl-geoip
/etc/php.d/geoip.ini
/usr/lib64/php/modules/geoip.so
/usr/share/doc/php-pecl-geoip-1.0.8
/usr/share/doc/php-pecl-geoip-1.0.8/ChangeLog
/usr/share/doc/php-pecl-geoip-1.0.8/README
/usr/share/pear/.pkgxml/php-pecl-geoip.xml
[root@localhost ~]# rpm -qi php-pecl-geoip
Name        : php-pecl-geoip               Relocations: (not relocatable)
Version     : 1.0.8                             Vendor: Fedora Project
Release     : 3.el6                         Build Date: Sun 28 Oct 2012 04:29:05 PM CST
Install Date: Mon 29 Jan 2018 05:52:00 PM CST      Build Host: buildvm-34.phx2.fedoraproject.org
Group       : Development/Languages         Source RPM: php-pecl-geoip-1.0.8-3.el6.src.rpm
Size        : 29718                            License: PHP
Signature   : RSA/8, Tue 30 Oct 2012 12:49:42 AM CST, Key ID 3b49df2a0608b895
Packager    : Fedora Project
URL         : http://pecl.php.net/package/geoip
Summary     : Extension to map IP addresses to geographic places
Description :
This PHP extension allows you to find the location of an IP address
City, State, Country, Longitude, Latitude, and other information as
all, such as ISP and connection type. It makes use of Maxminds geoip
database
[root@localhost ~]# php -m | grep geoip
geoip

你可能感兴趣的:(PHP)