Control Panels

IP Geolocation on cPanel: GeoLite2 Setup with PHP, Apache and CSF (2026)

GeoIP allows websites to detect a visitor’s location based on their IP address. It provides details such as country, city, ISP, and connection type. If you’re running a cPanel server and need to install GeoIP, follow this updated guide for 2025.

Short answer: Use MaxMind’s GeoLite2 databases with the current tools: create a free MaxMind account, install geoipupdate to download and refresh the databases, then read them with MaxMind’s PHP library (geoip2/geoip2) or the Apache module mod_maxminddb. The old mod_geoip and the legacy GeoIP library cannot read GeoLite2 .mmdb files, so do not use them on a new setup. If your site is behind Cloudflare, the CF-IPCountry header may be all you need.

What IP geolocation does

IP geolocation looks up an IP address in a database and returns an estimated country, region or city. Websites use it to show local prices or languages, to apply tax rules, to fill in a country on forms, or to block traffic from places they do not serve. Servers use it to block countries at the firewall.

The result is an estimate. Country data is usually reliable, while city data is often wrong or missing. Visitors on a VPN, a mobile network or a corporate proxy may appear somewhere else. Never use geolocation alone for anything that must be exact, such as legal age or identity checks.

Common uses on a hosting server

  • Show local content: pick a default language, currency or shipping region from the visitor’s country, and let visitors change it.
  • Fill in forms: pre-select the country on sign-up and checkout forms to save visitors a step.
  • Reduce abuse: add extra checks, such as a captcha, for logins from countries where you have no customers.
  • Read your traffic: group log entries or analytics by country to see where visitors come from.
  • Limit an admin area: allow logins to a control panel or admin page only from the countries where your team works.

Legacy GeoIP and GeoLite2: what changed

Legacy GeoIP (old) GeoIP2 and GeoLite2 (current)
Database format .dat files .mmdb files
Apache module mod_geoip mod_maxminddb
PHP access The old geoip PECL extension MaxMind’s geoip2/geoip2 library, or the maxminddb extension
Database updates Discontinued Updated regularly with geoipupdate

Important:

The legacy tools cannot read GeoLite2 files, and the old free databases are no longer updated. Guides that install mod_geoip or download GeoIP.dat will give you missing or out-of-date data.

Step 1: create a MaxMind account and licence key

  1. Sign up for a free GeoLite2 account on the MaxMind website.
  2. Log in, open Manage License Keys, and create a new licence key.
  3. Note your Account ID and the key. The key is shown only once, so store it in a password manager.

Read the GeoLite2 licence terms. Among other things, they require you to keep the database up to date.

Step 2: install geoipupdate and download the databases

geoipupdate is MaxMind’s official tool for downloading and refreshing databases. Run these commands as root.

# AlmaLinux, Rocky Linux, CloudLinux (EPEL, where your release includes it)
dnf install -y epel-release
dnf install -y geoipupdate

# Ubuntu and Debian
apt update && apt install -y geoipupdate

If your distribution does not have a package, download the current release from MaxMind’s geoipupdate page on GitHub. Then edit the configuration file, usually /etc/GeoIP.conf:

AccountID 123456
LicenseKey your_licence_key
EditionIDs GeoLite2-Country GeoLite2-City

Protect the file so only root can read it, then download the databases:

chmod 600 /etc/GeoIP.conf
geoipupdate -v
ls -l /usr/share/GeoIP/      # the folder is set by DatabaseDirectory in GeoIP.conf

Step 3: keep the databases up to date

MaxMind updates GeoLite2 several times a month. Add a cron job so your copy stays current:

# /etc/cron.d/geoipupdate  (runs every Wednesday and Saturday at 03:15)
15 3 * * 3,6 root /usr/bin/geoipupdate

Check the path with which geoipupdate. Some packages add their own timer, so check systemctl list-timers before you add a second job.

Step 4a: look up a country in PHP

