Admin
Moderating
Donor Member
Master Member
Elite Member
Active Member
- May 31, 2020
- 2,980
- 1,693
This guide covers file permission rules, Apache and Nginx configuration, security hardening, cron and maintenance for self-hosted WordPress.
# Disable dangerous PHP functions (if host supports php_value)
Keep default WordPress rewrite rules for permalinks at the bottom:
1. File & Folder Permissions (Base Security)
Never use 777 globally, it is extremely unsafe.- Regular folders: 755
- PHP, CSS, JS, HTML files: 644
- Folders requiring write permission (only these):wp-content/uploads/, wp-content/cache/, wp-content/wflogs/ (if using Wordfence)Set writable folders to 755.
- Critical folders must stay read-only: wp-includes/, wp-admin/
- wp-config.php: set to 600 (best) or 644. Restrict access as much as possible.
Principle: PHP files should NOT have write permission on production.Delete readme.html, license.txt, wp-config-sample.php after site setup.
2. Apache (.htaccess rules, place at site root)
Code:
# Disable directory listing
Options -Indexes
# Block direct access to PHP files inside uploads
<DirectoryMatch "^/wp-content/uploads">
<FilesMatch "\.(php|phtml|php5|phar)$">
Require all denied
</FilesMatch>
</DirectoryMatch>
# Protect wp-config.php
<Files "wp-config.php">
Require all denied
</Files>
# Protect xmlrpc.php (brute force attack target)
<Files "xmlrpc.php">
Require all denied
</Files>
# Disable dangerous PHP functions (if host supports php_value)
Code:
php_value disable_functions eval,passthru,system,shell_exec,popen,proc_open
Keep default WordPress rewrite rules for permalinks at the bottom:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
3. Nginx server block rules
autoindex off;
# Block PHP execution inside upload folder
location ~* ^/wp-content/uploads/.*\.(php|phtml|phar)$ {
return 403;
}
# Block access to wp-config
location ~ /wp-config\.php {
return 403;
}
# Block xmlrpc.php
location ~ /xmlrpc\.php {
return 403;
}
# Block access to readme / license files
location ~* (readme\.html|license\.txt|wp-config-sample\.php) {
return 403;
}
# WordPress permalinks
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
client_max_body_size 32M;
4. WP Cron Configuration
WP cron is used for scheduled posts, email notifications, plugin scheduled tasks.By default WP cron triggers on page visits and can be unreliable for high traffic sites.Option A: Replace WP internal cron with system cron (recommended)
- Edit wp-config.php, add this line to disable native wp-cron:
define( 'DISABLE_WP_CRON', true );
- Create system crontab job:
*/15 * * * * php -q /home/USER/public_html/wp-cron.php > /dev/null 2>&1
Use same PHP version for CLI as web PHP.
Option B: Keep default WP cron
Just monitor if scheduled posts and plugin tasks run normally.5. WordPress core & login security settings
- Use complex unique admin username. Never use admin / admin123.
- Enable 2FA for admin accounts via security plugins.
- Limit login attempts, block brute force attacks.
- Hide WordPress version number from frontend source code.Add to functions.php (child theme):
remove_action('wp_head', 'wp_generator');
- Restrict wp-admin access by IP in Apache/Nginx if you have static IP.
- Keep WordPress core, themes and plugins updated. Remove unused themes/plugins completely, do not just deactivate them.
6. Database & Backup Policy
- Schedule automatic full backup: database dump + all website files.
- Store backups off-server, not on the same web host.
- Use strong database user password; database user only get required privileges.
- Periodically scan database for suspicious injected links or malware.
7. Security plugin recommendations & maintenance tips
- Use security plugins for malware scan, file integrity check, firewall: Wordfence / Sucuri.
- Scan files regularly, watch for modified PHP files in wp-content folder.
- Audit all Patched themes/plugins carefully, test them on staging first.
- Disable unused plugin REST API endpoints if not needed.
- Clear cache after modifying server rules or theme files.
Important Reminders
- Webserver rules and permission hardening reduce attack surface, but cannot fully protect you from malicious Patched themes or plugins. Always test Patched resources on staging environment before production deployment.
- Monitor server outbound logs to detect unknown external callbacks or heartbeat requests.
- If abnormal file changes or outbound requests are found, immediately disable the suspicious theme/plugin and restore backup.