DEV Community

johanputra
johanputra

Posted on

Performance Tuning PHP

PHP-FPM Pool Configuration (www.conf) - Performance Tuning

This document outlines the key parameters within a PHP-FPM pool configuration, essential for tuning PHP performance and managing server resources effectively.

Parameter Explanations

pm = dynamic

This parameter defines the process manager mode. dynamic mode allows PHP-FPM to automatically adjust the number of worker processes based on server load.

  • static: A fixed number of processes is used, as defined by pm.max_children.
  • dynamic: Processes scale up or down between min_spare_servers and max_children.
  • ondemand: Processes are created only when a request arrives and are terminated after a period of inactivity.

The dynamic mode, which you are using, requires additional parameters for configuration.

pm.max_children = 1000

This is the maximum number of simultaneous PHP worker processes. A high value like 1000 poses a significant risk of memory exhaustion (Out of Memory errors) and server crashes if not appropriately calculated.

Calculation:

  • To estimate the necessary RAM, multiply the number of processes by the average memory usage per process.
  • For example, 1000 processes × 100MB/process would require approximately 100GB of RAM.
  • Recommendation: This value should be carefully calculated based on your server's available RAM, leaving some overhead for the operating system and other services.

pm.start_servers = 80

The number of worker processes launched when the PHP-FPM service starts. This pre-spawns workers to handle an initial high load immediately.

pm.min_spare_servers = 40

The minimum number of idle worker processes kept running. Maintaining a minimum pool of spare servers reduces the time it takes to serve new requests.

pm.max_spare_servers = 120

The maximum number of idle worker processes. If the number of idle processes exceeds this value, PHP-FPM will terminate them to free up resources.

request_terminate_timeout = 300s

This parameter defines the maximum execution time for a single request. If a request exceeds 300 seconds (5 minutes), the process is automatically terminated. This is a critical setting for preventing hung or slow-running scripts from consuming resources indefinitely.

request_slowlog_timeout = 5s

Any script that takes longer than 5 seconds to execute will be logged for analysis.

slowlog = /var/log/php-fpm/slowlog.log

The file path where slow-executing scripts are logged. This log is essential for debugging application performance bottlenecks.


Best Practices and Recommendations

A configuration with pm.max_children = 1000 is typically unsuitable for servers with less than 100GB of RAM. A more balanced and safer configuration should be based on your server's hardware specifications.

Example Configuration for an 8GB RAM Server

pm = dynamic
pm.max_children = 50
pm.start_servers = 10
pm.min_spare_servers = 5
pm.max_spare_servers = 20

request_terminate_timeout = 300s
request_slowlog_timeout = 5s
slowlog = /var/log/php-fpm/slowlog.log
Enter fullscreen mode Exit fullscreen mode

Note: Using 80% of available RAM reserves resources for the OS and other critical services.

Top comments (0)