Join the community today
Become a Member

PHP PHP-FPM tuning: Using ‘pm static’ for max performance

Discussion in 'Nginx and PHP-FPM news & discussions' started by rdan, Jan 27, 2019.

  1. rdan

    rdan Well-Known Member

    4,667
    1,121
    113
    May 25, 2014
    Ratings:
    +1,663
    Local Time:
    12:36 AM
    Mainline
    10.2
    PHP-FPM tuning: Using 'pm static' for max performance

    When it comes to PHP-FPM, once you start to serve serious traffic, ondemand and dynamic process managers for PHP-FPM can limit throughput because of the inherent overhead. Know your system and set your PHP-FPM processes to match your server’s max capacity. Start with pm.max_children set based on max usage of pm dynamic or ondemand and then increase to the point where memory and CPU can process without becoming overwhelmed. You will notice that with pm static, because you keep everything sitting in memory, traffic spikes over time cause less spikes to CPU and your server’s load and CPU averages will be smoother. The average size of your PHP-FPM process will vary per web server requiring manual tuning, thus why the more automated overhead process managers – dynamicand ondemand – are more popular recommendations.
     
  2. eva2000

    eva2000 Administrator Staff Member

    41,645
    9,380
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +14,408
    Local Time:
    2:36 AM
    Nginx 1.17.x
    MariaDB 5.5/10.x
    Yes it's well known that pm = static is best for performance. It's what I use myself. But it will use the most memory as all PHP-FPM processes are in memory even if not used unlike ondemand or dynamic. Hence why Centmin Mod defaults to pm = ondemand in PHP-FPM config file at /usr/local/etc/php-fpm.conf :)
     
  3. rdan

    rdan Well-Known Member

    4,667
    1,121
    113
    May 25, 2014
    Ratings:
    +1,663
    Local Time:
    12:36 AM
    Mainline
    10.2
    Just like Nginx use static workers :).
     
    • Agree Agree x 1
  4. eva2000

    eva2000 Administrator Staff Member

    41,645
    9,380
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +14,408
    Local Time:
    2:36 AM
    Nginx 1.17.x
    MariaDB 5.5/10.x
    with PHP-FPM systemd service file in place see diff between pm = dynamic vs pm = static

    For Centmin Mod 123.09beta01's defaults set in /usr/local/etc/php-fpm.conf for a 2 cpu, 4GB KVM VPS on CentOS 7
    Code (Text):
    ;pm = ondemand
    ;pm = dynamic
    pm = static
    pm.max_children = 20
    ; Default Value: min_spare_servers + (max_spare_servers - min_spare_servers) / 2
    pm.start_servers = 8
    pm.min_spare_servers = 4
    pm.max_spare_servers = 12
    pm.max_requests = 5000
    

    pm = dynamic has pm.start_servers = 8
    Code (Text):
    fpmstatus
    Redirecting to /bin/systemctl status php-fpm.service
    ● php-fpm.service - PHP FastCGI Process Manager
       Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; enabled; vendor preset: disabled)
      Drop-In: /etc/systemd/system/php-fpm.service.d
               └─limit.conf
       Active: active (running) since Sat 2019-01-26 18:20:54 UTC; 13h ago
     Main PID: 1987 (php-fpm)
       Status: "Processes active: 0, idle: 8, Requests: 11, slow: 0, Traffic: 0req/sec"
       CGroup: /system.slice/php-fpm.service
               ├─1987 php-fpm: master process (/usr/local/etc/php-fpm.conf)
               ├─1988 php-fpm: pool www
               ├─1989 php-fpm: pool www
               ├─1990 php-fpm: pool www
               ├─1991 php-fpm: pool www
               ├─1994 php-fpm: pool www
               ├─1995 php-fpm: pool www
               ├─1996 php-fpm: pool www
               └─1997 php-fpm: pool www
    
    Jan 26 18:20:54 test.com systemd[1]: Starting PHP FastCGI Process Manager...
    Jan 26 18:20:54 test.com systemd[1]: php-fpm.service: Supervising process 1987 which is not our child. We'll most likely not notice when it exits.
    Jan 26 18:20:54 test.com systemd[1]: Started PHP FastCGI Process Manager.
    

    pm = static only looks to pm.max_children = 20
    Code (Text):
    fpmstatus 
    Redirecting to /bin/systemctl status php-fpm.service
    ● php-fpm.service - PHP FastCGI Process Manager
       Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; enabled; vendor preset: disabled)
      Drop-In: /etc/systemd/system/php-fpm.service.d
               └─limit.conf
       Active: active (running) since Sun 2019-01-27 08:04:03 UTC; 2s ago
     Main PID: 15219 (php-fpm)
       Status: "Ready to handle connections"
       CGroup: /system.slice/php-fpm.service
               ├─15219 php-fpm: master process (/usr/local/etc/php-fpm.conf)
               ├─15220 php-fpm: pool www
               ├─15221 php-fpm: pool www
               ├─15222 php-fpm: pool www
               ├─15223 php-fpm: pool www
               ├─15224 php-fpm: pool www
               ├─15225 php-fpm: pool www
               ├─15226 php-fpm: pool www
               ├─15227 php-fpm: pool www
               ├─15228 php-fpm: pool www
               ├─15229 php-fpm: pool www
               ├─15230 php-fpm: pool www
               ├─15231 php-fpm: pool www
               ├─15232 php-fpm: pool www
               ├─15233 php-fpm: pool www
               ├─15234 php-fpm: pool www
               ├─15235 php-fpm: pool www
               ├─15236 php-fpm: pool www
               ├─15237 php-fpm: pool www
               ├─15238 php-fpm: pool www
               └─15239 php-fpm: pool www
    
    Jan 27 08:04:03 test.com systemd[1]: Starting PHP FastCGI Process Manager...
    Jan 27 08:04:03 test.com systemd[1]: php-fpm.service: Supervising process 15219 which is not our child. We'll most likely not notice when it exits.
    Jan 27 08:04:03 test.com systemd[1]: Started PHP FastCGI Process Manager.
    
     
    • Like Like x 1
  5. rdan

    rdan Well-Known Member

    4,667
    1,121
    113
    May 25, 2014
    Ratings:
    +1,663
    Local Time:
    12:36 AM
    Mainline
    10.2
    After using Static for almost a month I decided to switch back to dynamic as it uses a lot of CPU.
    Static: usually 30-50% of the total CPU
    Dynamic: only 10-15% CPU usage and 5-7% lower Memory usage

    With almost the same site speed.
     
    Last edited: Feb 24, 2019
  6. eva2000

    eva2000 Administrator Staff Member

    41,645
    9,380
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +14,408
    Local Time:
    2:36 AM
    Nginx 1.17.x
    MariaDB 5.5/10.x
    Yeah because static leaves PHP-FPM processes running even if idle and can have higher peaks due to handling more PHP work through more static PHP-FPM processes/children.