WordPress Site Optimization Guide

Section 19: Comprehensive Server Configuration & Optimization

🚀 WordPress Site Optimization Process

This comprehensive guide walks you through optimizing your WordPress site by configuring post revisions, memory limits, cron jobs, OpCache, and implementing FastCGI caching. These optimizations will significantly improve your site's performance and resource efficiency.

📌 Prerequisites: Ensure you have completed the WordPress installation and basic hardening steps before proceeding with optimization.

📝 Step 1: Configure Post Revisions Policy

WordPress stores multiple revisions of your posts by default, which can bloat your database. We'll disable this feature to conserve database space and improve performance.

Navigate to Document Root

cd /var/www/example.com/public_html

Edit wp-config.php

sudo nano wp-config.php

Scroll down to the section that allows custom directives, typically located below the define('WP_AUTO_UPDATE_CORE', 'false'); line.

Add Post Revisions Directive

/** DISABLE POST REVISIONS */ define('WP_POST_REVISIONS', 'false');
✓ Result: WordPress will no longer create multiple revisions of your posts, reducing database size and query overhead.

💾 Step 2: Configure WordPress Memory Limit

Increasing the WordPress memory limit allows your site to handle more complex operations and plugins without running into memory errors.

Add Memory Limit Directive to wp-config.php

/** MEMORY LIMIT */ define('WP_MEMORY_LIMIT', '256M');

Update PHP-FPM Pool Configuration

cd /etc/php/8.3/fpm/pool.d/
sudo nano example.com.conf

Locate the php_admin_value[memory_limit] directive (press CTRL+W to search). Uncomment the line and change the value from 32M to 256M:

php_admin_value[memory_limit] = 256M
⚠️ Important: After modifying wp-config.php, always reload PHP-FPM to apply changes.
sudo systemctl reload php8.3-fpm

⏰ Step 3: Configure WordPress Cron

By default, WordPress cron runs on every page load, which can slow down your site. We'll disable the built-in cron and set up a real server-side cron job for better performance.

Disable Built-in WordPress Cron

Add this directive to wp-config.php (in the same section as previous directives):

/** DISABLE WP-CRON */ define('DISABLE_WP_CRON', true);

Save and close the file (CTRL+X, then Y, then Enter).

Create Server-Side Cron Job

crontab -e

Add the following line to run WordPress cron every 15 minutes:

*/15 * * * * wget -q -O - https://example.com/wp-cron.php?doing_wp_cron >/dev/null 2>&1
💡 Tip: If you have multiple sites, stagger the cron jobs by a minute to prevent simultaneous execution. For example, use */16 * * * * for the second site.

WordPress Cron Workflow

Built-in WP-Cron
(Disabled)
Server Cron
Every 15 minutes
WordPress Tasks
Executed reliably

⚡ Step 4: Configure OpCache

OpCache stores precompiled PHP scripts in memory, dramatically improving PHP execution speed. We'll configure OpCache with optimal settings for WordPress.

Edit PHP Pool Configuration

sudo nano /etc/php/8.3/fpm/pool.d/example.com.conf

Scroll to the end of the file (CTRL+END) and add the following directives:

Development Server Configuration (Recommended for Testing)

; OPCACHE CONFIGURATION - DEVELOPMENT SERVER - Jul 2025 ; Directive php_admin_flag[opcache.enabled] leave commented - enabled by default ; php_admin_flag[opcache.enabled] = 1 php_admin_value[opcache.memory_consumption] = 256 php_admin_value[opcache.interned_strings_buffer] = 32 php_admin_value[opcache.max_accelerated_files] = 20000 php_admin_flag[opcache.validate_timestamps] = 1 php_admin_value[opcache.revalidate_freq] = 2 php_admin_flag[opcache.validate_permission] = 1

Production Server Configuration (For Live Sites)

