Nginx – php-fpm use a lot of cpu

central-processing-unitnginxPHPphp-fpm

I use wordpress on Ubuntu 12.04 with Nginx + php-fpm on my VPS.
There are 2 CPU cores + 4096Mb memory.

I've moved mysql database to another server and set remote access.
There are about 300 online visitors at once and php-fpm uses really a lot of CPU:

enter image description here

I also use APC-cache and batcache for wordpress.

php-fpm config:

listen = /var/run/fpm-macradar.sock
;listen.backlog = -1

pm = ondemand
pm.max_children = 30
pm.start_servers = 15
pm.min_spare_servers = 10
pm.max_spare_servers = 20
;pm.process_idle_timeout = 10s;
pm.max_requests = 500

pm.status_path = /status

chdir = /

request_slowlog_timeout = 60s
slowlog = /var/log/$pool.log.slow

request_terminate_timeout = 120s
rlimit_files = 131072
rlimit_core = unlimited
catch_workers_output = yes

;php_flag[display_errors] = off
php_admin_value[error_log] = /var/log/fpm-php.www.log
php_admin_flag[log_errors] = on
php_admin_value[memory_limit] = 128M

Any help will be appreciated

Best Answer

If you have only two CPU and has 30 children in parallel, are carrying on the number of processes that your CPU supports. When there are several children trying to work, each has less CPU, and the result is that everything goes slower, plus the overhead of CPU. This is really important in case like WordPress who use a lot of CPU for each request.

Is better reduce the number of maximum to 2 or 3 children, and nginx which is responsible for managing the queue of connections, queries are not lost.

When FPM is "ondemand" you only need to define pm.max_children, in this case:

pm.max_children = 2 

I hope you find it useful.