install module
sudo apt install libapache2-mod-geoip
enable module
sudo a2enmod geoip
Method 1 – Server Wide Configuration
sudo nano /etc/apache2/mods-available/geoip.conf
edit conf
<IfModule mod_geoip.c>
GeoIPEnable On
GeoIPDBFile /usr/share/GeoIP/GeoIP.dat
</IfModule>
Method 2 – Virtual Host Configuration
sudo nano /etc/apache2/sites-available/virtualhost.conf
Add the following below the ServerAlias directive.
GeoIPEnable On
GeoIPDBFile /usr/share/GeoIP/GeoIP.dat
GeoIPScanProxyHeaders On
Manage Restrictions
Block Certain Countries
Create or open the .htaccess file which is inside your web root directory and add the following snippet to block countries.
SetEnvIf GEOIP_COUNTRY_CODE UA BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE VN BlockCountry
Deny from env=BlockCountry
The above configuration will block requests from the above 2 countries. You can include as per your wish.
Allow Certain Countries
Create or open the .htaccess file which is inside your web root directory and add the following snippet to allow countries.
SetEnvIf GEOIP_COUNTRY_CODE US AllowCountry
SetEnvIf GEOIP_COUNTRY_CODE CA AllowCountry
Deny from all
Allow from env=AllowCountry
Blocking a client based on country
This example show you how to block clients based on the country code that GeoIP sets.
GeoIPEnable On
GeoIPDBFile /path/to/GeoIP.dat
SetEnvIf GEOIP_COUNTRY_CODE CN BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE RU BlockCountry
# ... place more countries here
Deny from env=BlockCountry
Allowing clients based on country
This example show you how to allow only clients from specific countries.
GeoIPEnable On
GeoIPDBFile /path/to/GeoIP.dat
SetEnvIf GEOIP_COUNTRY_CODE US AllowCountry
SetEnvIf GEOIP_COUNTRY_CODE CA AllowCountry
SetEnvIf GEOIP_COUNTRY_CODE MX AllowCountry
# ... place more countries here
Deny from all
Allow from env=AllowCountry
GeoIP options
GEOIP_ADDR
GEOIP_CONTINENT_CODE
GEOIP_COUNTRY_CODE
GEOIP_COUNTRY_NAME
GEOIP_REGION
GEOIP_REGION_NAME
GEOIP_CITY
Top comments (1)
This module is deprecated. See details here blog.maxmind.com/2020/06/retiremen...