> ## Content Index
> Fetch the complete content index at: https://www.ctrl.blog/llms.txt
> Use this file to discover other available public pages before exploring further.

# How to set up PHP-GeoIP with automatic database updates
- URL: https://www.ctrl.blog/entry/how-to-php-geoip-updates/
- Published: 2016-09-29T18:38:00.000Z
- Updated: 2026-08-23T22:10:44.000Z
- Description: Instructions for setting up auto-updates for the GeoIP location databases, and set up the PHP-GeoIP extension in Fedora/Ubuntu Linux.
- Author: Daniel Aleksandersen
- Tags: Networking, Webmaster

Here is how to configure and setup automatic database updates for IPv4 and IPv6 to geographical location data with the free GeoLite2, GeoIP, service from MaxMind in Debian, Fedora Linux, and Ubuntu.

The GeoLite2 service provides free data for mapping IPv4 and IPv6 addresses to geographical locations. This service and an auto-update service is included in the package repository of all major Linux distributions, but the setup instructions vary a little for each distribution. Here are the complete steps for the most popular Linux distributions.

You can skip installing the `php-geoip` package, and the step adjusting `php.ini` file if you intend to use another GeoIP compatible implementation and just want to know how to configure `geoipupdate`.

Jump to instructions for [Fedora Linux](#section-geoip-fedora) and derivatives or for [Ubuntu](#section-geoip-ubuntu), Debian, and derivatives.

### Fedora Linux

Tested with Fedora Linux 24, 23, and 22.

Fedora Linux has already done almost all the work for you, and you just need to install a few extra configuration packages to get going.

1. Run the `geoipupdate` program once to initiate the database. It will be kept updated by the cron jobs that were installed along with the package.
2. Restart the `php-fpm`, `httpd`, `nginx`, etc. services as necessary.

Open `/etc/php.ini` for editing, and append the following at the bottom:

```shell
geoip.custom_directory="/usr/share/GeoIP/"
```

Install all the packages with the following command:

```shell
dnf install geoipupdate geoipupdate-cron* php-pecl-geoip
```

### Ubuntu

Tested with Ubuntu 16.04 “Xenial Xerus” and Debian 8.5, but the below instructions may not work with older versions without making some adaptations because of packaging changes.

1. Run the `geoipupdate` program once to initiate the database.
2. Restart the `php-fpm`, `apache2`, `nginx`, etc. services as necessary.

Open `/etc/php.ini` for editing, and append the following at the bottom:

```ini
geoip.custom_directory="/usr/share/GeoIP/"
```

Make the maintenance task executable:

```shell
chmod +x  /etc/cron.weekly/geoipupdate
```

Install a weekly maintenance job for updating the databases in cron:

```shell
echo -e '#!/bin/sh' "ngeoipupdate > /dev/null" > /etc/cron.weekly/geoipupdate
```

Open `/etc/GeoIP.conf` for editing, and locate and adjust these three parameters:

```
UserID 999999
LicenseKey 000000000000
ProductIds GeoLite2-City GeoLite2-Country GeoLite-Legacy-IPv6-City GeoLite-Legacy-IPv6-Country 506 517 533
```

Pay attention to the quotation marks used, they’re significant for escape sequences.

Copy the sample GeoIP configuration file to your configuration directory:

```shell
cp /usr/share/doc/geoipupdate/templates/GeoIP.conf.default /etc/GeoIP.conf
```

Install all the packages with the following command:

```shell
apt install geoipupdate geoip-database php-geoip
```

Make sure you meet the [license and attribution requirements](https://dev.maxmind.com/geoip/geoip2/geolite2/?ref=ctrl.blog) for using the GeoLite2 database. MaxMind uses a permissive Creative Commons Attribution-ShareAlike 4.0 International License for this data. You can consider upgrading to their paid GeoIP2 database if you need increased precision.

IP based location services may [not be very accurate for IPv6](https://www.ctrl.blog/entry/ip-auth-factor/) yet, but you should make sure to test that you’ve configured support for both IPv4 and IPv6 in your program. The above instructions should ensure that you’ve access to both IPv4 and IPv6 location databases, but supporting both my require additional implementation steps.

#### Sources

- [Automatic Updates for GeoIP2 and GeoIP Legacy Databases](https://dev.maxmind.com/geoip/geoipupdate/?ref=ctrl.blog)
- [File list of package geoipupdate in xenial of architecture amd64](https://packages.ubuntu.com/xenial/amd64/geoipupdate/filelist?ref=ctrl.blog)