安装/配置

Table of Contents

User Contributed Notes

Michelangelo van Dam 21-Mar-2019 10:45
As of January 2, 2019 Maxmind discontinued the original GeoLite databases that we have been using in all these examples. You can read the full announcement here: https://support.maxmind.com/geolite-legacy-discontinuation-notice/

This means that if you require to have an updated version of the GeoIP information, there are a few steps you need to do:
1. Install the GeoIP2 client libraries (Composer, Phar)
2. Download the new GeoIP2 datasets
3. Update your application to make use of this new API and dataset

You can find detailed instructions on http://maxmind.github.io/GeoIP2-php/

C extension
Yes, there is a C extension available! Check out https://github.com/maxmind/MaxMind-DB-Reader-php for more details on how to install and use this extension.
ewwink at yahoo dot com 27-Mar-2013 04:23
compiled .dll or php_geoip.dll is availbale at:

http://windows.php.net/downloads/pecl/releases/geoip/1.0.8/

tested and worked with PHP Version 5.3.9 in windows 7 Home Premium
sballeste at gmail dot com 30-Apr-2011 02:57
If the data files under /usr/share/GeoIP/ are not readable by apache, apache's process will crash with a segfault on calls to geoip functions.
ben at SPAMLESS dot ism dot net dot nz 28-Apr-2011 09:20
SUSE (as root):

# zypper install php5-devel
# SuSEconfig
# mkdir -p /usr/share/GeoIP
# wget http://pecl.php.net/get/geoip-1.0.7.tgz
(whatever the latest version is from http://pecl.php.net/package/geoip )
# tar -xzf geoip-1.0.7.tgz
# cd geoip-1.0.7/
# phpize
# ./configure
# make
# make install
# cp /etc/php5/conf.d/gd.ini /etc/php5/conf.d/geoip.ini
# vi /etc/php5/conf.d/geoip.ini
Change gd.so to geoip.so
:wq!
# vi /etc/php5/apache2/php.ini
Find the [gd] section and add a new section afterwards:
[geoip]
geoip.custom_directory = /usr/share/GeoIP/
:wq!
# /etc/init.d/apache2 restart

Check phpinfo() that geoip is loaded and the default location for the DB's is set.

If you want the free DBs that are update roughly monthly, follow the "Lite" links from http://www.maxmind.com/app/country and download the tar.gz to /usr/share/GeoIP/ and extract.
bob at qww dot cz 19-Aug-2009 12:00
To install geoip on debian lenny:

wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
gunzip GeoLiteCity.dat.gz
sudo mkdir -v /usr/share/GeoIP
sudo mv -v GeoLiteCity.dat /usr/share/GeoIP/GeoIPCity.dat

sudo apt-get install php5-geoip

and then try it in PHP:
print_r(geoip_record_by_name('php.net'));

returns:
Array
(
    [country_code] => US
    [country_code3] => USA
    [country_name] => United States
    [region] => CA
    [city] => Sunnyvale
    [postal_code] => 94089
    [latitude] => 37.4249000549
    [longitude] => -122.007400513
    [dma_code] => 807
    [area_code] => 408
)
munitoris at inetspec dot com 14-Jul-2009 03:12
At the time of this writing, the PECL binaries at http://pecl4win.php.net/ are NOT AVAILABLE.

Specifically, the php_geoip.dll extension is not available in a pre-compiled form for Windoze PHP developers.

There are only two alternative solutions to self-compiling for Windoze (AFAIK):

1) Use the "Pure" PHP version of the GeoIP API from MaxMind available here (http://www.maxmind.com/download/geoip/api/php/).  This solution is not really acceptable for production machines because of its slower speed.  However, it does work for development.

2) Use the older version of this pre-compiled extension (http://www.gknw.de/php/ext_win32/php-5.2.1_geoip-w32.zip).  It worked on my development system using Windoze XP sp2, Apache 2.2.3 and PHP 5.2.10.  There are also older versions for PHP 5.1.2 and PHP 5.1.4 in the same directory.
matt at kynx dot org 14-Dec-2008 10:16
Installation on OSX (Leopard) running MAMP

First you need MacPorts installed and operational:
http://www.macports.org

Use that to install libgeoip. From a terminal window, do:

    $ sudo port install libgeoip

This installs the library in /opt/local. Unfortunately the current version of the PECL extension doesn't know to look there, so you'll need to download and compile manually from http://pecl.php.net/package/geoip

Assuming it's in your download directory, extract it:

    $ cd ~/Downloads
    $ tar -xzf geoip-1.0.3.tgz
    $ cd geoip-1.0.3

Now edit the config.m4 file and change the SEARCH_PATH variable, as described in the following bug report:
http://pecl.php.net/bugs/bug.php?id=14795

Now you should be able to compile and install the extension as usual. Make sure you use the phpize from the MAMP package, not the one that ships with OSX:

    $ /App*/MAMP/bin/php5/bin/phpize
    $ ./configure
    $ make
    $ sudo make install

If phpize complains that it cannot find files under /Applications/MAMP/bin/php5/include, make sure you have the development version of MAMP installed (link towards the bottom of the MAMP download page).

You're nearly there! The extension will have installed in /usr/lib/php. You need to copy it into your MAMP extension directory:

    $ cd /App*/MAMP/bin/php5/lib/php/ext*/no-debug*/
    $ mv /usr/lib/php/ext*/no-debug*/geoip.so .

Now edit the php.ini file in /Applications/MAMP/conf/php5/ and add the following line:

     extension=geoip.so

Restart your web server and check phpinfo() - the geoip extension should now be listed.

Good luck!
code at edoceo dot com 08-Dec-2008 01:12
Installation on Gentoo (2008.0) as follows:

emerge dev-libs/geoip
pecl install geoip

Then fetch the databases to /usr/share/GeoIP (wrapped!)
cd /usr/share/GeoIP
wget http://geolite.maxmind.com/download/geoip/ \
  database/GeoLiteCity.dat.gz
wget http://geolite.maxmind.com/download/geoip/ \
  database/GeoLiteCountry/GeoIP.dat.gz
PHP8中文手册 站长在线 整理 版权归PHP文档组所有