Monday, February 2, 2026

Block Website Access for Specific Country

 

This guide explains how to block access to a website directory based on country (United States, Mexico, and India) using Apache and the GeoIP module on Ubuntu.


Step 1: Install Required Packages

sudo apt update
sudo apt install apache2 libapache2-mod-geoip geoip-database -y


Step 2: Enable the GeoIP Module

sudo a2enmod geoip
sudo systemctl restart apache2
  • Verify that the module is loaded:
apachectl -M | grep geoip


Step 3: Verify GeoIP Database
  • You should see GeoIP.dat in the output.
ls /usr/share/GeoIP/


Step 4: Configure GeoIP in Apache Separate virtual host file or apache2.conf

<IfModule geoip_module>
    GeoIPEnable On
    GeoIPDBFile /usr/share/GeoIP/GeoIP.dat
</IfModule>

<Directory /var/www/html/>
    <RequireAll>
        Require all granted
        Require not env BlockCountry
    </RequireAll>

    SetEnvIf GEOIP_COUNTRY_CODE US BlockCountry
    SetEnvIf GEOIP_COUNTRY_CODE MX BlockCountry
    SetEnvIf GEOIP_COUNTRY_CODE IN BlockCountry
</Directory>


Result: Visitors from United States (US), Mexico (MX), and India (IN) will now be blocked from accessing the specified directory, while users from other countries will continue to have access.


No comments:

Post a Comment

testing