日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > php >内容正文

php

【PHP-FPM】配置,优化性能

發(fā)布時(shí)間:2025/3/20 php 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【PHP-FPM】配置,优化性能 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

關(guān)于pm.max_children

一個(gè)前提設(shè)置:
標(biāo)識(shí)fpm子進(jìn)程的產(chǎn)生模式:pm = static/dynamic

  • static :表示在fpm運(yùn)行時(shí)直接fork出pm.max_children個(gè)worker進(jìn)程,
  • dynamic:表示運(yùn)行時(shí)fork出start_servers個(gè)進(jìn)程,隨著負(fù)載的情況,動(dòng)態(tài)的調(diào)整,最多不超過(guò)max_children個(gè)進(jìn)程。

一般推薦用static
優(yōu)點(diǎn):不用動(dòng)態(tài)的判斷負(fù)載情況,提升性能;
缺點(diǎn):多占用些系統(tǒng)內(nèi)存資源;

max_children代表的worker的進(jìn)程數(shù)。對(duì)于配置越多能同時(shí)處理的并發(fā)也就越多,則是一個(gè)比較大的誤區(qū):

  • 管理進(jìn)程和worker進(jìn)程是通過(guò)pipe進(jìn)行數(shù)據(jù)通訊的。
    • 所以進(jìn)程多了,增加進(jìn)程管理的開(kāi)銷(xiāo),系統(tǒng)進(jìn)程切換的開(kāi)銷(xiāo),
    • 更核心的是,能并發(fā)執(zhí)行的fpm進(jìn)程不會(huì)超過(guò)cpu個(gè)數(shù)。
    • 因此通過(guò)多開(kāi)worker的個(gè)數(shù)來(lái)提升qps是錯(cuò)誤的。
  • 但worker進(jìn)程開(kāi)少了,如果server比較繁忙的話,會(huì)導(dǎo)到nginx把數(shù)據(jù)打到fpm的時(shí)候,發(fā)現(xiàn)所有的woker都在工作中,沒(méi)有空閑的worker來(lái)接受請(qǐng)求,從而導(dǎo)致502。

如何配置max_children及優(yōu)化PHP-FPM

php-fpm.conf有兩個(gè)至關(guān)重要的參數(shù):

  • request_terminate_timeout:可以根 據(jù)你服務(wù)器的性能進(jìn)行設(shè)定

    • request_terminate_timeout的值一般來(lái)說(shuō)值越高,性能越好,20分鐘-30分鐘都可以。
    • 由于服務(wù)器PHP腳本需要長(zhǎng)時(shí)間運(yùn)行,有的可能會(huì)超過(guò)10分鐘因此我設(shè)置了900秒,這樣不會(huì)導(dǎo)致PHP-CGI死掉而出現(xiàn)502 Bad gateway這個(gè)錯(cuò)誤。
  • max_children:原則上是越大越好;

    • max_children 的值原則上是越大越好,php-cgi的進(jìn)程多了就會(huì)處理的很快,排隊(duì)的請(qǐng)求就會(huì)很少。

    • 設(shè)置 max_children 也需要根據(jù)服務(wù)器的性能進(jìn)行設(shè)定,

      一般來(lái)說(shuō)一臺(tái)服務(wù)器正常情況下每一個(gè)php-cgi所耗費(fèi)的內(nèi)存在20M左右,因此”max_children”我設(shè)置成40個(gè),20M*40=800M也就是說(shuō)在峰值的時(shí)候所有PHP-CGI所耗內(nèi)存在800M以?xún)?nèi),低于我的有效內(nèi)存1Gb。

      而如果我 的”max_children”設(shè)置的較小,比如5-10個(gè),那么php-cgi就會(huì)“很累”,處理速度也很慢,等待的時(shí)間也較長(zhǎng)。如果長(zhǎng)時(shí)間沒(méi)有得到處 理的請(qǐng)求就會(huì)出現(xiàn)504 Gateway Time-out這個(gè)錯(cuò)誤,而正在處理的很累的那幾個(gè)php-cgi如果遇到了問(wèn)題就會(huì)出現(xiàn)502 Bad gateway這個(gè)錯(cuò)誤。

    • 計(jì)算:pm.max_children = (total RAM - RAM used by other process) / (average amount of RAM used by a PHP process)

  • max_requests:每個(gè)進(jìn)程若超過(guò)這個(gè)數(shù)目(跟php進(jìn)程有一點(diǎn)點(diǎn)關(guān)系,關(guān)系不大),就自動(dòng)殺死。

  • start_servers:the number of children created on startup

    • 計(jì)算:pm.start_servers = max_spare_servers / 2

php-fpm.d/www.conf PM配置

78 ; Choose how the process manager will control the number of child processes.79 ; Possible Values:80 ; static - a fixed number (pm.max_children) of child processes;81 ; dynamic - the number of child processes are set dynamically based on the82 ; following directives. With this process management, there will be83 ; always at least 1 children.84 ; pm.max_children - the maximum number of children that can85 ; be alive at the same time.86 ; pm.start_servers - the number of children created on startup.87 ; pm.min_spare_servers - the minimum number of children in 'idle'88 ; state (waiting to process). If the number89 ; of 'idle' processes is less than this90 ; number then some children will be created.91 ; pm.max_spare_servers - the maximum number of children in 'idle'92 ; state (waiting to process). If the number93 ; of 'idle' processes is greater than this94 ; number then some children will be killed.95 ; ondemand - no children are created at startup. Children will be forked when96 ; new requests will connect. The following parameter are used:97 ; pm.max_children - the maximum number of children that98 ; can be alive at the same time.99 ; pm.process_idle_timeout - The number of seconds after which 100 ; an idle process will be killed.

Docker Nginx PHP-FPM config example

總結(jié)

以上是生活随笔為你收集整理的【PHP-FPM】配置,优化性能的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。