【PHP-FPM】配置,优化性能
關于pm.max_children
一個前提設置:
標識fpm子進程的產生模式:pm = static/dynamic
- static :表示在fpm運行時直接fork出pm.max_children個worker進程,
- dynamic:表示運行時fork出start_servers個進程,隨著負載的情況,動態的調整,最多不超過max_children個進程。
一般推薦用static
優點:不用動態的判斷負載情況,提升性能;
缺點:多占用些系統內存資源;
max_children代表的worker的進程數。對于配置越多能同時處理的并發也就越多,則是一個比較大的誤區:
- 管理進程和worker進程是通過pipe進行數據通訊的。
- 所以進程多了,增加進程管理的開銷,系統進程切換的開銷,
- 更核心的是,能并發執行的fpm進程不會超過cpu個數。
- 因此通過多開worker的個數來提升qps是錯誤的。
- 但worker進程開少了,如果server比較繁忙的話,會導到nginx把數據打到fpm的時候,發現所有的woker都在工作中,沒有空閑的worker來接受請求,從而導致502。
如何配置max_children及優化PHP-FPM
php-fpm.conf有兩個至關重要的參數:
-
request_terminate_timeout:可以根 據你服務器的性能進行設定
- request_terminate_timeout的值一般來說值越高,性能越好,20分鐘-30分鐘都可以。
- 由于服務器PHP腳本需要長時間運行,有的可能會超過10分鐘因此我設置了900秒,這樣不會導致PHP-CGI死掉而出現502 Bad gateway這個錯誤。
-
max_children:原則上是越大越好;
-
max_children 的值原則上是越大越好,php-cgi的進程多了就會處理的很快,排隊的請求就會很少。
-
設置 max_children 也需要根據服務器的性能進行設定,
一般來說一臺服務器正常情況下每一個php-cgi所耗費的內存在20M左右,因此”max_children”我設置成40個,20M*40=800M也就是說在峰值的時候所有PHP-CGI所耗內存在800M以內,低于我的有效內存1Gb。
而如果我 的”max_children”設置的較小,比如5-10個,那么php-cgi就會“很累”,處理速度也很慢,等待的時間也較長。如果長時間沒有得到處 理的請求就會出現504 Gateway Time-out這個錯誤,而正在處理的很累的那幾個php-cgi如果遇到了問題就會出現502 Bad gateway這個錯誤。
-
計算:pm.max_children = (total RAM - RAM used by other process) / (average amount of RAM used by a PHP process)
-
-
max_requests:每個進程若超過這個數目(跟php進程有一點點關系,關系不大),就自動殺死。
-
start_servers:the number of children created on startup
- 計算: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
總結
以上是生活随笔為你收集整理的【PHP-FPM】配置,优化性能的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Docker】ADD COPY no
- 下一篇: 【PHP】 php7.0 扩展列表