Would you like to learn how to install Apache and deny access from a country? In this tutorial, we are going to configure the Apache server to block the access from a country.

• Ubuntu 18
• Ubuntu 19
• Ubuntu 20
• Apache 2.4.41

In our example, the Apache server is hosting the website WWW.GAMEKING.TIPS.

Equipment list

The following section presents the list of equipment used to create this tutorial.

As an Amazon Associate, I earn from qualifying purchases.

Tutorial Apache - GEOIP2 database installation

Access the MAXMIND website and create a new account.

Access your account profile and create a new key.

Nginx - GeoIP2 key

Add the MAXMIND repository to your Ubuntu Linux.

Copy to Clipboard

Install the package named GEOIPUPDATE.

Copy to Clipboard

Edit the GEOIPUPDATE configuration file.

Copy to Clipboard

Insert your account ID and license key.

Here is the file, before our configuration.

Copy to Clipboard

Here is the file, after our configuration.

Copy to Clipboard

Update the database of GEOIP2.

Copy to Clipboard

Verify the GEOIP2 database installation.

Copy to Clipboard

Here is the command output.

Copy to Clipboard

You installed the GEOIP2 database for countries and cities.

Congratulations! You have finished the installation of GEOIPUPDATE.

Tutorial Apache - MaxMind module installation

Install the Apache server.

Copy to Clipboard

Install the required packages.

Copy to Clipboard

Install the library named LIBMAXMINDDB.

Copy to Clipboard

Download the latest version of the Apache module named MOD_MAXMINDDB.

Copy to Clipboard

Compile and install the Apache module.

Copy to Clipboard

Congratulations! You have finished the MAXMIND module installation on the Apache server.

Tutorial Apache - Blocking access from a country

Enable the required Apache modules.

Copy to Clipboard

Edit the Apache configuration file for the website.

Copy to Clipboard

Add the following lines to this configuration file.

Copy to Clipboard

In our example, we denied access from Brazil and the United States.

Verify the list of country codes.

Here is the file, before our configuration.

Copy to Clipboard

Here is the file, after our configuration.

Copy to Clipboard

Restart the Apache service.

Copy to Clipboard

The Apache server will deny access based on the country of the source IP address.

From a remote computer in a restricted country, try to access your website.
Copy to Clipboard

Here is the command output.

Copy to Clipboard

Congratulations! You are able to configure Apache to restrict access to your website based on the country of the request.

Tutorial GEOIP2 - Database update

Create a scheduled task to update the GEOIP2 database.

Copy to Clipboard

Here is the file content.

Copy to Clipboard

Congratulations! You have configured the GEOIP2 database automatic update.