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
- Connect to your server with SSH.
- Install the program and its library. On Red Hat style systems:
This installs GeoIP and the files other programs need.yum install GeoIP GeoIP-devel - On Ubuntu or Debian, use:
This installs the same things under Debian names.apt install geoip-bin geoip-database libgeoip-dev - Test it with a sample address.
This prints the country linked to that IP address.geoiplookup 8.8.8.8 - If you use PHP, install the PHP part.
This adds a PHP add-on. Then addpecl install geoipextension=geoip.soto a PHP settings file and restart your web server. - 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.
Quick recap
- GeoIP matches IP addresses to countries.
- Install it with
yumorapt, then test withgeoiplookup. - Add the PHP or Apache part if your site needs it.
- Update the data now and then. Newer GeoIP2 is the better choice.