Explanation of Configuration and Optimization of PHP FPM

  • 2021-11-29 23:16:16
  • OfStack

PHP-FPM (FastCGI Process Manager: FastCGI Process Manager) is an PHPFastCGI manager that manages the PHP process pool and receives and processes requests from the Web server. PHP-FPM creates a main process that controls when and how HTTP requests are forwarded to one or more child processes for processing.

Global configuration


emergency_restart_threshold = 60 (Recommended value) 

If the child process receives the SIGSEGV or SIGBUS exit message number of the parameter set times within the time set by emergency_restart_interval, FPM will be restarted.


emergency_restart_interval = 1m

Used to set the smooth restart interval in s (seconds), m (minutes), h (hours), or d (days). Default unit: s (seconds).

The purpose of these two settings is to cause PHP-FPM to restart if the specified child process fails within the specified period of time.

These two settings are turned off by default, so it is recommended to turn them on.

Process pool configuration

There is one Pool Definitions region in PHP-FPM, which means process pool. It is generally recommended to use one process pool for one PHP application.

The PHP-FPM configuration file has configuration content named www by default.

user = 运行 PHP 的非 root 用户

The user whose child process is running. Must be set.

group = 运行 PHP 的非 root 用户所属的用户组

The user group that the child process runs. If it is not set, the group of default users is used.

listen = 127.0.0.1:9000 Or /path/to/unix/socket

Set the address to accept FastCGI requests.

listen.allowed_clients = 127.0.0.1

Set the server IP address that is allowed to connect to FastCGI. By default, this field is commented out, so it is recommended to open it.

pm = 生产环境 static

Sets how the process manager manages child processes. Available values: static, ondemand, dynamic.

static: The number of child processes is fixed (pm.max_children). ondemand: The process is generated when required (when requested, as opposed to dynamic, pm. start_servers starts when the service starts. dynamic: The number of child processes is dynamically set based on the following configuration: pm.max_children, pm.start_servers, pm.min_spare_servers, pm.max_spare_servers.

pm.max_children =

Set the maximum number of processes in the PHP-FPM process pool. There is no absolute correct value for this, so it should be set according to the actual situation.

Assuming that each PHP process uses 20-30M memory and 512 M memory is configured for the entire PHP-FPM environment, it can be set to an intermediate value of 17-25.

pm.start_servers =

Sets the number of child processes created at startup. Use only when pm is set to dynamic.

pm.min_spare_servers =

Set the minimum number of idle service processes. Used only when pm is set to dynamic. Must be set.

pm.max_spare_servers =

Sets the maximum number of idle service processes. Used only when pm is set to dynamic. Must be set.

group = 运行 PHP 的非 root 用户所属的用户组0

Sets the number of requests served before each child process is reborn. It is very useful for third-party modules that may have memory leaks. If set to '0', 1 is directly requested, and the default value is 0.

slowlog =

Logging of slow requests.

Note that the users and user groups to which the PHP-FPM process belongs must have write access to the appropriate file.

request_slowlog_timeout = 5

After 1 timeout for requesting this setting, the corresponding PHP call stack information will be completely written to the slow log.

Summarize


Related articles: