访问量过大时 Php5-fpm 会崩溃

访问量过大时 Php5-fpm 会崩溃

我决定将我的 OP 从 Litespeed 更改为 Nginx,因为我读到了很多关于 Nginx 所需资源成本低的文章。

我正在运行一个 Wordpress 网站,有 500 个在线用户

好的,非常感谢大家的帮助:)我编辑了一些内容。

我还有一些问题:

我是否必须删除 pm. 设置前的 ;?我删除了它 哪些配置算数?我有两个快速 cgi,一个在 /etc/php5/fpm 中,名为 php-fpm.conf,另一个在 /etc/php5/fpm/pool.d 中,名为 www.conf?

我晚上尝试了新的配置,但当我醒来时,我已经看到一个空白页。顶部:

top - 13:55:27 up 1 day, 19:28,  2 users,  load average: 0.18, 0.36, 0.19
Tasks:  84 total,   1 running,  83 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.2%us,  0.0%sy,  0.0%ni, 99.8%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   3974264k total,  1051360k used,  2922904k free,   162380k buffers
Swap:  3998700k total,        0k used,  3998700k free,   609220k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 1857 www-data  20   0  193m  55m  22m S    0  1.4   0:04.67 php5-fpm
    1 root      20   0  8356  808  680 S    0  0.0   0:01.37 init
    2 root      20   0     0    0    0 S    0  0.0   0:00.00 kthreadd

我认为 CPU 使用率现在较低,但这并不能解决崩溃问题...

我的配置现在是:php-fpm.conf

    ;;;;;;;;;;;;;;;;;;;;;
; FPM Configuration ;
;;;;;;;;;;;;;;;;;;;;;

; All relative paths in this configuration file are relative to PHP's install
; prefix (/usr). This prefix can be dynamicaly changed by using the
; '-p' argument from the command line.

