php5-fpm如果访问者过多,则会崩溃


8

我决定将我的OP从Litespeed更改为Nginx,因为我了解了很多有关Nginx会花费的低资源的信息。

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

好的,谢谢你们的帮助:)我编辑了一些内容。

我还有一些问题:

我必须删除吗?下午之前。设置?我删除了它什么配置很重要?我在/ etc / php5 / fpm中有两个快速cgi,一个叫做php-fpm.conf,在/etc/php5/fpm/pool.d中有一个快速的cgi,叫做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_params:

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/$

www.conf

; 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

The Mashine:Dualcore 4GB内存


PHP-FPM错误日志怎么说?
devicenull 2012年

只有关于我何时重新启动fpm的记录... [04-Apr-2012 13:46:44]注意:正在终止... [04-Apr-2012 13:46:44]注意:正在退出,再见再见![04-Apr-2012 13:46:44]注意:fpm正在运行,pid 1744 [04-Apr-2012 13:46:44]注意:准备处理连接[04-Apr-2012 13:48:57]注意:[pool www]子项1748在启动后132.647614秒后以代码0退出[2012年4月4日13:48:57]注意:[pool www]子项1829已启动
chillah 2012年

我只是注意到,如果我禁用/启用或编辑wordpress文件,php fpm崩溃速度会更快。但是我不是100%肯定...
chillah 2012年

您还在崩溃吗?您检查了的用法APC吗?通常,您在单独的配置文件中定义一个池(所有pm设置都存放在该文件中)。
达达2012年

是的,它仍然崩溃。当我的页面上吸引更多访问者时,崩溃次数似乎有所增加。我看不到“顶部”中的APC拥挤。我该如何检查?我现在使用cron每2分钟重新启动php fpm...。我真的需要尽快解决该问题!
chillah 2012年

Answers:


10

目前尚不清楚您是否正在使用任何操作码缓存器,例如APC。首先,将其启用。调整其内存以避免碎片。另外,请使用套接字,而不要使用http连接从nginxphp5-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错误。相应降低Lower pm.start_servers,min和max_spare(15、5、25)。

如果有大量的匿名用户,可以考虑使用任何缓存WordPress插件一样WP-FFPCWP Super Cache或者类似的。

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

也许您需要在Nginx中调整fastcgi_buffer_sizefastcgi_buffers选项。

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

munin在现场安装监控系统,以检查负载/内存消耗/延迟等。


nginx和php5-fpm之间的连接可能不是HTTP,而只是TCP。JFYI。
AykutÇevik'15

4

当您的服务器内存不足以运行php进程时,它可能会遭受痛苦。您的php5-fpm设置对于4GB RAM似乎太宽裕了。我想尽办法达到最佳php5-fpm配置(对于32GB内存和+ 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

无论您在上述目录中使用哪种timout,都应符合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/$

祝好运


我一整夜都在尝试配置,但cpu的使用率似乎降低了,但没有解决空白页和崩溃问题。这是一个Ip计数器图片:abload.de/img/unbenanntsgyxi.png
chillah 2012年

我同意set的建议request_terminate_timeout,但您的原因不正确;)该指令是在一定的CPU时间后终止正在运行的进程,以作为PHP max_execution_time无法执行时的保护措施(该值应小一些,以获取第一次机会)[来源 ]。“为了避免由于空闲进程而导致内存不足”,使用的指令是pm.process_idle_timeout = 40 [请参见此答案 ]。
tanius

1

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

对于一台具有128 MB PHP进程的4GB计算机,大约可以运行24台PHP-FPM服务器,按需运行是很好的,因为服务器将确定最大服务器数量并随着负载的增加而创建新服务器。


1
同意在大多数情况下,PHP-FPM按需流程管理器是更好的选择。但最大 子进程的数量不能这样计算:memory_limit是PHP进程在强制终止之前可能消耗的绝对最大值。平均而言,它们消耗更少。
tanius

1
而且无论如何,内存通常不是最大内存的限制因素。子级,而不是CPU内核。最高 儿童人数应设置为大约。CPU核心数,再加上一些以补偿IO等待等。没有什么比100%CPU负载更快的了,这只是队列等待与切换缓慢并行服务之间的区别。
tanius
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.