# Enforce geo-blocking on apache (PHP app)
[TOC]
## Geo-blocking by country on Bitnami Apache (AWS Lightsail)
Block visitors from a specific country and redirect them to an error page, using MaxMind GeoLite2 and `mod_maxminddb` on a Bitnami stack running on AWS Lightsail.
**Environment:**
- AWS Lightsail instance (Debian 12)
- Bitnami Contao stack
- Apache 2.4.54 at `/opt/bitnami/apache`
---
### Step 1: Install build dependencies and MaxMind library
```bash
sudo apt-get install -y libmaxminddb0 libmaxminddb-dev mmdb-bin gcc make
```
---
### Step 2: Compile and install mod_maxminddb
Bitnami ships its own Apache binary, so the Debian package `libapache2-mod-maxminddb` won't work. Compile from source against Bitnami's Apache instead.
```bash
cd /tmp
wget https://github.com/maxmind/mod_maxminddb/releases/download/1.2.0/mod_maxminddb-1.2.0.tar.gz
tar xzf mod_maxminddb-1.2.0.tar.gz
cd mod_maxminddb-1.2.0
autoreconf -fiv
./configure --with-apxs=/opt/bitnami/apache/bin/apxs
make
sudo make install
```
Verify the module installed and was auto-added to `httpd.conf`:
```bash
ls /opt/bitnami/apache/modules/ | grep maxmind
grep -i maxminddb /opt/bitnami/apache/conf/httpd.conf
```
Expected output: `LoadModule maxminddb_module modules/mod_maxminddb.so`
---
### Step 3: Download the GeoLite2 database
Register for a free account at [https://www.maxmind.com/en/geolite2/signup](https://www.maxmind.com/en/geolite2/signup), then generate a license key under **Account → Manage License Keys**.
```bash
sudo mkdir -p /opt/bitnami/apache/geoip
cd /opt/bitnami/apache/geoip
sudo wget --user=YOUR_ACCOUNT_ID --password=YOUR_LICENSE_KEY \
"https://download.maxmind.com/geoip/databases/GeoLite2-Country/download?suffix=tar.gz" \
-O GeoLite2-Country.tar.gz
sudo tar xzf GeoLite2-Country.tar.gz --strip-components=1 --wildcards "*.mmdb"
ls *.mmdb
```
Test the database works:
```bash
mmdblookup --file /opt/bitnami/apache/geoip/GeoLite2-Country.mmdb --ip 8.8.8.8 country iso_code
# Should return: "US"
```
---
### Step 4: Enable mod_remoteip
AWS Lightsail sits behind internal load balancers, so Apache sees a private `172.26.x.x` IP rather than the real client IP. The real IP arrives in the `X-Forwarded-For` header. Enable `mod_remoteip` to handle this:
```bash
sudo sed -i 's/#LoadModule remoteip_module modules\/mod_remoteip.so/LoadModule remoteip_module modules\/mod_remoteip.so/' /opt/bitnami/apache/conf/httpd.conf
```
Verify:
```bash
grep "LoadModule remoteip_module" /opt/bitnami/apache/conf/httpd.conf
```
---
### Step 5: Configure the HTTP vhost
Edit `/opt/bitnami/apache/conf/vhosts/example.com-vhosts.conf`:
```apache
Access to this website is restricted in your region. If you believe this is an error, please contact the site administrator.