This is the simplest option for most cPanel websites. In the site folder, install the library with Composer:

cd ~/public_html
composer require geoip2/geoip2

Then look up the visitor’s country:

<?php
require __DIR__ . '/vendor/autoload.php';

use GeoIp2\Database\Reader;

$reader = new Reader('/usr/share/GeoIP/GeoLite2-Country.mmdb');
try {
    $record = $reader->country($_SERVER['REMOTE_ADDR']);
    echo $record->country->isoCode;   // for example "TH" or "US"
} catch (GeoIp2\Exception\AddressNotFoundException $e) {
    echo 'unknown';
}

On shared hosting, the database folder may not be readable by your account. Ask support where the server’s copy is, or keep your own copy outside public_html and update it with geoipupdate from a cron job.

Step 4b: use the Apache module

mod_maxminddb adds the location to Apache environment variables, so rewrite rules and applications can use it without code changes. On a cPanel server, check EasyApache 4 for a packaged module first. A module built by hand can break when EasyApache updates Apache. Once the module is loaded, a configuration looks like this:

<IfModule mod_maxminddb.c>
    MaxMindDBEnable On
    MaxMindDBFile COUNTRY_DB /usr/share/GeoIP/GeoLite2-Country.mmdb
    MaxMindDBEnv MM_COUNTRY_CODE COUNTRY_DB/country/iso_code
</IfModule>

Applications can then read MM_COUNTRY_CODE from the environment.

Sites behind Cloudflare

If the site uses Cloudflare, turn on IP Geolocation in the Cloudflare dashboard. Cloudflare then sends the visitor’s country in the CF-IPCountry header, and you do not need a database on the server. Also make sure the server logs and your application see the real visitor IP, not Cloudflare’s. Our Cloudflare and cPanel guide explains the setup.

Country blocking with CSF

To block or allow whole countries at the firewall, use CSF instead of the web server. In /etc/csf/csf.conf, choose a country data source with CC_SRC. If you choose MaxMind, enter your licence key in MM_LICENSE_KEY. Then list country codes in CC_DENY or CC_ALLOW, and restart CSF. Large country lists use a lot of memory, so block only what you need. See our CSF firewall guide for the basics.

Privacy

IP addresses can be personal data under laws such as the GDPR. Store only what you need, for example the country code rather than the full IP address, and mention location lookups in your privacy policy.

Troubleshooting

Problem Likely cause Fix
geoipupdate fails with an authentication error Wrong Account ID or licence key Check both values in GeoIP.conf, or create a new key.
Every visitor shows the same country The site sees a proxy or Cloudflare IP Restore the real visitor IP, or use the CF-IPCountry header.
PHP error: file not found or not readable Wrong path, or the account cannot read the folder Check the path and permissions, or keep a copy inside your home folder.
Results look out of date The update job is not running Run geoipupdate -v by hand and check the cron job or timer.

Need help with this?

Need GeoLite2 or country blocking set up on your server? Our team can install and configure it for you.

See server management plansContact @CustomerPanel

Frequently asked questions

Can I still use mod_geoip on cPanel?

It is not recommended. mod_geoip reads only the legacy .dat format, which MaxMind no longer updates. Use mod_maxminddb or the PHP library with GeoLite2 databases.

Is GeoLite2 free?

Yes. GeoLite2 is free with a MaxMind account and licence key, under MaxMind’s licence terms. MaxMind also sells more accurate GeoIP2 databases.

How accurate is IP geolocation?

Country results are usually accurate. City results are less reliable, and VPNs, proxies and mobile networks can show a visitor in the wrong place.

How often should I update the GeoLite2 databases?

Once or twice a week with geoipupdate. MaxMind’s licence requires you to keep the database up to date.

Can I block countries without changing my website?

Yes. Use CSF country blocking with CC_DENY or CC_ALLOW, or use Cloudflare’s firewall rules if the site is behind Cloudflare.


Sources