WordPress Security Best Practices on Your Own nginx Server
Server-side WordPress hardening that needs no security plugin: updates, nginx rules, login rate limits, fail2ban, wp-config.php settings and a checklist with a way to verify each item.
Most WordPress sites are compromised through an outdated plugin or theme, a weak or reused admin password, or a file the web server was allowed to write and then run. On your own VPS you can close these at the server level, where a compromised plugin cannot switch the protection off.
The measures below are: keep everything updated, add a few nginx rules, slow down login attempts, lock down wp-config.php and file ownership, limit the database user, and check the result regularly. None of them needs a security plugin.
The nginx directives and WP-CLI commands follow the official documentation. They were not run on a test server for this article, so test each change on your own site.
Updates come first
WordPress installs minor core releases automatically by default; these carry the security fixes. Do not disable that. Plugins and themes are not updated automatically unless you turn it on:
cd /var/www/example.com
wp plugin auto-updates enable --all
wp theme auto-updates enable --all
wp core version
wp plugin list --update=available
The last command should return an empty list. The current WordPress release is 7.1.5, a security release.
PHP and nginx are updated by the operating system, not by WordPress. Recent examples of why that matters are the PHP-FPM request smuggling flaw and the nginx HTTP/2 vulnerability. Turn on automatic security updates and stay on a PHP branch that still receives fixes (8.2 loses them on 31 December 2026).
Delete plugins and themes you do not use. An inactive plugin can still be reached by its URL. Keep one default theme as a fallback.
wp plugin list --status=inactive
wp plugin delete PLUGIN-SLUG
nginx rules
Add these inside the site's server block, above the general location ~ \.php$ block. nginx uses the first regular-expression location that matches, so the order matters.
# No PHP execution in uploads
location ~* ^/wp-content/uploads/.*\.php$ { deny all; }
# Hidden files (.git, .env, .user.ini), except Let's Encrypt validation
location ~ /\.(?!well-known) { deny all; }
# Never serve the configuration file
location = /wp-config.php { deny all; }
# XML-RPC: block unless something needs it
location = /xmlrpc.php { deny all; }
The uploads folder must be writable by PHP, so it is where an attacker's file lands; the first rule stops it from running. xmlrpc.php is an older remote API that is a common target for password guessing. Some services still use it, for example Jetpack. If you need it, replace deny all; with allow lines for the addresses that need access, followed by deny all;.
sudo nginx -t && sudo systemctl reload nginx
curl -sI https://example.com/wp-content/uploads/test.php | head -1
curl -sI https://example.com/xmlrpc.php | head -1
Both should answer 403.
Rate-limit the login page
limit_req limits how many requests per second or minute one IP address may make. Define a zone once in the http block (for example in a file under /etc/nginx/conf.d/):
limit_req_zone $binary_remote_addr zone=wplogin:10m rate=30r/m;
Then give the login page its own location in the site's server block. An exact-match location replaces the general PHP block for this URL, so it needs the same FastCGI lines as that block:
location = /wp-login.php {
limit_req zone=wplogin burst=5 nodelay;
limit_req_status 429;
include snippets/fastcgi-php.conf; # Ubuntu, Debian; otherwise copy your PHP block
fastcgi_pass unix:/run/php/php8.5-fpm.sock; # use the socket from your PHP block
}
The rate and burst are a starting point, not a documented recommendation. Rejected requests get status 503 unless limit_req_status changes it, as the limit_req documentation describes. Verify after a reload:
for i in $(seq 1 15); do curl -s -o /dev/null -w "%{http_code} " https://example.com/wp-login.php; done; echo
The first answers are 200, the rest 429. Behind Cloudflare or another proxy, nginx sees the proxy's address unless the real-IP module is configured; set that up first or the limit hits all visitors together.
Ban repeat offenders with fail2ban
fail2ban reads log files and blocks an IP address in the firewall after too many matching lines. For WordPress the simplest signal is repeated POST requests to wp-login.php in the nginx access log. A filter, /etc/fail2ban/filter.d/wordpress-login.conf:
[Definition]
failregex = ^<HOST> .* "POST /wp-login\.php
And a jail, /etc/fail2ban/jail.d/wordpress.conf:
[wordpress-login]
enabled = true
port = http,https
filter = wordpress-login
logpath = /var/log/nginx/access.log
maxretry = 10
findtime = 600
bantime = 3600
This is an example and must be adapted. The regular expression assumes nginx's default "combined" log format, where each line begins with the client address; a custom log_format, per-site log files or a proxy in front change that. It also counts successful logins, so keep maxretry generous. Test the filter against your real log before enabling it:
sudo fail2ban-regex /var/log/nginx/access.log /etc/fail2ban/filter.d/wordpress-login.conf
sudo systemctl restart fail2ban
sudo fail2ban-client status wordpress-login
wp-config.php settings
Disable the built-in file editor. It lets any administrator account, including a stolen one, edit plugin and theme PHP from the dashboard.
wp config set DISALLOW_FILE_EDIT true --raw
wp config get DISALLOW_FILE_EDIT
Check the salts. The eight keys and salts in wp-config.php sign login cookies. They must be long random values, not the placeholder text "put your unique phrase here". Generate new ones with:
wp config shuffle-salts
Everyone is logged out when they change. Do this once on a new or inherited site and after any suspected compromise; routine rotation is not needed.
File ownership and the database user
PHP should be able to write only where it has to. The less of the code tree the PHP-FPM user can modify, the less an exploited plugin can change. wp-config.php should be readable by its owner and the web server only (mode 640 or stricter). The trade-offs between easy updates and strict ownership are in WordPress file permissions.
The database user in wp-config.php should have rights on its own database and nothing else, and never be root:
sudo mariadb -e "SHOW GRANTS FOR 'wpuser'@'localhost';"
Expect one line with GRANT USAGE ON *.* and one granting privileges on `wordpress`.*. A line that grants real privileges ON *.* means a compromised site can read every database on the server.
Logins, HTTPS and headers
Give every administrator a unique, long password and a second factor. WordPress core has no two-factor login; the "Two-Factor" plugin from the WordPress.org directory adds app-based codes and is one option. List administrators and remove the ones nobody uses:
wp user list --role=administrator --fields=ID,user_login,user_email
Serve the whole site over HTTPS and redirect HTTP to it in nginx. Then add response headers in the server block:
add_header X-Content-Type-Options "nosniff" always;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
add_header Strict-Transport-Security "max-age=31536000" always;
Add the last one (HSTS) only when every part of the site works over HTTPS, because browsers will refuse plain HTTP for the stated time. nginx inherits add_header lines from the outer level only if the inner block defines none of its own, so repeat them in any location that has its own add_header. Check with curl -sI https://example.com/ | grep -iE 'strict|x-frame|nosniff|referrer'.
Verify core files regularly
wp core verify-checksums
wp plugin verify-checksums --all
These compare the installed files with the checksums published by WordPress.org and report any file that differs. A weekly run from cron gives early warning. If one reports changes you did not make, go to cleaning up a hacked WordPress site.
Checklist
| Measure | Where | How to verify |
|---|---|---|
| Core, plugins, themes current | WordPress | wp core check-update, wp plugin list --update=available |
| OS, PHP, nginx patched | Server | apt list --upgradable or dnf check-update |
| No PHP in uploads, no dotfiles | nginx | curl -sI returns 403 |
xmlrpc.php blocked or restricted | nginx | curl -sI returns 403 |
| Login rate limit | nginx | curl loop returns 429 |
| Login bans | fail2ban | fail2ban-client status wordpress-login |
| File editor off, salts set | wp-config.php | wp config get DISALLOW_FILE_EDIT |
| Database user limited | MariaDB | SHOW GRANTS |
| Two-factor for admins | WordPress | Log in with each admin account |
| HTTPS and headers | nginx | curl -sI |
| Files unmodified | WP-CLI | wp core verify-checksums |