How to install mod_maxminddb on linux server and confiure firewall blocking

By | July 7, 2025
Spread the love

To install mod_maxminddb using yum, follow these steps (works for CentOS/RHEL 7+ systems):

Step 1: Install EPEL (if not already)

sudo yum install epel-release

Step 2: Install dependencies

sudo yum install git gcc make libmaxminddb libmaxminddb-devel httpd-devel

Step 3: Clone and build mod_maxminddb

git clone https://github.com/maxmind/mod_maxminddb.git
cd mod_maxminddb
./configure
make
sudo make install

Step 4: Load the module in Apache

Add this line to your Apache config or a separate .conf file (like /etc/httpd/conf.modules.d/20-maxminddb.conf):

LoadModule maxminddb_module modules/mod_maxminddb.so

Then restart Apache:

sudo systemctl restart httpd

Step 5: Verify module is loaded

httpd -M | grep maxminddb

If you see maxminddb_module, it’s working.

Optional: Example Configuration

Create a file at /etc/apache2/conf.d/geoip.conf

<IfModule mod_maxminddb.c>
    # Enable mod_maxminddb
    MaxMindDBEnable On

    # Load the GeoLite2 Country database
    MaxMindDBFile COUNTRY_DB /usr/share/GeoIP/GeoLite2-Country.mmdb

    # Map environment variable for country code
    MaxMindDBEnv MM_COUNTRY_CODE COUNTRY_DB/country/iso_code

    # Allow only users from US or CA
    SetEnvIf MM_COUNTRY_CODE ^(US|CA)$ AllowedCountry

    # Allow if User-Agent matches specific bots (e.g., Googlebot or Bingbot)
    SetEnvIfNoCase User-Agent "Googlebot" allowed_bot
    SetEnvIfNoCase User-Agent "Bingbot" allowed_bot

    # Deny access to everyone not from US or CA
    <If !env('allowed_bot') && "%{ENV:MM_COUNTRY_CODE} != 'US' && %{ENV:MM_COUNTRY_CODE} != 'CA' && %{ENV:MM_COUNTRY_CODE} != 'IN'">
       Require all denied
    </If>
</IfModule>

Make sure you have file “GeoLite2-Country.mmdb” downloaded at /usr/share/GeoIP

sudo systemctl restart httpd