php-fpm use a lot of cpu

12,418

Solution 1

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.

Solution 2

This is an old question, but it was bumped today by someone editing the post.

While installing a Wordpress caching plugin can be effective, it still means PHP is executed, which is relatively slow. If some of your website visitors are anonymous (ie not logged in) you can do page caching (fastcgi_cache) in Nginx that will avoid calling PHP at all, which dramatically lowers CPU usage and improves response times. The page can be cached for as little as 1 second to be valuable, or if the site is quite static you can take that out to a day or more. The tricky part is clearing the cache when you publish changes, but I have a solution to that - see below.

Have a read of the tutorial I wrote on LEMP. This first part gives you config files to download, they're well commented and the later parts of the tutorial explain them. There's also a great article by Nginx on microcaching.

Share:
12,418

Related videos on Youtube

Andrei Nikolaev
Author by

Andrei Nikolaev

Updated on September 18, 2022

Comments

  • Andrei Nikolaev
    Andrei Nikolaev over 1 year

    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

    • Michael Hampton
      Michael Hampton almost 10 years
      You need more CPU, and to do some page caching (e.g. WP Super Cache).
  • Arie Skliarouk
    Arie Skliarouk almost 9 years
    You should use more children if your php do external requests (mysql, redis, etc..) and are not using CPU.