Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

Install GeoIP on a Linux server

This guide shows you how to install GeoIP so your server can tell which country a visitor comes from.

Dedicated Servers2 min read6 steps

What is GeoIP?

Every device online has an IP address. An IP address is like a home address for a computer. GeoIP is a list that matches IP addresses to places, such as a country. Websites use it to show the right language, or to block visitors from some countries.

You need root access for this guide. Root is the main administrator login of the server.

Tip: The old GeoIP "legacy" tool is no longer updated. Its newer version is called GeoIP2 or GeoLite2. The steps below use the old package, and the newer tools work in a similar way.

Steps

  1. Connect to your server with SSH.
  2. Install the program and its library. On Red Hat style systems:
    yum install GeoIP GeoIP-devel
    This installs GeoIP and the files other programs need.
  3. On Ubuntu or Debian, use:
    apt install geoip-bin geoip-database libgeoip-dev
    This installs the same things under Debian names.
  4. Test it with a sample address.
    geoiplookup 8.8.8.8
    This prints the country linked to that IP address.
  5. If you use PHP, install the PHP part.
    pecl install geoip
    This adds a PHP add-on. Then add extension=geoip.so to a PHP settings file and restart your web server.
  6. If you use Apache, you can add the Apache module. Install the package named mod_geoip, if your system offers it, then restart Apache.

Keep the data fresh

IP addresses move between countries. Old data becomes wrong over time. Update the list from time to time. Free GeoLite2 data now needs a free account on the MaxMind website and a license key. Read their instructions, then use their update tool called geoipupdate.

If you use cPanel and WHM

Do not change PHP by hand on a cPanel server unless you must. Use Module Installers in WHM for PHP add-ons.

Tip: Take a backup of your web server settings before you change them. Then restart the web server and check your sites still load.

Quick recap

  • GeoIP matches IP addresses to countries.
  • Install it with yum or apt, then test with geoiplookup.
  • Add the PHP or Apache part if your site needs it.
  • Update the data now and then. Newer GeoIP2 is the better choice.