← ServerPilot Docs

How to Install the PHP GeoIP Extension

In the examples shown, replace "X.Y" with your app's PHP version (for example, "7.1"). To install a PECL extension for multiple PHP versions, repeat the PECL extension installation for each PHP version.

The GeoIP extension allows you to find the location of an IP address, including the city, state, country, latitude, and longitude.

If you'd like to use the new GeoIP2, MaxMind now offers a pure PHP implementation that doesn't require a C extension: https://github.com/maxmind/GeoIP2-php.

Installing GeoIP on PHP 7.X

To install this extension on PHP 7.0, 7.1, 7.2, 7.3, or 7.4, run the following commands as your server's root user:

sudo apt-get -y install gcc make autoconf libc-dev pkg-config
sudo apt-get -y install libgeoip-dev
sudo pecl7.X-sp install geoip-beta

Once installed, create a configuration file for the extension and restart PHP by running the following command as root:

sudo bash -c "echo extension=geoip.so > /etc/php7.X-sp/conf.d/geoip.ini"
sudo service php7.X-fpm-sp restart

Installing GeoIP on PHP 5.X

To install this extension on PHP 5.4, 5.5, or 5.6, run the following commands as your server's root user:

sudo apt-get -y install gcc make autoconf libc-dev pkg-config
sudo apt-get -y install libgeoip-dev
sudo pecl5.X-sp install geoip

Once installed, create a configuration file for the extension and restart PHP by running the following command as root:

sudo bash -c "echo extension=geoip.so > /etc/php5.X-sp/conf.d/geoip.ini"
sudo service php5.X-fpm-sp restart

Verifying GeoIP Is Installed

You can check that the extension was installed with this command:

phpX.Y-sp -i | grep geoip

The output will look like this:

$ php7.1-sp -i | grep geoip
/etc/php7.1-sp/conf.d/geoip.ini,
geoip
geoip support => enabled
geoip extension version => 1.1.1
geoip library version => 1006000
geoip.custom_directory => no value => no value

Uninstalling the GeoIP Extension

To uninstall this extension, as root run the commands:

sudo rm /etc/phpX.Y-sp/conf.d/geoip.ini
sudo peclX.Y-sp uninstall geoip

Next, restart PHP-FPM with the command:

sudo service phpX.Y-fpm-sp restart
Last updated: October 8, 2018

Launch your first site in 5 minutes