WordPress service running & security hardening guide

Summary
This guide covers various security measures for self-hosted WordPress installations, including file permissions, Apache and Nginx configuration, WP Cron settings, core & login security settings, database backup policy, and recommendations for security plugins. Key points include avoiding global 777 permissions, setting appropriate folder permissions (e.g., 644 for PHP files, 755 for writable folders), protecting critical directories like wp-admin and wp-config.php with restrictive permissions, disabling write permission on production environments, configuring .htaccess or Nginx server blocks to block unauthorized access, using system crontab jobs instead of native WP cron for reliability in high traffic sites, enforcing strong admin username policies, enabling 2FA for admins, limiting login attempts, hiding WordPress version number, updating core and plugins, backing up databases off-server, regularly scanning files with security plugins, and testing patched resources on a staging environment before deployment. Important reminders emphasize the need to monitor server logs and test changes in a staging environment before production use.
Admin
Moderating
Donor Member
Master Member
Elite Member
Active Member
Joined
May 31, 2020
Messages
2,980
Reaction score
1,693
This guide covers file permission rules, Apache and Nginx configuration, security hardening, cron and maintenance for self-hosted WordPress.

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)​

  1. Edit wp-config.php, add this line to disable native wp-cron:
define( 'DISABLE_WP_CRON', true );
  1. 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​

  1. Use complex unique admin username. Never use admin / admin123.
  2. Enable 2FA for admin accounts via security plugins.
  3. Limit login attempts, block brute force attacks.
  4. Hide WordPress version number from frontend source code.Add to functions.php (child theme):
remove_action('wp_head', 'wp_generator');
  1. Restrict wp-admin access by IP in Apache/Nginx if you have static IP.
  2. Keep WordPress core, themes and plugins updated. Remove unused themes/plugins completely, do not just deactivate them.

6. Database & Backup Policy​

  1. Schedule automatic full backup: database dump + all website files.
  2. Store backups off-server, not on the same web host.
  3. Use strong database user password; database user only get required privileges.
  4. 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.
If you encounter 403 errors after applying rules, cron failure or malware scan alerts, create a support thread with your webserver type and logs.
 
Trending Resources
Back
Top