How to display country code in the Apache log using the IP2Location Apache module

How to display country code in the Apache log using the IP2Location Apache module


Apache is a popular open-source web server being used in both the Linux and Windows environments. In this article, we will demonstrate how to add a country code to your Apache log via the IP2Location Apache Module for the Debian Linux environment.



We will assume that you have a functioning Apache 2.4 web server. We will not cover the installation of Apache 2.4 as there are many guides on the web for that. You will need to install the apache2-dev package if you don’t have it installed.

sudo apt-get install apache2-dev


The last pre-requisite is the IP2Location BIN database file which you can get from: (commercial)

or (free LITE version, less accurate)

Download and decompress the BIN file into a folder called ip2location.



First of all, we need to get the IP2Location C library via the below link:

Download the above and decompress in a folder called ip2location.

Navigate to the IP2Location-C-Library-master sub-folder.

cd IP2Location-C-Library-master


Now, compile the IP2Location C library by running the below commands.

sudo autoreconf -i -v --force
sudo ./configure
sudo make
sudo make install



Next, get the IP2Location Apache module via the below link:

Download the above and decompress into the ip2location folder from above.

In your ip2location folder, you should now have 2 sub-folders called ip2location-apache-master and IP2Location-C-Library-master. Navigate to the ip2location-apache-master folder.

cd ../ip2location-apache-master


Now, we will compile both the IP2Location C library and the IP2Location Apache module.

sudo apxs2 -i -a -L /usr/local/lib/ -I ../IP2Location-C-Library-master/libIP2Location/ -l IP2Location -c mod_ip2location.c


Add a symbolic link to the shared object file.

sudo ln -s /usr/local/lib/ /usr/lib/




Add following lines into the /etc/apache2/apache2.conf file. Modify the IP2LocationDBFile parameter for your own folder and BIN filename.

<IfModule mod_ip2location.c>
  IP2LocationEnable On
  IP2LocationDetectProxy Off
  IP2LocationSetmode ENV

In that file, look for the row below:

LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined

Change it to look like the below:

LogFormat "%{IP2LOCATION_COUNTRY_SHORT}e %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined

Then save the file. The above format modification adds another field to the front of the log entry to show the country code for the geolocation information of that IP address.


Restart Apache for the above settings to take effect

Run the following command to restart.

sudo systemctl restart apache2



Open your browser and navigate to your webpage

Your log should now have an entry like the below:

MY - - [24/Oct/2019:06:08:02 +0000] "GET / HTTP/1.1" 200 3380 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0"

In this case, the country code is MY for Malaysia.

Was this article helpful?

Related Articles