Set the number of php-fpm process

Recently, the server is abnormal, the site can not be accessed properly. Php by the investigation is the question.
When restart php-fpm, returned to normal. After one minute and failure. Check php log file / usr / local / php after the / var / log Tips

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

The child has reached the maximum number set.

To set the number of php process. We need to modify the php-fpm.conf file.

/Usr/local/php/etc/php-fpm.conf look at the configuration file parsing

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 . 默认值: 空.

Based on the analysis of the above configuration, the following configuration is added php-fpm.conf file:

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

php-fpm optimization parameters introduced:
They are: pm, pm.max_children, pm.start_servers, pm.min_spare_servers , pm.max_spare_servers.

pm: indication that way, there are two values can be selected, it is static (static) or dynamic (dynamic).
In some of the older versions, dynamic is called apache-like. This should pay attention to see the description for the profile.

Means the following four parameters were:

pm.max_children:静态方式下开启的php-fpm进程数量
pm.start_servers:动态方式下的起始php-fpm进程数量
pm.min_spare_servers:动态方式下的最小php-fpm进程数
pm.max_spare_servers:动态方式下的最大php-fpm进程数量

the difference:

If dm is set to static, then there is only pm.max_children this parameter to take effect. The system will open set the number of php-fpm process.
If dm is set to dynamic, then pm.max_children parametric failure, followed by three parameters to take effect.
The system will start pm.start_servers a php-fpm process when php-fpm running start, and
then dynamically adjusted php-fpm between pm.min_spare_servers number of processes and systems according to the needs of pm.max_spare_servers

2, server specific configuration
for our servers, which perform the mode selection squatters? In fact, as with Apache, PHP programs running after execution is completed, there will be more or less memory leak problem.
This is why at the beginning of a php-fpm process only takes about 3M memory, the reason 20-30M after running for some time to rise to the.
For large memory servers (such as 8G above), the specified static max_children actually be more appropriate, because it does not require additional number of process control, will improve efficiency.
Because frequent switching php-fpm process there will be a time lag, so the effect will be better under static memory large enough to open the case. Number may also be based on the memory / 30M obtained, for example 8GB of memory can be set to 100,
then php-fpm consuming memory can be controlled in way of 2G-3G. If the memory is slightly smaller, such as 1G, then specify the number of static process more conducive to the stability of the server.
This ensures that php-fpm get only enough memory, the memory allocated to the few other applications to use, will make the system run more smooth.
For small memory servers, such as 256M memory of VPS, even if the amount of memory in accordance with a 20M to count, 10 php-cgi process will consume 200M memory, the collapse of that system should be normal.
So we should try to control the number of php-fpm process, generally clear after the memory occupied by other applications, assign it a static small number will make the system more stable number. Or use a dynamic way,
because the dynamic mode will end off the excess processes, free up some memory can be recovered, it is recommended to use less memory on the server or VPS. DETAILED obtained according to the maximum number of memory / 20M.
For example, 512M of VPS, recommendations pm.max_spare_servers to 20. As pm.min_spare_servers, it is recommended that the server is set according to the load, such as the deployment server only if php environment, suitable values ​​between 5-10.

This server configuration

1, basic information of the server:
a hard disk: a data disk 30G, 20G system disk
memory: 1.5G
the CPU: dual core
system: CentOS 6.3 64 bit
Bandwidth: exclusive 2M
2, application deployment
Git, SVN, Apache, Tomcat, PHP, Nginx, Mysql, JDK
parameters 3, optimization

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 each child process before the rebirth of services for third-party modules there may be a memory leak is very useful if set to '0' is equivalent to the request has been accepted PHP_FCGI_MAX_REQUESTS environment variable Default: 0.
This section configuration means that when the number of requests a PHP-CGI process 500 to process the accumulated automatically restart the process.

But why should restart the process?

Generally in the project, we more or less will use some third-party libraries for PHP, these third-party libraries are often a memory leak problem, if from time to time to restart PHP-CGI process will inevitably result in memory usage continues to grow. So PHP-FPM as PHP-CGI manager, providing such a monitoring function, the request for the specified number of PHP-CGI process restart, ensure that the memory usage does not increase.

It is because of this mechanism, in highly concurrent sites, often leads to a 502 error, I guess because PHP-FPM request queue to come from NGINX did not handle. But still I am currently using PHP 5.3.2, I do not know whether the problem still exists in PHP 5.3.3 in.

At present, our solution is to try to set this value larger, as much as possible to reduce the number of PHP-CGI re-SPAWN, but also can improve overall performance. In our own actual production found in the environment, a memory leak is not obvious, so we set this value very large (204800). We have to set this value according to their actual situation, not blindly increase.

Guess you like

Origin www.cnblogs.com/yinguohai/p/12409000.html