; Include one or more files. If glob(3) exists, it is used to include a bunch of
; files from a glob(3) pattern. This directive can be used everywhere in the
; file.
; Relative path can also be used. They will be prefixed by:
;  - the global prefix if it's been set (-p arguement)
;  - /usr otherwise
;include=/etc/php5/fpm/*.conf

;;;;;;;;;;;;;;;;;;
; Global Options ;
;;;;;;;;;;;;;;;;;;

[global]
; Pid file
; Note: the default prefix is /var
; Default Value: none
pid = /var/run/php5-fpm.pid

; Error log file
; If it's set to "syslog", log is sent to syslogd instead of being written
; in a local file.
; Note: the default prefix is /var
; Default Value: log/php-fpm.log
error_log = /var/log/php5-fpm.log

; syslog_facility is used to specify what type of program is logging the
; message. This lets syslogd specify that messages from different facilities
; will be handled differently.
; See syslog(3) for possible values (ex daemon equiv LOG_DAEMON)
; Default Value: daemon
;syslog.facility = daemon

; syslog_ident is prepended to every message. If you have multiple FPM
; instances running on the same server, you can change the default value
; which must suit common needs.
; Default Value: php-fpm
;syslog.ident = php-fpm

; Log level
; Possible Values: alert, error, warning, notice, debug
; Default Value: notice
;log_level = notice

; If this number of child processes exit with SIGSEGV or SIGBUS within the time
; interval set by emergency_restart_interval then FPM will restart. A value
; of '0' means 'Off'.
; Default Value: 0
emergency_restart_threshold = 1

; Interval of time used by emergency_restart_interval to determine when 
; a graceful restart will be initiated.  This can be useful to work around
; accidental corruptions in an accelerator's shared memory.
; Available Units: s(econds), m(inutes), h(ours), or d(ays)
; Default Unit: seconds
; Default Value: 0
emergency_restart_interval = 1s

; Time limit for child processes to wait for a reaction on signals from master.
; Available units: s(econds), m(inutes), h(ours), or d(ays)
; Default Unit: seconds
; Default Value: 0
process_control_timeout = 10s

; The maximum number of processes FPM will fork. This has been design to control
; the global number of processes when using dynamic PM within a lot of pools.
; Use it with caution.
; Note: A value of 0 indicates no limit
; Default Value: 0
process.max = 150

; Send FPM to background. Set to 'no' to keep FPM in foreground for debugging.
; Default Value: yes
;daemonize = yes

; Set open file descriptor rlimit for the master process.
; Default Value: system defined value
rlimit_files = 1024

; Set max core size rlimit for the master process.
; Possible Values: 'unlimited' or an integer greater or equal to 0
; Default Value: system defined value
;rlimit_core = 0

; Specify the event mechanism FPM will use. The following is available:
; - select     (any POSIX os)
; - poll       (any POSIX os)
; - epoll      (linux >= 2.5.44)
; - kqueue     (FreeBSD >= 4.1, OpenBSD >= 2.9, NetBSD >= 2.0)
; - /dev/poll  (Solaris >= 7)
; - port       (Solaris >= 10)
; Default Value: not set (auto detection)
; events.mechanism = epoll

;;;;;;;;;;;;;;;;;;;;
; Pool Definitions ; 
;;;;;;;;;;;;;;;;;;;;

; Multiple pools of child processes may be started with different listening
; ports and different management options.  The name of the pool will be
; used in logs and stats. There is no limitation on the number of pools which
; FPM can handle. Your system will tell you anyway :)

; To configure the pools it is recommended to have one .conf file per
; pool in the following directory:
include=/etc/php5/fpm/pool.d/*.conf

request_terminate_timeout = 30s

pm.max_children = 25

; 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 = 5

; 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 = 2

; 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 = 5

; 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 = 10s;

fastcgi_参数:

fastcgi_param   QUERY_STRING        $query_string;
fastcgi_param   REQUEST_METHOD      $request_method;
fastcgi_param   CONTENT_TYPE        $content_type;
fastcgi_param   CONTENT_LENGTH      $content_length;

fastcgi_param   SCRIPT_FILENAME     $request_filename;
fastcgi_param   SCRIPT_NAME     $fastcgi_script_name;
fastcgi_param   REQUEST_URI     $request_uri;
fastcgi_param   DOCUMENT_URI        $document_uri;
fastcgi_param   DOCUMENT_ROOT       $document_root;
fastcgi_param   SERVER_PROTOCOL     $server_protocol;

fastcgi_param   GATEWAY_INTERFACE   CGI/1.1;
fastcgi_param   SERVER_SOFTWARE     nginx/$nginx_version;

fastcgi_param   REMOTE_ADDR     $remote_addr;
fastcgi_param   REMOTE_PORT     $remote_port;
fastcgi_param   SERVER_ADDR     $server_addr;
fastcgi_param   SERVER_PORT     $server_port;
fastcgi_param   SERVER_NAME     $server_name;

#fastcgi_param  HTTPS           $https;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param   REDIRECT_STATUS     200;

fastcgi_connect_timeout 60;
fastcgi_send_timeout 180;
fastcgi_read_timeout 180;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on;

nginx配置:

user www-data;
worker_processes 2;
pid /var/run/nginx.pid;

events {
    worker_connections 500;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 40;
    types_hash_max_size 2048;
    server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # Logging Settings
    ##

    # access_log /var/log/nginx/access.log;
    # error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

 gzip_vary on;
 gzip_proxied any;
 gzip_comp_level 9;
 gzip_buffers 16 8k;
 gzip_http_version 1.1;
 gzip_types text/plain text/css application/json application/x-javascript text/$

閣下網站

; Start a new pool named 'www'.
; the variable $pool can we used in any directive and will be replaced by the
; pool name ('www' here)
[www]

user = www-data
group = www-data


pm = dynamic

pm.max_children = 25

pm.start_servers = 5

pm.min_spare_servers = 2

pm.max_spare_servers = 5

pm.process_idle_timeout = 10s;

pm.max_requests = 100

request_terminate_timeout = 120s

机器:双核 4GB RAM

答案1

不清楚您是否正在使用任何操作码缓存器,例如APC。首先,将其启用。调整其内存以避免碎片化。另外,使用套接字(而不是从nginx到 的http 连接php5-fpm),并将该套接字放在 上/dev/shm。在 nginx 中,使用fastcgi_pass unix:/dev/shm/php5-fpm.sock;,并相应地更改 php5-fpm 的配置。

尝试降低pm.max_children到更实际的值,例如 30-40-50-60,然后使用 查看负载和可用内存free -m。查看客户端是否收到任何 502/504 错误。pm.start_servers相应地降低 、min 和 max_spare(15、5、25)。

如果有很多匿名用户,请考虑使用任何 wordpress 缓存插件,如WP-FFPCWP Super Cache类似的。

如果您在 nginx 中启用了 gzipping,请禁用它。

也许您需要在 nginx 中调整fastcgi_buffer_size和选项。fastcgi_buffers

检查您是否已启用查询缓存并且 Mysql 上有足够的查询缓存。

munin在网站上安装监控系统来检查负载/内存消耗/延迟等。

答案2

当您的服务器内存不足而无法运行 php 进程时,它可能会受到影响。您的 php5-fpm 设置似乎对于 4GB RAM 来说太慷慨了。我进行了很多调整才获得最佳的 php5-fpm 配置(对于 32GB RAM 和 +1K 在线用户,以下是我的相关参数:

pm.max_children = 25
pm.start_servers = 5
pm.min_spare_servers = 2 
pm.max_spare_servers = 5
pm.max_requests = 100

另外,不要忘记设置请求终止超时,以避免由于空闲进程而导致内存短缺:

request_terminate_timeout = 120s

您在以上指令中使用的任何超时都应符合 php.ini 中的 max_execution_time 指令。

对于 nginx 来说,启用 gzip 是一个非常好的主意:它在 CPU 方面非常便宜,并且可以节省服务器的大量带宽。

   #sendfile on;
    #tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 10;
    types_hash_max_size 2048;
    # server_tokens off;
    gzip on;
    gzip_disable "msie6";

    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 9;
    gzip_buffers 16 8k;
    gzip_http_version 1.1;
    gzip_types text/plain text/css application/json application/x-javascript text/$

祝你好运

答案3

我建议使用 pm = ondemand,并确保 php 中的 memory_limit 的大小与机器的内存大小相等。

对于一台 4GB 机器,其中 PHP 进程为 128 MB,那么可以运行大约 24 个 PHP-FPM 服务器,按需服务很不错,因为服务器可以确定最大服务器数量,并在负载增加时创建新的服务器。

相关内容