; OPCACHE CONFIGURATION - PRODUCTION SERVER - Jul 2025 ; Directive php_admin_flag[opcache.enabled] leave commented - enabled by default ;php_admin_flag[opcache.enabled] = 1 ;php_admin_value[opcache.memory_consumption] = 256 ;php_admin_value[opcache.interned_strings_buffer] = 32 ;php_admin_value[opcache.max_accelerated_files] = 20000 ;php_admin_flag[opcache.validate_timestamps] = 0 ;php_admin_flag[opcache.validate_permission] = 1
⚠️ Note: Comment out development settings and uncomment production settings when moving to production. The key difference is validate_timestamps = 0 in production for maximum performance.

Reload PHP-FPM

sudo systemctl reload php8.3-fpm
OpCache Directive Value Purpose
opcache.memory_consumption 256 MB Memory allocated for OpCache
opcache.interned_strings_buffer 32 MB Memory for storing interned strings
opcache.max_accelerated_files 20000 Maximum number of cached files
opcache.validate_timestamps 1 (Dev) / 0 (Prod) Check for file changes
opcache.revalidate_freq 2 seconds File check frequency (Dev only)

🚄 Step 5: Configure FastCGI Caching

FastCGI caching stores rendered HTML pages in memory, serving them instantly without executing PHP or querying the database. This is one of the most effective performance optimizations available.

Configure Nginx Main Configuration

cd /etc/nginx
sudo nano nginx.conf

Scroll down to the FastCGI caching section and add a new cache path for your site:

### FASTCGI CACHING # fastcgi_cache_path directive - PATH & NAME must be unique for each site fastcgi_cache_path /var/run/SITENAME levels=1:2 keys_zone=SITENAME:100m inactive=60m; fastcgi_cache_path /var/run/SITE2NAME levels=1:2 keys_zone=SITE2NAME:100m inactive=60m; # Applied to all sites fastcgi_cache_key "$scheme$request_method$host$request_uri"; fastcgi_cache_use_stale error timeout invalid_header http_500; fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
📌 Important: Replace SITENAME with a unique identifier for your site (e.g., example_com). Each site must have a unique path and keys_zone name.

Create Cache Excludes File

cd /etc/nginx/includes/

If the fastcgi_cache_excludes.conf file doesn't exist, create it with appropriate exclusion rules for WordPress admin areas, logged-in users, and dynamic content.

Configure Site-Specific Server Block

sudo nano /etc/nginx/sites-available/example.com.conf

Add FastCGI Caching to PHP Location Block

Locate the PHP processing location block and add the following directives:

location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_param HTTP_HOST $host; fastcgi_pass unix:/run/php/php8.3-fpm-example.com.sock; include /etc/nginx/includes/fastcgi_optimize.conf; # FastCGI caching directives fastcgi_cache_bypass $skip_cache; fastcgi_no_cache $skip_cache; fastcgi_cache SITENAME; fastcgi_cache_valid 60m; }

Add Cache Excludes and Headers

Above the PHP location block, add:

include /etc/nginx/includes/fastcgi_cache_excludes.conf; add_header X-FastCGI-Cache $upstream_cache_status;

Add Cache Purge Location

Add this location block to allow manual cache purging:

location ~ /purge(/.*) { fastcgi_cache_purge SITENAME "$scheme$request_method$host$1"; }
⚠️ Remember: Replace SITENAME with your actual keys_zone name from nginx.conf

Test and Reload Nginx

sudo nginx -t
sudo systemctl reload nginx

Verify FastCGI Caching

Test if caching is working properly:

curl -I https://example.com

On first request, you should see X-FastCGI-Cache: MISS

curl -I https://example.com

On subsequent requests, you should see X-FastCGI-Cache: HIT

✓ Success: When you see "HIT", your page is being served from the FastCGI cache, resulting in dramatically faster load times!

FastCGI Cache Flow

User Request
First visit
PHP Execution
Generate HTML
Cache Storage
Save to memory
Subsequent Requests
Serve from cache

☁️ Step 6: Cloudflare Integration

Cloudflare provides CDN, DDoS protection, and additional caching layers. Proper configuration ensures secure communication between Cloudflare and your server.

Server-Side Configuration

Verify Cloudflare IP List File

readlink -f /etc/nginx/includes/cloudflare_ip_list.conf

Copy the full path displayed by this command.

Update Nginx Server Block

cd /etc/nginx/sites-available/
sudo nano example.com.conf

Add the include directive for Cloudflare IPs (underneath security includes):

include /etc/nginx/includes/cloudflare_ip_list.conf;

Test and Reload

sudo nginx -t
sudo systemctl reload nginx

Cloudflare Dashboard Configuration

Step 1: Enable Proxy

  1. Log in to your Cloudflare dashboard
  2. Select your domain
  3. Navigate to DNS section
  4. Change proxy status from DNS only to Proxied for your A and AAAA records
💡 What this does: When proxied, visitor traffic flows through Cloudflare's network before reaching your server, providing DDoS protection and CDN capabilities.

Step 2: Configure SSL/TLS Settings

  1. Navigate to SSL/TLS section
  2. Set encryption mode to Full (strict)
⚠️ Critical: Always use "Full (strict)" mode. This ensures end-to-end encryption between the visitor → Cloudflare → your server.

Cloudflare Traffic Flow

Visitor
Makes request
Cloudflare
SSL encryption
(CF certificates)
Your Server
SSL encryption
(Let's Encrypt)
Response
Encrypted & cached

Step 3: Additional Cloudflare Settings

Configure the following settings in your Cloudflare dashboard:

Verify Configuration

Test Redirects

curl -I http://example.com
curl -I http://www.example.com
curl -I https://www.example.com
curl -I https://example.com

All requests should properly redirect to HTTPS with your preferred domain (with or without www).

Test SSL Configuration

Visit these testing tools to verify your SSL setup:

Verify HTTP/3 in Browser

  1. Open your browser's Developer Tools (F12)
  2. Navigate to the Network tab
  3. Refresh your page
  4. Right-click on column headers and ensure Protocol is checked
  5. Look for h3 in the Protocol column (indicates HTTP/3)
✓ Optimization Complete: Your WordPress site is now fully optimized with post revisions disabled, increased memory limits, proper cron configuration, OpCache enabled, FastCGI caching implemented, and Cloudflare integration configured!

📊 Performance Optimization Summary

Optimization Configuration Expected Impact
Post Revisions Disabled Reduced database size
Memory Limit 256MB Better plugin/theme performance
WordPress Cron Server-side (15 min) Faster page loads
OpCache 256MB / 20K files 3-5x PHP execution speed
FastCGI Cache 100MB / 60 min 10-100x page load speed
Cloudflare Proxied + Full (strict) Global CDN, DDoS protection

🔧 Additional Optimization Steps

📌 Note: The following optimization steps were covered in detail in the first site setup section:
  • Web Application Firewall (WAF) plugin configuration
  • Disabling WP REST API for non-logged-in users
  • Additional Cloudflare dashboard settings
  • Image optimization using plugins
  • Database optimization
  • CSS and JavaScript minification/concatenation
  • Disallow file modifications (DISALLOW_FILE_MODS)

🎯 Best Practices & Tips

Permission Management Scripts

Use the provided bash scripts for quick permission changes:

Loosen Permissions (for updates)

cd ~/wp_bash_scripts/ bash 5.loosen_permissions.sh

Tighten Permissions (after updates)

cd ~/wp_bash_scripts/ bash 6.tighten_permissions.sh

Monitoring & Maintenance

Check OpCache File Count

bash ~/wp_bash_scripts/opcache_files.sh

Monitor PHP-FPM Pool Usage

bash ~/wp_bash_scripts/pool_list_usage.sh

Clear FastCGI Cache

To manually clear the cache for a specific page:

curl -X PURGE https://example.com/page-url/

Security Considerations

Performance Monitoring

⚠️ Important Reminder: This guide covers server-level optimization. Additional plugin-based optimizations (image compression, CSS/JS minification, database cleanup) should be implemented according to your specific needs and are not covered in this server administration course.

🎉 Conclusion

You have successfully completed the comprehensive WordPress site optimization process. Your site now benefits from:

These optimizations work together to create a high-performance, secure WordPress hosting environment. Regular monitoring and maintenance will ensure your site continues to perform optimally.

🚀 Next Steps: For hosting additional sites, repeat this process with unique configuration values. For subdomain sites, refer to the subdomain setup section which covers WP Super Cache and Redis integration.