PHP-FPM pm.max children

From CS Wiki

pm.max_children is a directive in PHP-FPM (FastCGI Process Manager) configuration that specifies the maximum number of child processes that can be created to handle incoming requests. This setting is critical for managing server resources and ensuring that PHP-FPM can efficiently handle concurrent traffic without overloading the server.

Overview[edit | edit source]

  • The `pm.max_children` directive determines the upper limit on the number of simultaneous PHP-FPM worker processes.
  • When the number of incoming requests exceeds `pm.max_children`, additional requests are queued until a process becomes available.
  • This setting is applicable for all process management modes (`dynamic`, `static`, and `ondemand`).

Syntax[edit | edit source]

The directive is configured in the PHP-FPM pool configuration file (e.g., `www.conf`).

pm = dynamic
pm.max_children = 50

How It Works[edit | edit source]

  • In `dynamic` mode:
    • Child processes are created as needed, but the total number of processes cannot exceed `pm.max_children`.
  • In `static` mode:
    • The server always creates exactly `pm.max_children` processes.
  • In `ondemand` mode:
    • Processes are spawned only when requests arrive, but the total number of processes is still limited by `pm.max_children`.

Configuration Example[edit | edit source]

Here’s an example configuration for a PHP-FPM pool:

[www]
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 10

Explanation[edit | edit source]

  • pm = dynamic: Enables dynamic process management mode.
  • pm.max_children = 50: Limits the total number of worker processes to 50.
  • pm.start_servers = 5: Starts with 5 worker processes when PHP-FPM initializes.
  • pm.min_spare_servers = 2: Maintains at least 2 idle processes.
  • pm.max_spare_servers = 10: Limits the number of idle processes to 10.

Key Considerations[edit | edit source]

  • Server Resources: Setting `pm.max_children` too high can lead to excessive memory and CPU usage, potentially causing server instability.
  • Application Demand: The value should be high enough to handle peak traffic without excessive queuing or delays.
  • Queue Length: When the number of requests exceeds `pm.max_children`, requests are queued. Monitor the queue length to determine if the value needs adjustment.

How to Calculate pm.max_children[edit | edit source]

To calculate the optimal value for `pm.max_children`, consider the following:

  • Available Memory: Ensure that the total memory usage of PHP-FPM processes does not exceed the server's physical memory.
  • Memory Usage Per Process: Monitor the average memory usage of each PHP-FPM process using tools like `top` or `htop`.

Formula[edit | edit source]

pm.max_children = (Total RAM - Other Services Memory) / Average PHP-FPM Process Memory

Monitoring and Troubleshooting[edit | edit source]

Use the `php-fpm` status page or tools like `htop` to monitor:

  • The number of active processes.
  • Memory and CPU usage.
  • Request queues, which indicate whether `pm.max_children` needs adjustment.

Example Issues[edit | edit source]

  • 502 Bad Gateway Errors:
    • Cause: Requests are queued due to insufficient child processes.
    • Solution: Increase `pm.max_children` or optimize the application to reduce resource usage.
  • High Memory Usage:
    • Cause: `pm.max_children` is set too high for the server's available memory.
    • Solution: Reduce `pm.max_children` and ensure enough resources are available.

Related Directives[edit | edit source]

  • pm.start_servers: Defines the number of worker processes created on startup.
  • pm.min_spare_servers: Sets the minimum number of idle worker processes.
  • pm.max_spare_servers: Limits the maximum number of idle worker processes.
  • pm.process_idle_timeout: Specifies the timeout for idle processes in `ondemand` mode.

See Also[edit | edit source]