50 个 PHP 实例杀死了我的网络服务器?

50 个 PHP 实例杀死了我的网络服务器?

我之前遇到过“内存不足”问题。我刚打开顶部,它就冻结在屏幕上,因为内存不足。我列出了一个用户,其中列出了大约 50 个“php”实例。我如何限制允许打开的 PHP 实例数?或者允许单个用户使用的 PHP 实例数?为什么它使用这么多 PHP 实例?这是一个 wordpress 网站。

答案1

基本上,默认的 php-fpm 配置使用的 RAM 比您可用的 RAM 多。您需要配置 PHP-FPM,以免耗尽资源,具体方法如下:这个

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

您需要决定要使用的 PM 类型。读这个. ondemand 通常适用于低内存、流量不大的服务器。我认为我将 dynamic 用于我的主要 PHP 池,因为它可以让一些 PHP 进程始终可用并等待处理请求,然后将 ondemand 用于我很少使用的测试 PHP 池。ondemand 意味着更多的延迟,因为需要启动一个 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

相关内容