Introduction to Fail2Ban for WordPress

Fail2Ban is an open-source intrusion prevention tool that protects servers from brute-force attacks by monitoring log files and banning malicious IP addresses. It works by scanning logs for suspicious activity (such as repeated failed login attempts) and automatically blocking offending IPs using firewall rules.

Fail2Ban for WordPress is particularly useful in preventing unauthorized access to wp-login.php and xmlrpc.php, two common targets for hackers. In this guide, we’ll walk you through installing and configuring Fail2Ban on an Ubuntu server (including AWS Lightsail Bitnami stacks) to enhance your WordPress security.

Step 1: Install Fail2Ban

First, update your system and install Fail2Ban:

Note: Bitnami stacks on AWS Lightsail don’t include Fail2Ban by default, so manual installation is required.

Step 2: Create a Fail2Ban Filter for WordPress

Since Bitnami logs WordPress requests differently, we need a custom filter.

Create a new filter file:

Add the following configuration:

Save and exit (CTRL+X, then Y, then ENTER).

Step 3: Create a Jail for WordPress

Next, set up a jail to define how Fail2Ban should handle WordPress attacks:

Add the following rules:

  • maxretry: Number of failed attempts before a ban.
  • findtime: Time window (in seconds) for max retries.
  • bantime: Duration (in seconds) an IP remains banned.
  • ignoreip: Whitelisted IPs that should never be banned.

Save and exit.

Step 4: Restart Fail2Ban

Apply the new configuration:

Step 5: Enable Fail2Ban on Boot

Ensure Fail2Ban starts automatically after a reboot:

Checking Banned IPs & Managing Fail2Ban

View Jail Status

Check active jails:

Check the WordPress jail specifically:

Unban an IP

If a legitimate user gets blocked, unban their IP:

Conclusion

By configuring Fail2Ban for WordPress, you significantly reduce the risk of brute-force attacks. Regularly monitor banned IPs and adjust settings as needed for optimal security.

For further hardening, consider: