Skip to content
ES EN
Theme: automatic

Secure Apache httpd: Source Install, ModSecurity CRS and Fail2Ban

A practical guide to building Apache httpd 2.4 from source, hardening it, adding ModSecurity with the OWASP CRS and banning abusers with Fail2Ban.

Updated on

This is a fresh version, rewritten around what the current documentation says: build the Apache HTTP Server 2.4 from source, harden it, put a web application firewall in front of your apps and let Fail2Ban react to what the logs record. None of these layers is complete alone; together they raise the cost of an attack.

A web server rack in a data center with network cables and status lights
A public web server should be built in layers, not trusted to one control. Image: Unsplash — Red error messages on a black computer screen indicating blocked web resources

What you need

Apache is released as source code, and the build instructions cover Unix and Unix-like systems. You need a C compiler such as GCC, the Apache Portable Runtime libraries and Perl 5 for some helper scripts; the install page lists the full requirements. If you prefer your distribution's package, expect a different layout and different defaults, as described on the distribution layout page. Ubuntu users can follow Ubuntu's own tutorial.

  1. Download the latest 2.4 release from the official download page and extract it.
  2. Configure, compile and install, choosing your own prefix.
  3. Edit httpd.conf, then start the server and test it.
  4. Apply the hardening advice from the Apache security tips.
  5. Add ModSecurity and the OWASP Core Rule Set.
  6. Install Fail2Ban; its README says it is ready to read standard sshd and Apache logs.

Build and install Apache from source

The official quick sequence is below. Replace NN with the current version number and PREFIX with the installation path; if you omit the prefix, it defaults to /usr/local/apache2.

tar xzf httpd-NN.tar.gz
cd httpd-NN
./configure --prefix=PREFIX
make
make install
vi PREFIX/conf/httpd.conf
PREFIX/bin/apachectl -k start
Tip Upgrading within 2.4 (for example 2.4.66 to 2.4.67) has its own section in the install guide. Read it before overwriting a running server, and subscribe to the Apache announcements list so you hear about security updates.

Harden the server

The official Security Tips page is the reliable baseline. It covers keeping up to date, mitigating denial-of-service attacks, permissions on the ServerRoot directories, server side includes, CGI, protecting system settings and server files by default, and watching your logs. It also reminds us that most compromises come from add-on code, CGI scripts or the operating system rather than the server itself, so patch everything.

Add ModSecurity and the OWASP Core Rule Set

ModSecurity is an open source web application firewall engine. Mind the versions: the newer v3 line, libmodsecurity, no longer depends on Apache, while ModSecurity for Apache (v2.x) is still maintained, as the project README explains. Pair the engine with the OWASP Core Rule Set. For installation, follow the official CRS documentation.

Terminal window showing server log lines scrolling past
Logs are the raw material for both the WAF and Fail2Ban. Image: Unsplash — Security, privacy, and performance status with fix options

Ban repeat offenders with Fail2Ban

The Fail2Ban README says it scans log files and updates firewall rules to reject IP addresses with too many failed login attempts, for a configurable time. It is ready to read standard logs such as those for sshd and Apache, and can be configured for other logs. The README also notes IPv6 support since v0.10 and lists Python 3.5 or newer and setuptools as source-install requirements.

This example preserves values from our original post; they are not Fail2Ban defaults. Adjust them to your traffic. Example jail settings:

[DEFAULT]
ignoreip = 127.0.0.1
banaction = iptables-multiport
bantime = 600
findtime = 600
maxretry = 5

With these illustrative values, five failures within 600 seconds trigger a 600-second ban. The Fail2Ban README warns that the software cannot eliminate the risk of weak authentication, so use public-key or two-factor access for services that matter.

Tip Put your own admin address in ignoreip before enabling jails, and test with a tolerant bantime first. Locking yourself out of SSH on a remote server is an avoidable mistake.

Original source: Apache HTTP Server Project

Article generated with AI.larebelion

Falcon

· Signals analyst · Riyadh

“Four tools, none of them magic: the guide that matters is the one you write after reading your own logs.”