Php

50 個 PHP 實例殺死了我的 Web 伺服器?

  • April 16, 2019

我之前遇到了“記憶體不足”的問題。我剛剛打開頂部,它在記憶體不足時凍結在螢幕上。我列出了一個使用者,其中列出了大約 50 個“php”實例。如何限制允許打開 PHP 的實例數?或者允許單個使用者使用的 PHP 實例數?為什麼它使用這麼多 PHP 實例?這是一個wordpress網站。

基本上,預設的 php-fpm 配置使用的 RAM 比您可用的更多。您需要配置 PHP-FPM,這樣它就不會耗盡您的資源,按照這樣的指南

sudo nano /etc/php/7.0/fpm/pool.d/www.conf

您需要決定要使用的 PM 類型。讀這個。ondemand 通常適用於流量很少的低記憶體伺服器。我認為我將動態用於我的主 PHP 池,因為它使一些 PHP 程序始終可用並等待服務請求,然後按需為我很少使用的測試 PHP 池。按需意味著更多延遲,因為需要啟動 php 程序,但對於容量較小的站點,這可能還可以。

如果您的記憶體和流量較低,配置可能看起來像這樣

pm = ondemand

; The number of child processes to be created when pm is set to 'static' and the
; maximum number of child processes when pm is set to 'dynamic' or 'ondemand'.
; This value sets the limit on the number of simultaneous requests that will be
; served. Equivalent to the ApacheMaxClients directive with mpm_prefork.
; Equivalent to the PHP_FCGI_CHILDREN environment variable in the original PHP
; CGI. The below defaults are based on a server without much resources. Don't
; forget to tweak pm.* to fit your needs.
; Note: Used when pm is set to 'static', 'dynamic' or 'ondemand'
; Note: This value is mandatory.
pm.max_children = 5

; The number of child processes created on startup.
; Note: Used only when pm is set to 'dynamic'
; Default Value: min_spare_servers + (max_spare_servers - min_spare_servers) / 2
;pm.start_servers = 2

; The desired minimum number of idle server processes.
; Note: Used only when pm is set to 'dynamic'
; Note: Mandatory when pm is set to 'dynamic'
;pm.min_spare_servers = 1

; The desired maximum number of idle server processes.
; Note: Used only when pm is set to 'dynamic'
; Note: Mandatory when pm is set to 'dynamic'
;pm.max_spare_servers = 3

; The number of seconds after which an idle process will be killed.
; Note: Used only when pm is set to 'ondemand'
; Default Value: 10s
pm.process_idle_timeout = 60s;

; The number of requests each child process should execute before respawning.
; This can be useful to work around memory leaks in 3rd party libraries. For
; endless request processing specify '0'. Equivalent to PHP_FCGI_MAX_REQUESTS.
; Default Value: 0
pm.max_requests = 50

如果您獲得更高的流量,則最好使用動態並調整值,以便在可用的 CPU / RAM 中執行足夠的伺服器。

PHP 5.6

在我的 AWS t2.nano 上,我有兩個池。這是為生產請求提供服務的動態池的定義

文件:/etc/php-fpm-5.6.d/www.conf

pm = dynamic
pm.start_servers = 2
pm.max_children = 4
pm.start_servers = 2
pm.max_spare_servers = 2
pm.min_spare_servers = 1
pm.max_requests = 25

這是我的測試池,用於我很少訪問的測試伺服器

文件:/etc/php-fpm-5.6.d/testpool.conf

pm = ondemand
pm.max_children = 2
pm.process_idle_timeout = 120s;
pm.max_requests = 50

引用自:https://serverfault.com/questions/963203