Home  >  Article  >  Backend Development  >  How to modify the number of php fpm processes

How to modify the number of php fpm processes

藏色散人
藏色散人Original
2021-09-19 11:01:383922browse

Method to modify the number of php fpm processes: 1. View the php log file "/usr/local/php/var/log"; 2. Open the "php-fpm.conf" file; 3. Add "pm .max_spare_servers" and other parameters to set the maximum number of php-fpm processes.

How to modify the number of php fpm processes

The operating environment of this article: Windows 7 system, PHP5.3, Dell G3 computer.

How to modify the number of php fpm processes?

Setting the number of PHP-FPM processes

Recently, the server has experienced an abnormality and the website cannot be accessed normally. After troubleshooting, it was a problem with php.

When restarting php-fpm, it returns to normal. Another failure occurred after 1 minute. Check the php log file /usr/local/php/var/log and it will prompt

WARNING: [pool www] server reached pm.max_children setting (5), consider raising it

The number of child processes has reached the maximum value set.

To set the number of php processes. Needs to be modified in the php-fpm.conf file.

First look at the configuration analysis of the /usr/local/php/etc/php-fpm.conf file

pid = run/php-fpm.pid
#pid设置,默认在安装目录中的var/run/php-fpm.pid,建议开启
error_log = log/php-fpm.log
#错误日志,默认在安装目录中的var/log/php-fpm.log
log_level = notice
#错误级别. 可用级别为: alert(必须立即处理), error(错误情况), warning(警告情况), notice(一般重要信息), debug(调试信息). 默认: notice.
emergency_restart_threshold = 60
emergency_restart_interval = 60s
#表示在emergency_restart_interval所设值内出现SIGSEGV或者SIGBUS错误的php-cgi进程数如果超过 emergency_restart_threshold个,php-fpm就会优雅重启。这两个选项一般保持默认值。
process_control_timeout = 0
#设置子进程接受主进程复用信号的超时时间. 可用单位: s(秒), m(分), h(小时), 或者 d(天) 默认单位: s(秒). 默认值: 0.
daemonize = yes
#后台执行fpm,默认值为yes,如果为了调试可以改为no。在FPM中,可以使用不同的设置来运行多个进程池。 这些设置可以针对每个进程池单独设置。
listen = 127.0.0.1:9000
#fpm监听端口,即nginx中php处理的地址,一般默认值即可。可用格式为: ‘ip:port’, ‘port’, ‘/path/to/unix/socket’. 每个进程池都需要设置.
listen.backlog = -1
#backlog数,-1表示无限制,由操作系统决定,此行注释掉就行。backlog含义参考:
http://www.3gyou.cc/?p=41
listen.allowed_clients = 127.0.0.1
#允许访问FastCGI进程的IP,设置any为不限制IP,如果要设置其他主机的nginx也能访问这台FPM进程,listen处要设置成本地可被访问的IP。默认值是any。每个地址是用逗号分隔. 如果没有设置或者为空,则允许任何服务器请求连接
listen.owner = www
listen.group = www
listen.mode = 0666
#unix socket设置选项,如果使用tcp方式访问,这里注释即可。
user = www
group = www
#启动进程的帐户和组
pm = dynamic #对于专用服务器,pm可以设置为static。
#如何控制子进程,选项有static和dynamic。如果选择static,则由pm.max_children指定固定的子进程数。如果选择dynamic,则由下开参数决定:
pm.max_children #,子进程最大数
pm.start_servers #,启动时的进程数
pm.min_spare_servers #,保证空闲进程数最小值,如果空闲进程小于此值,则创建新的子进程
pm.max_spare_servers #,保证空闲进程数最大值,如果空闲进程大于此值,此进行清理
pm.max_requests = 1000
#设置每个子进程重生之前服务的请求数. 对于可能存在内存泄漏的第三方模块来说是非常有用的. 如果设置为 ’0′ 则一直接受请求. 等同于 PHP_FCGI_MAX_REQUESTS 环境变量. 默认值: 0.
pm.status_path = /status
#FPM状态页面的网址. 如果没有设置, 则无法访问状态页面. 默认值: none. munin监控会使用到
ping.path = /ping
#FPM监控页面的ping网址. 如果没有设置, 则无法访问ping页面. 该页面用于外部检测FPM是否存活并且可以响应请求. 请注意必须以斜线开头 (/)。
ping.response = pong
#用于定义ping请求的返回相应. 返回为 HTTP 200 的 text/plain 格式文本. 默认值: pong.
request_terminate_timeout = 0
#设置单个请求的超时中止时间. 该选项可能会对php.ini设置中的’max_execution_time’因为某些特殊原因没有中止运行的脚本有用. 设置为 ’0′ 表示 ‘Off’.当经常出现502错误时可以尝试更改此选项。
request_slowlog_timeout = 10s
#当一个请求该设置的超时时间后,就会将对应的PHP调用堆栈信息完整写入到慢日志中. 设置为 ’0′ 表示 ‘Off’
slowlog = log/$pool.log.slow
#慢请求的记录日志,配合request_slowlog_timeout使用
rlimit_files = 1024
#设置文件打开描述符的rlimit限制. 默认值: 系统定义值默认可打开句柄是1024,可使用 ulimit -n查看,ulimit -n 2048修改。
rlimit_core = 0
#设置核心rlimit最大限制值. 可用值: ‘unlimited’ 、0或者正整数. 默认值: 系统定义值.
chroot =
#启动时的Chroot目录. 所定义的目录需要是绝对路径. 如果没有设置, 则chroot不被使用.
chdir =
#设置启动目录,启动时会自动Chdir到该目录. 所定义的目录需要是绝对路径. 默认值: 当前目录,或者/目录(chroot时)
catch_workers_output = yes
#重定向运行过程中的stdout和stderr到主要的错误日志文件中. 如果没有设置, stdout 和 stderr 将会根据FastCGI的规则被重定向到 /dev/null . 默认值: 空.

According to the analysis of the above configuration, in php-fpm Add the following configuration to the .conf file:

pm.max_children = 100
pm.start_servers = 30
pm.min_spare_servers = 20
pm.max_spare_servers = 100
pm.max_requests = 500

1. Introduction to php-fpm optimization parameters

They are: pm, pm.max_children, pm.start_servers, pm .min_spare_servers, pm.max_spare_servers.

pm: Indicates which method to use. There are two values ​​to choose from, namely static (static) or dynamic (dynamic).

In older versions, dynamic was called apache-like. Please pay attention to the description of the configuration file for this.

The meanings of the following four parameters are:

pm.max_children: the number of php-fpm processes started in static mode

pm.start_servers: the number of php-fpm processes started in dynamic mode Number of initial php-fpm processes

pm.min_spare_servers: The minimum number of php-fpm processes in dynamic mode

pm.max_spare_servers: The maximum number of php-fpm processes in dynamic mode

Difference:

If dm is set to static, then only the pm.max_children parameter will take effect. The system will start a set number of php-fpm processes.

If dm is set to dynamic, then the pm.max_children parameter will be invalid and the next three parameters will take effect.

The system will start pm.start_servers php-fpm processes when php-fpm starts running.

Then dynamically adjust php between pm.min_spare_servers and pm.max_spare_servers according to the needs of the system. -Number of fpm processes

2. Server specific configuration

Which execution method is better for our server? In fact, like Apache, the running PHP program will more or less have memory leaks after execution.

This is also the reason why a php-fpm process only occupies about 3M of memory at the beginning, and it will increase to 20-30M after running for a period of time.

For servers with large memory (such as 8G or more), it is actually more appropriate to specify static max_children, because this does not require additional process number control and will improve efficiency.

Because frequent switching of the php-fpm process will cause lag, so if the memory is large enough, the static effect will be better. The quantity can also be obtained based on memory/30M. For example, 8GB memory can be set to 100,

Then the memory consumed by php-fpm can be controlled at 2G-3G. If the memory is slightly smaller, such as 1G, then specifying a static number of processes is more conducive to the stability of the server.

This can ensure that php-fpm only obtains enough memory, and allocates a small amount of memory to other applications for use, which will make the system run more smoothly.

For a server with small memory, such as a VPS with 256M memory, even if it is calculated based on a 20M memory, 10 php-cgi processes will consume 200M of memory, and the system crash should be very serious. Normal.

Therefore, we should try to control the number of php-fpm processes as much as possible. After roughly clarifying the memory occupied by other applications, assigning it a static small number will make the system more stable. Or use the dynamic method.

Because the dynamic method will end redundant processes and can recycle and release some memory, it is recommended to use it on servers or VPS with less memory. The specific maximum amount is obtained based on memory/20M.

For example, for a 512M VPS, it is recommended to set pm.max_spare_servers to 20. As for pm.min_spare_servers, it is recommended to set it according to the load of the server. For example, if only the PHP environment is deployed on the server, the more appropriate value is between 5 and 10.

Recommended study: "PHP Video Tutorial"

This server configuration

1. Basic server information:

Hard disk: data Disk 30G, system disk 20G

Memory: 1.5G

CPU: Dual-core

System: CentOS 6.3 64-bit

Bandwidth: Exclusive 2M

2. Deployed application

Git、SVN、Apache、Tomcat、PHP、Nginx、Mysql、JDK

3. Optimized parameters

pm = dynamic
pm.start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 8
 
pm.max_requests = 500

Set the number of requests served before each child process is reborn . Very useful for third-party modules that may have memory leaks. If set to '0', requests will always be accepted. Equivalent to the PHP_FCGI_MAX_REQUESTS environment variable. Default value: 0.

The meaning of this configuration Yes, when the number of requests processed by a PHP-CGI process reaches 500, the process will be automatically restarted.

But why do we need to restart the process?

Generally in projects, we will use some third-party libraries of PHP to some extent. These third-party libraries often have memory leak problems. If the PHP-CGI process is not restarted regularly, the memory usage will inevitably increase. Therefore, PHP-FPM, as the manager of PHP-CGI, provides such a monitoring function to restart the PHP-CGI process that has requested a specified number of times to ensure that the memory usage does not increase.

It is precisely because of this mechanism that 502 errors are often caused in highly concurrent sites. I guess the reason is that PHP-FPM does not handle the request queue coming from NGINX well. However, I am still using PHP 5.3.2. I don’t know if this problem still exists in PHP 5.3.3.

Our current solution is to set this value as large as possible to reduce the number of times PHP-CGI re-SPAWNs as much as possible, and at the same time improve overall performance. In our own actual production environment, we found that the memory leak was not obvious, so we set this value very large (204800). Everyone should set this value according to their actual situation and cannot blindly increase it.

The above is the detailed content of How to modify the number of php fpm processes. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn