php 中php-fpm工作原理
<p><span style="font-size: 16px; font-family: "Microsoft YaHei""><strong>1.<code>CGI</code></strong></span></p><p>是服务器与后台语言交互的协议,有了这个协议,开发者可以使用任何语言处理服务器转发过来的请求,动态地生成内容,</p>
<p>保证了传递过来的数据是<code>标准格式</code>的(规定了<code>以什么样的格式传哪些数据(URL、查询字符串、POST数据、HTTP header等等</code>)),方便了开发者。</p>
<p><span style="font-family: "Microsoft YaHei""><strong><span style="font-size: 16px">2.fastCGI</span></strong></span></p>
<div>
<div>首先,<code>FastCGI</code>会先启一个<code>master进程</code>,解析配置文件,初始化执行环境,然后再启动多个<code>worker进程</code>。当请求过来时,<code>master</code>会传递给一个<code>worker</code>,然后立即可以接受下一个请求。<br>
这样就避免了重复的劳动,效率自然是高。<br>
而且当<code>worker</code>不够用时,<code>master</code>可以根据配置预先启动几个<code>worker</code>等着。<br>
当然空闲<code>worker</code>太多时,也会停掉一些,这样就提高了性能,也节约了资源。这就是<code>FastCGI</code>的对进程的管理。</div>
<div> </div>
<div><span style="font-family: "Microsoft YaHei"; font-size: 15px"><strong> </strong></span></div>
<span style="font-family: "Microsoft YaHei"; font-size: 15px"><strong>
3.php-fpm</strong></span></div>
<div><code>master进程</code>只有一个,负责监听端口,接收来自服务器的请求,而<code>worker进程</code>则一般有多个(具体数量根据实际需要配置),每个进程内部都嵌入了一个<code>PHP解释器</code>,</div>
<div>是<code>PHP代码</code>真正执行的地方,下面是我本机上<code>FPM</code>的进程情况:1个<code>master进程</code>,2个<code>worker进程</code>。</div>
<div><strong>从<code>FPM</code>接收到请求,到处理完毕,其具体的流程如下</strong></div>
<div><code>1).FPM</code>的<code>master进程</code>接收到请求。</div>
<div><code>2).master进程</code>根据配置指派特定的<code>worker进程</code>进行请求处理,如果没有可用进程,返回错误,这也是我们配合<code>Nginx</code>遇到<code>502</code>错误比较多的原因。</div>
<div><code>3).worker</code>进程处理请求,如果超时,返回<code>504</code>错误。</div>
<div>
<p>4).请求处理结束,返回结果。</p>
</div>
<p><strong>4 php-fpm配置文件</strong></p>
<div class="cnblogs_code">
<pre>pid = run/php-fpm.<span style="color: rgba(0, 0, 0, 1)">pid
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">pid设置,默认在安装目录中的var/run/php-fpm.pid,建议开启</span>
<span style="color: rgba(0, 128, 128, 1)">error_log</span> = <span style="color: rgba(0, 128, 128, 1)">log</span>/php-fpm.<span style="color: rgba(0, 128, 128, 1)">log</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">错误日志,默认在安装目录中的var/log/php-fpm.log</span>
<span style="color: rgba(0, 0, 0, 1)">
log_level </span>=<span style="color: rgba(0, 0, 0, 1)"> notice
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">错误级别. 可用级别为: alert(必须立即处理), error(错误情况), warning(警告情况), notice(一般重要信息), debug(调试信息). 默认: notice.</span>
<span style="color: rgba(0, 0, 0, 1)">
emergency_restart_threshold </span>= 60<span style="color: rgba(0, 0, 0, 1)">
emergency_restart_interval </span>=<span style="color: rgba(0, 0, 0, 1)"> 60s
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">表示在emergency_restart_interval所设值内出现SIGSEGV或者SIGBUS错误的php-cgi进程数如果超过 emergency_restart_threshold个,php-fpm就会优雅重启。这两个选项一般保持默认值。</span>
<span style="color: rgba(0, 0, 0, 1)">
process_control_timeout </span>= 0
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">设置子进程接受主进程复用信号的超时时间. 可用单位: s(秒), m(分), h(小时), 或者 d(天) 默认单位: s(秒). 默认值: 0.</span>
<span style="color: rgba(0, 0, 0, 1)">
daemonize </span>=<span style="color: rgba(0, 0, 0, 1)"> yes
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">后台执行fpm,默认值为yes,如果为了调试可以改为no。在FPM中,可以使用不同的设置来运行多个进程池。 这些设置可以针对每个进程池单独设置。</span>
<span style="color: rgba(0, 0, 0, 1)">
listen </span>= 127.0.0.1:9000
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">fpm监听端口,即nginx中php处理的地址,一般默认值即可。可用格式为: ‘ip:port’, ‘port’, ‘/path/to/unix/socket’. 每个进程池都需要设置.</span>
<span style="color: rgba(0, 0, 0, 1)">
listen</span>.backlog = -1
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">backlog数,-1表示无限制,由操作系统决定,此行注释掉就行。backlog含义参考:</span>
<span style="color: rgba(0, 0, 0, 1)">
http</span>:<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">www.3gyou.cc/?p=41</span>
<span style="color: rgba(0, 0, 0, 1)">
listen</span>.allowed_clients = 127.0.0.1
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">允许访问FastCGI进程的IP,设置any为不限制IP,如果要设置其他主机的nginx也能访问这台FPM进程,listen处要设置成本地可被访问的IP。默认值是any。每个地址是用逗号分隔. 如果没有设置或者为空,则允许任何服务器请求连接</span>
<span style="color: rgba(0, 0, 0, 1)">
listen</span>.owner =<span style="color: rgba(0, 0, 0, 1)"> www
listen</span>.group =<span style="color: rgba(0, 0, 0, 1)"> www
listen</span>.mode = 0666
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">unix socket设置选项,如果使用tcp方式访问,这里注释即可。</span>
<span style="color: rgba(0, 0, 0, 1)">
user </span>=<span style="color: rgba(0, 0, 0, 1)"> www
group </span>=<span style="color: rgba(0, 0, 0, 1)"> www
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">启动进程的帐户和组</span>
<span style="color: rgba(0, 0, 0, 1)">
pm </span>= dynamic <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">对于专用服务器,pm可以设置为static。</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)">如何控制子进程,选项有static和dynamic。如果选择static,则由pm.max_children指定固定的子进程数。如果选择dynamic,则由下开参数决定:</span>
pm.max_children <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">,子进程最大数</span>
pm.start_servers <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">,启动时的进程数</span>
pm.min_spare_servers <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">,保证空闲进程数最小值,如果空闲进程小于此值,则创建新的子进程</span>
pm.max_spare_servers <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">,保证空闲进程数最大值,如果空闲进程大于此值,此进行清理</span>
<span style="color: rgba(0, 0, 0, 1)">
pm</span>.max_requests = 1000
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">设置每个子进程重生之前服务的请求数. 对于可能存在内存泄漏的第三方模块来说是非常有用的. 如果设置为 ’0′ 则一直接受请求. 等同于 PHP_FCGI_MAX_REQUESTS 环境变量. 默认值: 0.</span>
<span style="color: rgba(0, 0, 0, 1)">
pm</span>.status_path = /<span style="color: rgba(0, 0, 0, 1)">status
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">FPM状态页面的网址. 如果没有设置, 则无法访问状态页面. 默认值: none. munin监控会使用到</span>
<span style="color: rgba(0, 0, 0, 1)">
ping</span>.path = /<span style="color: rgba(0, 0, 0, 1)">ping
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">FPM监控页面的ping网址. 如果没有设置, 则无法访问ping页面. 该页面用于外部检测FPM是否存活并且可以响应请求. 请注意必须以斜线开头 (/)。</span>
<span style="color: rgba(0, 0, 0, 1)">
ping</span>.response =<span style="color: rgba(0, 0, 0, 1)"> pong
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">用于定义ping请求的返回相应. 返回为 HTTP 200 的 text/plain 格式文本. 默认值: pong.</span>
<span style="color: rgba(0, 0, 0, 1)">
request_terminate_timeout </span>= 0
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">设置单个请求的超时中止时间. 该选项可能会对php.ini设置中的’max_execution_time’因为某些特殊原因没有中止运行的脚本有用. 设置为 ’0′ 表示 ‘Off’.当经常出现502错误时可以尝试更改此选项。</span>
<span style="color: rgba(0, 0, 0, 1)">
request_slowlog_timeout </span>=<span style="color: rgba(0, 0, 0, 1)"> 10s
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">当一个请求该设置的超时时间后,就会将对应的PHP调用堆栈信息完整写入到慢日志中. 设置为 ’0′ 表示 ‘Off’</span>
<span style="color: rgba(0, 0, 0, 1)">
slowlog </span>= <span style="color: rgba(0, 128, 128, 1)">log</span>/<span style="color: rgba(128, 0, 128, 1)">$pool</span>.<span style="color: rgba(0, 128, 128, 1)">log</span>.<span style="color: rgba(0, 0, 0, 1)">slow
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">慢请求的记录日志,配合request_slowlog_timeout使用</span>
<span style="color: rgba(0, 0, 0, 1)">
rlimit_files </span>= 1024
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">设置文件打开描述符的rlimit限制. 默认值: 系统定义值默认可打开句柄是1024,可使用 ulimit -n查看,ulimit -n 2048修改。</span>
<span style="color: rgba(0, 0, 0, 1)">
rlimit_core </span>= 0
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">设置核心rlimit最大限制值. 可用值: ‘unlimited’ 、0或者正整数. 默认值: 系统定义值.</span>
<span style="color: rgba(0, 0, 0, 1)">
chroot </span>=
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">启动时的Chroot目录. 所定义的目录需要是绝对路径. 如果没有设置, 则chroot不被使用.</span>
<span style="color: rgba(0, 128, 128, 1)">chdir</span> =
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">设置启动目录,启动时会自动Chdir到该目录. 所定义的目录需要是绝对路径. 默认值: 当前目录,或者/目录(chroot时)</span>
<span style="color: rgba(0, 0, 0, 1)">
catch_workers_output </span>=<span style="color: rgba(0, 0, 0, 1)"> yes
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">重定向运行过程中的stdout和stderr到主要的错误日志文件中. 如果没有设置, stdout 和 stderr 将会根据FastCGI的规则被重定向到 /dev/null . 默认值: 空.</span></pre>
</div>
<p> </p>
<p>参考文件:</p>
<p>https://www.jianshu.com/p/da152c6fdfa6</p>
<p>https://www.cnblogs.com/ahaii/p/5776809.html</p><br><br>
来源:https://www.cnblogs.com/myvic/p/11171868.html
頁:
[1]