Server diagnose: How to fix Wordpress server CPU over 100% utilties

Based on the provided top output and MySQL logs, I can identify several potential issues:

  1. High CPU Usage Analysis:
  • MySQL (mysqld) is using 31.6% CPU
  • Multiple PHP-FPM processes are each using significant CPU (30.2%, 26.9%, 25.2%, 17.6%)
  • Total CPU usage is at 55.5% user space utilization
  • The load average (5.35, 4.89, 2.95) indicates sustained high load
  1. Process Overview:
  • You have 4 PHP-FPM worker processes consuming significant resources
  • MySQL server appears to have recently restarted (based on the logs)
  • The system has high memory usage (1276.9 MiB used out of 1949.8 MiB total)

Here are recommended solutions:

  1. Optimize PHP-FPM Configuration:
# In /etc/php/7.4/fpm/pool.d/www.conf
pm = dynamic
pm.max_children = 20
pm.start_servers = 5
pm.min_spare_servers = 3
pm.max_spare_servers = 10
pm.max_requests = 500
  1. Add MySQL Query Caching and Optimization:
# In /etc/mysql/mysql.conf.d/mysqld.cnf
innodb_buffer_pool_size = 512M
innodb_buffer_pool_instances = 1
max_connections = 150
table_open_cache = 2000
thread_cache_size = 10
  1. Implement WordPress Caching:
  • Install and configure a caching plugin like WP Super Cache or W3 Total Cache
  • Add object caching using Redis or Memcached
  1. Add to your Nginx configuration:
# In your nginx site configuration
fastcgi_cache_path /tmp/nginx-cache levels=1:2 keys_zone=WORDPRESS:100m inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
fastcgi_cache_use_stale error timeout invalid_header http_500;
fastcgi_cache_valid 200 60m;

location ~ \.php$ {
    fastcgi_cache WORDPRESS;
    fastcgi_cache_valid 200 60m;
    fastcgi_cache_bypass $skip_cache;
    fastcgi_no_cache $skip_cache;
    # ... rest of your PHP configuration
}

# Add basic rate limiting
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
location / {
    limit_req zone=one burst=10 nodelay;
    # ... rest of your configuration
}
  1. Enable monitoring:
# Install and configure MySQL slow query log
echo "slow_query_log = 1
slow_query_log_file = /var/log/mysql/mysql-slow.log
long_query_time = 2" >> /etc/mysql/mysql.conf.d/mysqld.cnf

# Monitor PHP-FPM slow logs
echo "slowlog = /var/log/php7.4-fpm.slow.log
request_slowlog_timeout = 5s" >> /etc/php/7.4/fpm/pool.d/www.conf

Additional Recommendations:

  1. Monitor for malicious activities (might be caused by bot attacks or brute force attempts)
  2. Consider implementing CloudFlare or similar CDN/WAF
  3. Regularly update WordPress, themes, and plugins
  4. Use a monitoring solution like New Relic or Datadog to track performance
  5. Consider upgrading your server resources if the issue persists after optimization

Would you like me to provide more specific details about any of these solutions or help you implement them?