How to Harden WordPress Security, wp-config.php, and File Permissions
How to Harden WordPress Security, wp-config.php, and File Permissions
WordPress powers over 40% of the web, making it a primary target for credential stuffing, automated XML-RPC brute-force attacks, and malicious PHP uploads. Applying enterprise security hardening stops common attack vectors before they reach application logic.
This guide provides a comprehensive hardening checklist for locking down the filesystem, securing wp-config.php, and disabling unnecessary attack surfaces.
1. Enterprise File System Permissions
Improper filesystem permissions allow compromised plugins or web processes to overwrite executable files. Follow the principle of least privilege:
| Path | Recommended Permission | Owner / Group | Purpose |
|---|---|---|---|
| All Directories |
755 (rwxr-xr-x) | www-data:www-data | Readable and executable, writable only by owner |
| All PHP / Content Files | 644 (rw-r--r--) | www-data:www-data | Writable by owner, readable by web server |
| wp-config.php | 600 or 640 | www-data:www-data | Readable only by owner and web server process |
| .htaccess | 644 | www-data:www-data | Readable by Apache, protected from public browsers |
Applying Permissions via Linux Shell
Run the following commands from your WordPress root directory:
# Set directory permissions to 755
find /var/www/html/ -type d -exec chmod 755 {} ;
# Set file permissions to 644
find /var/www/html/ -type f -exec chmod 644 {} ;
# Lock down wp-config.php
chmod 600 /var/www/html/wp-config.php
2. Hardening wp-config.php Security Constants
Add the following security constants to your wp-config.php file above the /* That's all, stop editing! */ comment line:
// 1. Disable the Built-in Theme and Plugin Code Editor
// Prevents attackers who obtain an admin password from injecting PHP backdoors via wp-admin
define('DISALLOW_FILE_EDIT', true);
// 2. Prevent Unauthorized Plugin & Theme Installations (Optional for High-Security Stacks)
// define('DISALLOW_FILE_MODS', true);
// 3. Force SSL for all Administrative Sessions
define('FORCE_SSL_ADMIN', true);
// 4. Disable Unfiltered HTML for Administrators and Editors
define('DISALLOW_UNFILTERED_HTML', true);
DISALLOW_FILE_EDIT to true is one of the highest-value security settings in WordPress. It eliminates the Theme Editor and Plugin Editor menu options under Appearance and Plugins, closing the primary route attackers use to plant webshells.3. Blocking Direct PHP Execution in wp-content/uploads/
The wp-content/uploads/ directory must be writable so administrators can upload images, PDFs, and media assets. However, no PHP script should ever execute inside uploads/.
If an attacker exploits a vulnerable third-party plugin to upload a .php file disguised as an image, this rule prevents the web server from executing it.
Create an .htaccess file inside wp-content/uploads/.htaccess:
# Block direct execution of PHP scripts in uploads
<Files *.php>
Order Deny,Allow
Deny from all
</Files>
For Nginx, add this rule inside your server { ... } block:
location ~* ^/wp-content/uploads/.*.php$ {
deny all;
}
4. Disabling XML-RPC (xmlrpc.php)
WordPress includes xmlrpc.php for legacy remote publishing applications. Today, modern integrations use the WordPress REST API with OAuth or Application Passwords.
Attackers frequently abuse xmlrpc.php for:
- Amplified brute-force password attacks via
system.multicall. - Distributed Denial of Service (DDoS) reflection attacks via
pingback.ping.
Disabling XML-RPC via .htaccess
Add the following directive to your root .htaccess file:
# Block all external access to xmlrpc.php
<Files xmlrpc.php>
Order Deny,Allow
Deny from all
</Files>
Disabling XML-RPC via Filter Hook
Add this snippet to your theme’s functions.php:
// Completely disable XML-RPC methods
add_filter('xmlrpc_enabled', '__return_false');
5. Protecting wp-config.php from Browser Access
Ensure your web server never serves wp-config.php as plain text if PHP parsing ever fails. Add this to your root .htaccess:
# Protect wp-config.php
<Files wp-config.php>
Order Allow,Deny
Deny from all
</Files>
6. Disabling Username Enumeration
By default, visiting https://example.com/?author=1 redirects to https://example.com/author/username/, revealing the administrative username to attackers.
Add this snippet to your theme’s functions.php to block author query scanning:
/**
* Block user enumeration via ?author=N queries
*/
add_action('template_redirect', function() {
if (is_author() || isset($_GET['author'])) {
wp_safe_redirect(home_url('/'), 301);
exit;
}
});
7. Hardening Summary Checklist
- [x]
wp-config.phppermissions set to600or640 - [x]
DISALLOW_FILE_EDITdefined astrueinwp-config.php - [x] PHP execution blocked in
wp-content/uploads/ - [x] Access to
xmlrpc.phpblocked via.htaccessor web server rule - [x] HTTPS enforced across all admin sessions (
FORCE_SSL_ADMIN) - [x] Two-factor authentication (2FA) enforced on all administrator accounts
Was this technical article helpful?
Help us continuously refine Xyphla documentation.