nqkk58 发表于 2024-10-4 14:27:52

服务器程序源代码分析之二:php-fpm


    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">php<span style="color: black;">做为</span>排名top2 互联网<span style="color: black;">研发</span>工具,非常流行,<span style="color: black;">能够</span>参考:中国最大的25个网站采用技术选型<span style="color: black;">方法</span></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">php这个名<span style="color: black;">叫作</span><span style="color: black;">实质</span>上有两层含义</span></p><span style="color: black;">广义的php <span style="color: black;">指的是</span>用后缀名为</span><span style="color: black;"><span style="color: black;">.php</span></span>的脚本,<span style="color: black;">便是</span>程序员编写的php代码:<span style="color: black;">狭义的php <span style="color: black;">指的是</span>php<span style="color: black;">安排</span>程序,<span style="color: black;">例如</span>说 </span><strong style="color: blue;"><span style="color: black;">lnmp</span></strong><span style="color: black;">,这个php <span style="color: black;">便是</span> </span><strong style="color: blue;"><span style="color: black;">php-fpm</span></strong>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">php-fpm是什么</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">直接定义:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">php-fpm是一个内置php解析器,采用prefork+异步IO 模型的高性能服务器程序</span></strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">php-fpm从php5.3.3<span style="color: black;">起始</span><span style="color: black;">已然</span>进入到php源代码包,之前是<span style="color: black;">做为</span>patch存在的</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">很少人会去读php本身源代码,我6年前<span style="color: black;">处理</span>php内存<span style="color: black;">泄密</span>问题的时候做了些<span style="color: black;">科研</span>,<span style="color: black;">近期</span>再查看了一番,<span style="color: black;">发掘</span>php的<span style="color: black;">研发</span>者<span style="color: black;">特别有</span>诚意,这是一款非常出色的服务器软件,支持如下</span></p><span style="color: black;">php-fpm 采用 prefork的方式 (listen同一个<span style="color: black;">位置</span>,<span style="color: black;">而后</span>fork出若干子进程)</span><span style="color: black;">在子进程里面,采用异步IO处理客户端请求</span><span style="color: black;">event模型<span style="color: black;">能够</span>在php-fpm.conf中配置</span><span style="color: black;">;</span> <span style="color: black;">Specify</span> <span style="color: black;">the</span> <span style="color: black;">event</span> <span style="color: black;">mechanism</span> <span style="color: black;">FPM</span> <span style="color: black;">will</span> <span style="color: black;">use.</span> <span style="color: black;">The</span> <span style="color: black;">following</span>
    <span style="color: black;">is available:</span>
    <span style="color: black;">;</span> <span style="color: black;">-</span> <span style="color: black;">select</span> <span style="color: black;">(any</span> <span style="color: black;">POSIX</span> <span style="color: black;">os)</span>
    <span style="color: black;">;</span> <span style="color: black;">-</span> <span style="color: black;">poll</span> <span style="color: black;">(any</span> <span style="color: black;">POSIX</span> <span style="color: black;">os)</span>
    <span style="color: black;">;</span> <span style="color: black;">-</span> <span style="color: black;">epoll</span> <span style="color: black;">(linux</span> <span style="color: black;">&gt;=</span> <span style="color: black;">2.5</span><span style="color: black;">.44</span><span style="color: black;">)</span>
    <span style="color: black;">;</span> <span style="color: black;">-</span> <span style="color: black;">kqueue</span> <span style="color: black;">(FreeBSD</span> <span style="color: black;">&gt;=</span> <span style="color: black;">4.1</span><span style="color: black;">,</span> <span style="color: black;">OpenBSD</span> <span style="color: black;">&gt;=</span> <span style="color: black;">2.9</span><span style="color: black;">,</span> <span style="color: black;">NetBSD</span> <span style="color: black;">&gt;=</span> <span style="color: black;">2.0</span><span style="color: black;">)</span>
    <span style="color: black;">;</span> <span style="color: black;">-</span> <span style="color: black;">/dev/poll</span> <span style="color: black;">(Solaris</span> <span style="color: black;">&gt;=</span> <span style="color: black;">7</span><span style="color: black;">)</span>
    <span style="color: black;">;</span> <span style="color: black;">-</span> <span style="color: black;">port</span> <span style="color: black;">(Solaris</span> <span style="color: black;">&gt;=</span> <span style="color: black;">10</span><span style="color: black;">)</span>
    <span style="color: black;">;</span> <span style="color: black;">Default Value:</span> <span style="color: black;">not</span> <span style="color: black;">set</span> <span style="color: black;">(auto</span> <span style="color: black;">detection)</span>
    <span style="color: black;">;events.mechanism</span> <span style="color: black;">=</span> <span style="color: black;">epoll</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在linux服务器上,<span style="color: black;">倘若</span>不设置</span><span style="color: black;"><span style="color: black;">events.mechanism</span></span>,<span style="color: black;">那样</span>默认<span style="color: black;">便是</span>采用epoll,<span style="color: black;">因此</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">php-fpm的IO模型&amp;并发处理能力和nginx是完全一致</span></strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">nginx以性能卓越闻名,大部分程序员都认为php效率低下,看了源代码,才<span style="color: black;">晓得</span>这是传奇啊</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在高性能<span style="color: black;">安排</span>的时候,<span style="color: black;">大众</span><span style="color: black;">常常</span>会针对性的优化nginx 。我自己之前<span style="color: black;">安排</span>php程序<span style="color: black;">亦</span>犯了错误,8G内存的server,php-fpm的max children都会设置128+,<span style="color: black;">此刻</span>看来太多了,参考nginx的<span style="color: black;">安排</span>:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">nginx的worker_process<span style="color: black;">通常</span>设置为 2 * cpu-core</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">php-fpm配置为 3倍 cpu core number就<span style="color: black;">能够</span>了</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">php-fpm稳定性比nginx稍差</span></strong><span style="color: black;">这是<span style="color: black;">由于</span>php-fpm内置了一个php解析器,php-fpm进程就和php程序<span style="color: black;">绑缚</span>了,<span style="color: black;">倘若</span>php脚本写得<span style="color: black;">欠好</span>,有死循环<span style="color: black;">或</span>阻塞在某个远端资源上,会拖累加载它的php-fpm进程</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">而nginx和后端应用服务器之间<span style="color: black;">经过</span>网络连接,<span style="color: black;">能够</span>设置timeout,<span style="color: black;">不易</span>堵死的</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">php-fpm的fastcgi是短连接</span></strong><span style="color: black;"> 我原以为是长连接的,看了代码才<span style="color: black;">晓得</span><span style="color: black;">亦</span>是短连接,处理一个request就关闭掉</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">php-fpm接口采用fastcgi</span></strong><span style="color: black;"> 非常遗憾,php-fpm和fastcgi完全绑定了,<span style="color: black;">没法</span>独立<span style="color: black;">运用</span> 。只能<span style="color: black;">安排</span>在支持http-fcgi协议转换程序<span style="color: black;">背面</span>(nginx)。其实<span style="color: black;">能够</span><span style="color: black;">思虑</span>在php-fpm代码包里面引入http协议支持,<span style="color: black;">这般</span>php-fpm<span style="color: black;">能够</span>独立运行,让nodejs无话可说</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">php-fpm等同于OpenResty</span></strong><span style="color: black;"> OpenResty是一个国人<span style="color: black;">研发</span>的nginx模块,<span style="color: black;">便是</span>在nginx引入lua解释器. <span style="color: black;">实质</span>上,它和php-fpm的<span style="color: black;">独一</span>差别<span style="color: black;">便是</span>一个采用php语法,一个用lua,<span style="color: black;">因此</span>OpenResty要<span style="color: black;">做为</span>nginx<span style="color: black;">加强</span>包<span style="color: black;">运用</span>还<span style="color: black;">能够</span>,要<span style="color: black;">选取</span>它<span style="color: black;">做为</span>一个<span style="color: black;">重点</span>编程工具,<span style="color: black;">无</span>任何必要</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">从架构上<span style="color: black;">来讲</span>,php-fpm<span style="color: black;">已然</span>做到最好,超过大<span style="color: black;">都数</span> python<span style="color: black;">安排</span>工具,我再<span style="color: black;">亦</span>不黑它了</span></p>




wrjc1hod 发表于 2024-10-4 17:55:43

论坛是一个舞台,让我们在这里尽情的释放自己。

1fy07h 发表于 2024-11-3 12:07:09

外贸论坛是我们的,责任是我们的,荣誉是我们的,成就是我们的,辉煌是我们的。
页: [1]
查看完整版本: 服务器程序源代码分析之二:php-fpm