🔐
Security

Protecting WordPress from Brute Force with fail2ban

27.11.2026
← All articles

Why the WordPress login is always a target

A large share of websites run WordPress, and everyone knows its login address: /wp-login.php. Bots try leaked username and password pairs against thousands of sites, and xmlrpc.php lets them test several passwords in one request. Even with a strong password these attacks load the server, because every attempt runs PHP and queries the database. Security plugins only act after the request reaches PHP; fail2ban watches server logs and bans repeat offenders at the firewall, so their connections never reach the web server. It needs root access, which means a VPS or your own server.

How fail2ban works

A filter defines with a regular expression which log lines count as failures, a jail ties the filter to a log file and sets thresholds, and an action performs the ban, usually via iptables, nftables or firewalld.

# Debian / Ubuntu
apt install fail2ban

# AlmaLinux / Rocky (needs EPEL)
dnf install epel-release
dnf install fail2ban

systemctl enable --now fail2ban

Do not edit /etc/fail2ban/jail.conf directly; put your own settings in /etc/fail2ban/jail.d/ so package updates do not overwrite them.

A filter for WordPress

The simplest approach counts POST requests to the login and XML-RPC endpoints in the access log. It also counts successful logins, but an administrator does not log in five times in ten minutes, whereas a bot makes hundreds of attempts. Save this as /etc/fail2ban/filter.d/wordpress.conf:

[Definition]
failregex = ^<HOST> -.*"POST /wp-login\.php
            ^<HOST> -.*"POST /xmlrpc\.php
ignoreregex =

<HOST> is the fail2ban token that captures the IP. The pattern suits Nginx and Apache logs in the standard combined format; adjust it if yours differs. A more precise option is a WordPress plugin that logs only failed logins, with the filter matching those entries.

The jail

On ISPmanager servers domain logs usually live in /var/www/httpd-logs/; on a plain Nginx install, in /var/log/nginx/. Create /etc/fail2ban/jail.d/wordpress.conf:

[wordpress]
enabled  = true
port     = http,https
filter   = wordpress
logpath  = /var/log/nginx/access.log
maxretry = 5
findtime = 600
bantime  = 3600
ignoreip = 127.0.0.1/8 ::1 203.0.113.10

This bans an IP for an hour after five attempts in ten minutes. Always add your office or home static IP to ignoreip so you cannot lock yourself out. Newer fail2ban versions support bantime.increment = true, making each repeat ban longer.

Testing and running

fail2ban-regex /var/log/nginx/access.log /etc/fail2ban/filter.d/wordpress.conf
fail2ban-client reload
fail2ban-client status wordpress
fail2ban-client set wordpress unbanip 203.0.113.7

If fail2ban-regex reports zero matches, the pattern does not fit your log format. Every ban and unban is recorded in /var/log/fail2ban.log.

A real-world case and caveats

A small travel agency in Fergana running on a VPS saw its server load climb for no obvious reason; the access log showed dozens of POST requests per second to xmlrpc.php. Within the first hour of enabling this jail more than a hundred IPs were banned and load returned to normal. Since the agency used no mobile app or external service over XML-RPC, access to that file was later blocked entirely.

On shared hosting you cannot install fail2ban yourself; the provider handles server-level protection. On a sayt.uz VPS you have root access and can apply everything above.

Related articles

🗝️ Don't keep passwords and keys in code: managing secrets properly 🦠 Malware Scanners on Hosting: How They Work and How to Read Results 🔑 Stolen LLM API key: how to cap your costs 🔁 Cleaned Site Hacked Again? The Entry Point Is Still Open
🌐 Language
🇺🇿 O⁠'zbek 🇺🇿 Özbek (yangi) 🇺🇿 Ўзбек 🇷🇺 Русский 🇬🇧 English ✓