Docs
Secure WordPress
Secure WordPress
WordPress sites lose their secrets in the same few places, and none of them is a hole in WordPress itself. They are leftovers: a copy of wp-config.php saved during a quick fix, a debug log someone turned on and forgot, defaults nobody switched off. This guide shows how to find them yourself and close them.
| What | Where to look | Why it matters |
|---|---|---|
| wp-config.php copies and the debug log | wp-config.php.bak, .old, .save, wp-config.php~ and /wp-content/debug.log |
The copy holds the database password in plain text. The log shows errors, file paths and sometimes database queries. |
| XML-RPC | /xmlrpc.php |
One request can try hundreds of passwords, past most login limits. |
| Usernames | /?rest_route=/wp/v2/users |
The REST API lists the login name of everyone who has published something. |
| Folder listing | /wp-content/uploads/ |
An open listing shows every uploaded file, also the ones no page links to. |
wp-config.php itself is safe: the server runs it as PHP and returns an empty page. A copy with a different ending (.bak, .save, ~) is not PHP to the server, so it is sent as text. Editors make these copies on their own: nano saves .save when it is interrupted, gedit and emacs leave ~.
If a wp-config.php copy was public
Treat it as a leak. Deleting the file stops the next download, not the ones that already happened:
- Delete the copy (and any other
wp-configcopies next to it). - Change the database password: in cPanel under Databases → MySQL Databases → Current Users, in Plesk under Databases → User Management. Then put the new password in
DB_PASSWORDinwp-config.php. The site is down between the two steps, so have both windows open. - Replace the keys and salts: open
https://api.wordpress.org/secret-key/1.1/salt/and paste the eight lines over the ones inwp-config.php. Everyone is logged out, including whoever used the old ones. - Check the users under Users → All Users for administrators you do not know.
Check it yourself
Replace example.com with your site:
for f in wp-config.php.bak wp-config.php.old wp-config.php.save 'wp-config.php~' wp-content/debug.log; do
echo "$(curl -s -o /dev/null -w '%{http_code}' "https://example.com/$f") $f"
done
curl -s https://example.com/xmlrpc.php; echo
curl -s 'https://example.com/?rest_route=/wp/v2/users' | head -c 300; echo
curl -s https://example.com/wp-content/uploads/ | grep -io '<title>[^<]*'
- A file with
200: open it in the browser. A real copy starts with<?phpand containsdefine( 'DB_NAME'; some servers answer200with the home page for any address, and that is not a leak. XML-RPC server accepts POST requests only.means XML-RPC is open.403or404means blocked.- A list that starts with
[{"id":1,"name":…,"slug":"admin"shows the usernames (slugis the login name).rest_no_routeorrest_user_cannot_viewmeans they are hidden. <title>Index of /wp-content/uploadsis a folder listing.
How to fix
WordPress
Copies and the debug log. Delete the wp-config copies with your host's file manager or over SFTP (they sit next to wp-config.php), and delete wp-content/debug.log. Then stop the log from coming back. In wp-config.php, either switch logging off:
define( 'WP_DEBUG', false );
define( 'WP_DEBUG_LOG', false );
or, if you need the log, write it outside the public folder (WordPress 5.1 or newer):
define( 'WP_DEBUG_LOG', '/home/example/logs/wp-debug.log' );
define( 'WP_DEBUG_DISPLAY', false );
XML-RPC and usernames. Add this with the free Code Snippets plugin (Snippets → Add New, run everywhere) or at the end of your child theme's functions.php:
// xmlrpc.php answers 403. Leave this out if you use Jetpack or the
// WordPress mobile app with this site: both still need XML-RPC.
add_action( 'init', function () {
if ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) {
status_header( 403 );
exit;
}
} );
// The REST API lists users only to logged-in users (the editor needs it).
add_filter( 'rest_endpoints', function ( $endpoints ) {
if ( ! is_user_logged_in() ) {
unset( $endpoints['/wp/v2/users'], $endpoints['/wp/v2/users/(?P<id>[\d]+)'] );
}
return $endpoints;
} );
The often quoted add_filter( 'xmlrpc_enabled', '__return_false' ) switches off only the methods that log in: the file still answers and pingbacks through it still work.
Folder listing is a server setting; WordPress cannot change it. Use the tab for your server, or ask your host. Until then, an empty index.php in wp-content/uploads/ hides the listing of that one folder.
Apache and LiteSpeed
Add this to the .htaccess in the site's root folder, above the # BEGIN WordPress block (WordPress rewrites what is between its markers):
# No folder listings.
Options -Indexes
# wp-config copies and the debug log.
<FilesMatch "^(wp-config\.php.+|debug\.log)$">
Require all denied
</FilesMatch>
# XML-RPC (leave out if you use Jetpack or the WordPress mobile app).
<Files xmlrpc.php>
Require all denied
</Files>
Require all denied is Apache 2.4 and LiteSpeed. If the site answers with a 500 error afterwards, the host does not allow Options in .htaccess: remove that line and ask the host to turn off indexes.
The rules hide the files, they do not delete them. Delete the copies and the log as well, and set up logging as in the WordPress tab. Usernames are hidden in WordPress itself (WordPress tab).
nginx
Add this to the site's server { … } block, above the location ~ \.php$ block:
# wp-config copies and the debug log.
location ~* /(wp-config\.php.+|debug\.log)$ {
deny all;
}
# XML-RPC (leave out if you use Jetpack or the WordPress mobile app).
location = /xmlrpc.php {
deny all;
}
sudo nginx -t && sudo systemctl reload nginx
nginx does not list folders unless autoindex on; is set somewhere. If the uploads folder shows a listing, find that line (grep -rn autoindex /etc/nginx/) and remove it. In Plesk, put the same blocks in Apache & nginx Settings → Additional nginx directives.
Delete the copies and the log as well, and set up logging as in the WordPress tab. Usernames are hidden in WordPress itself (WordPress tab).
Cloudflare
Behind Cloudflare (orange cloud), a WAF rule blocks the files and XML-RPC on every plan:
- Go to Security → WAF → Custom rules (in the newer dashboard Security → Security rules) and create a rule.
- Name it WordPress files, click Edit expression and paste:
(http.request.uri.path contains "/wp-config.php" or http.request.uri.path eq "/wp-content/debug.log" or http.request.uri.path eq "/xmlrpc.php")
- Choose Block and Deploy. Leave out the
xmlrpc.phppart if you use Jetpack or the WordPress mobile app.
Do not block /wp/v2/users in Cloudflare: the block editor uses it, and editing pages would stop working. Hide usernames in WordPress itself (WordPress tab). Cloudflare protects only traffic that goes through it, so delete the copies and the log on the server too.
Confirm the fix
Run the commands from Check it yourself again. Every file should answer 403 or 404, xmlrpc.php should not print the XML-RPC sentence, and the user list should answer rest_no_route or rest_user_cannot_view.