请教LNMP 0.8 运作时cpu常飙高导致网站速度变慢的问题
请教军哥:先感谢您提供这么好的东西让我们使用, 但因为是新手上路, 所以有些问题始终找不到答案, 只好上来求救.
安装版本: lnmp 0.8 我们有把 MySQL资料库主机 和 Nginx 网站主机分成两台来跑
问题: 用 htop 观察 CPU值常飙高, 浏览网页也会变慢 (Chrome浏览器会一直绕圈圈)
# htop
1[||| 3.9%] Tasks: 146, 14 thr; 2 running
2[|||||| 9.7%] Load average: 6.00 6.67 6.54
3[||| 4.6%] Uptime: 30 days, 08:10:17
4[|||| 7.1%]
5[|| 3.3%]
6[|||||| 9.2%]
7[||| 3.2%]
8[|||||||||| 18.3%]
Mem[|||||||||||||||||||||||||||||||||||||||3207/13931MB]
Swp[| 1/10238MB]
PID USER PRINIVIRT RES SHR S CPU% MEM% TIME+Command
5365 www 20 0129M 198684920 S0.00.11:33.21 /usr/local/php/bin/php-cgi --fpm --fpm-config /usr/local/ph
7619 root 20 0110M20841268 R1.00.00:00.11 htop
4972 www 20 0238M197M 964 S1.01.41:06.89 nginx: worker process
4981 www 20 0237M196M 964 S0.01.41:04.09 nginx: worker process
5252 www 20 0129M 216724924 S0.00.21:34.98 /usr/local/php/bin/php-cgi --fpm --fpm-config /usr/local/ph
5254 www 20 0129M 216684920 S1.00.21:30.57 /usr/local/php/bin/php-cgi --fpm --fpm-config /usr/local/ph
5255 www 20 0129M 217884988 S0.00.21:28.59 /usr/local/php/bin/php-cgi --fpm --fpm-config /usr/local/ph
5267 www 20 0129M 217444932 S0.00.21:32.57 /usr/local/php/bin/php-cgi --fpm --fpm-config /usr/local/ph
5269 www 20 0127M 213684632 S0.00.11:37.92 /usr/local/php/bin/php-cgi --fpm --fpm-config /usr/local/ph
5274 www 20 0127M 213444616 S0.00.11:31.34 /usr/local/php/bin/php-cgi --fpm --fpm-config /usr/local/ph
5283 www 20 0129M 216964940 S0.00.21:33.54 /usr/local/php/bin/php-cgi --fpm --fpm-config /usr/local/ph
5286 www 20 0129M 217364960 S0.00.21:35.93 /usr/local/php/bin/php-cgi --fpm --fpm-config /usr/local/ph
5289 www 20 0127M 213684616 S0.00.11:32.12 /usr/local/php/bin/php-cgi --fpm --fpm-config /usr/local/ph
5291 www 20 0129M 197524904 R0.00.11:32.20 /usr/local/php/bin/php-cgi --fpm --fpm-config /usr/local/ph
5292 www 20 0129M 216444920 S0.00.21:35.01 /usr/local/php/bin/php-cgi --fpm --fpm-config /usr/local/ph
5304 www 20 0129M 218084928 S0.00.21:31.37 /usr/local/php/bin/php-cgi --fpm --fpm-config /usr/local/ph
5305 www 20 0127M 212724608 S1.00.11:35.34 /usr/local/php/bin/php-cgi --fpm --fpm-config /usr/local/ph
5310 www 20 0129M 216244912 S0.00.21:36.29 /usr/local/php/bin/php-cgi --fpm --fpm-config /usr/local/ph
5311 www 20 0129M 209925012 S0.00.11:34.46 /usr/local/php/bin/php-cgi --fpm --fpm-config /usr/local/ph
#netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S}'
TIME_WAIT 4881
FIN_WAIT1 10
ESTABLISHED 206
SYN_RECV 3
CLOSING 2
我调整过 max_children 为 200或300也没用, 也照着张宴的书优化过nignx设定也没用 >"<
安装环境: 8核CPU + 12GB 记忆体 + CentOS 6.2 (64bit) + DRBD (两台主机各切500GB跑Disk RAID).
4个设定档如下:
●(1). /usr/local/nginx/conf/nginx.conf
userwww www;
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
worker_processes 8;
#worker_processes 16;
#worker_cpu_affinity 0000000000000001 0000000000000010 0000000000000100 0000000000001000 0000000000010000 0000000000100000 0000000001000000 0000000010000000
0000000100000000 0000001000000000 0000010000000000 0000100000000000 0001000000000000 0010000000000000 0100000000000000 1000000000000000;
#proxy_next_upstream error timeout invalid_header http_500 http_503;
error_log/var/log/nginx/nginx_error.logcrit;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 65535;
events
{ use epoll;
worker_connections 65535; }
http
{ include mime.types;
default_typeapplication/octet-stream;
server_names_hash_bucket_size 512;
client_header_buffer_size 32k;
#large_client_header_buffers 4 32k;
large_client_header_buffers 4 128k;
client_max_body_size 50m;
client_body_buffer_size 16k;
client_body_temp_path /nginx_tmp 1 2;
client_body_timeout 60;
client_header_timeout 60;
sendfile on;
#tcp_nopush on;
tcp_nopush off;
#keepalive_timeout 60;
keepalive_timeout 3;
tcp_nodelay on;
#fastcgi_connect_timeout 60;
#fastcgi_send_timeout 180;
#fastcgi_read_timeout 180;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
#fastcgi_buffer_size 64k;
fastcgi_buffer_size 128k;
#fastcgi_buffers 4 64k;
fastcgi_buffers 4 256k;
#fastcgi_busy_buffers_size 128k;
fastcgi_busy_buffers_size 256k;
#fastcgi_temp_file_write_size 128k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on;
gzip on;
gzip_min_length1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
#limit_zonecrawler$binary_remote_addr10m;
server
{
listen 80;
#server_name localhost;
server_name www.linsanity.tw;
index index.php index.html index.htm default.html default.htm default.php;
root/www/www.linsanity.tw/htdocs;
#location ~ .*\.(php|php5)?$ {
location ~ \.(php|php5)?$ {
fastcgi_pass 127.0.0.1:9000;
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /www/www.linsanity.tw/htdocs$fastcgi_script_name;
include fcgi.conf; }
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
access_log off;
expires 30d; }
location ~ .*\.(js|css)?$ {
access_log off;
expires 12h; }
log_format www.linsanity.tw
'$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent"';
access_log/var/log/nginx/access.log www.linsanity.tw;
error_log/var/log/nginx/error.log;
error_page 404 = /miss.html;
location /nginx_status { stub_status on;
access_log off;
allow 100.2.3.4;
allow 127.0.0.1;
deny all; }
location /p/ { alias/www/p/;
indexindex.php index.html index.htm; }
location ~ ^/p/.+\.php$ {
root /www/p/;
rewrite /p/(.*\.php?) /$1 break;
include fcgi.conf;
fastcgi_pass 127.0.0.1:9000;
fastcgi_indexindex.php;
fastcgi_param SCRIPT_FILENAME /www/p$fastcgi_script_name; }
rewrite ^/(+)$ /list.php?id=$1;
rewrite ^/objview-(+).html$ /view.php?id=$1;
}
}
include vhost/*.conf;
} ●(2). /usr/local/php/etc/php-fpm.conf
<configuration>
All relative paths in this config are relative to php's install prefix
<section name="global_options">
Pid file
<value name="pid_file">/usr/local/php/logs/php-fpm.pid</value>
Error log file
<value name="error_log">/var/log/php-fpm.log</value>
Log level
<value name="log_level">notice</value>
When this amount of php processes exited with SIGSEGV or SIGBUS ...
<value name="emergency_restart_threshold">10</value>
... in a less than this interval of time, a graceful restart will be initiated.
Useful to work around accidental curruptions in accelerator's shared memory.
<value name="emergency_restart_interval">1m</value>
Time limit on waiting child's reaction on signals from master
<value name="process_control_timeout">5s</value>
Set to 'no' to debug fpm
<value name="daemonize">yes</value>
</section>
<workers>
<section name="pool">
Name of pool. Used in logs and stats.
<value name="name">default</value>
Address to accept fastcgi requests on.
Valid syntax is 'ip.ad.re.ss:port' or just 'port' or '/path/to/unix/socket'
<value name="listen_address">127.0.0.1:9000</value>
<value name="listen_options">
Set listen(2) backlog
<value name="backlog">-1</value>
Set permissions for unix socket, if one used.
In Linux read/write permissions must be set in order to allow connections from web server.
Many BSD-derrived systems allow connections regardless of permissions.
<value name="owner"></value>
<value name="group"></value>
<value name="mode">0666</value>
</value>
Additional php.ini defines, specific to this pool of workers.
<value name="php_defines">
<value name="sendmail_path">/usr/sbin/postfix -t -i</value>
<value name="display_errors">1</value>
</value>
Unix user of processes
<value name="user">www</value>
Unix group of processes
<value name="group">www</value>
Process manager settings
<value name="pm">
Sets style of controling worker process count.
Valid values are 'static' and 'apache-like'
<value name="style">static</value>
Sets the limit on the number of simultaneous requests that will be served.
Equivalent to Apache MaxClients directive.
Equivalent to PHP_FCGI_CHILDREN environment in original php.fcgi
Used with any pm_style.
<value name="max_children">100</value>
Settings group for 'apache-like' pm style
<value name="apache_like">
Sets the number of server processes created on startup.
Used only when 'apache-like' pm_style is selected
<value name="StartServers">20</value>
Sets the desired minimum number of idle server processes.
Used only when 'apache-like' pm_style is selected
<value name="MinSpareServers">5</value>
Sets the desired maximum number of idle server processes.
Used only when 'apache-like' pm_style is selected
<value name="MaxSpareServers">35</value>
</value>
</value>
The timeout (in seconds) for serving a single request after which the worker process will be terminated
Should be used when 'max_execution_time' ini option does not stop script execution for some reason
'0s' means 'off'
<value name="request_terminate_timeout">0s</value>
The timeout (in seconds) for serving of single request after which a php backtrace will be dumped to slow.log file
'0s' means 'off'
<value name="request_slowlog_timeout">5s</value>
The log file for slow requests
<value name="slowlog">/var/log/php-fpm-slow.log</value>
Set open file desc rlimit
<value name="rlimit_files">65535</value>
Set max core size rlimit
<value name="rlimit_core">0</value>
Chroot to this directory at the start, absolute path
<value name="chroot"></value>
Chdir to this directory at the start, absolute path
<value name="chdir"></value>
Redirect workers' stdout and stderr into main error log.
If not set, they will be redirected to /dev/null, according to FastCGI specs
<value name="catch_workers_output">yes</value>
How much requests each process should execute before respawn.
Useful to work around memory leaks in 3rd party libraries.
For endless request processing please specify 0
Equivalent to PHP_FCGI_MAX_REQUESTS
<value name="max_requests">0</value>
Comma separated list of ipv4 addresses of FastCGI clients that allowed to connect.
Equivalent to FCGI_WEB_SERVER_ADDRS environment in original php.fcgi (5.2.2+)
Makes sense only with AF_INET listening socket.
<value name="allowed_clients">127.0.0.1</value>
Pass environment variables like LD_LIBRARY_PATH
All $VARIABLEs are taken from current environment
<value name="environment">
<value name="HOSTNAME">$HOSTNAME</value>
<value name="PATH">/usr/local/bin:/usr/bin:/bin</value>
<value name="TMP">/tmp</value>
<value name="TMPDIR">/tmp</value>
<value name="TEMP">/tmp</value>
<value name="OSTYPE">$OSTYPE</value>
<value name="MACHTYPE">$MACHTYPE</value>
<value name="MALLOC_CHECK_">2</value>
</value>
</section>
</workers>
</configuration>
●(3). /usr/local/php/etc/php.ini
engine = On
zend.ze1_compatibility_mode = Off
short_open_tag = On
asp_tags = Off
precision =12
y2k_compliance = On
output_buffering = On
zlib.output_compression = Off
implicit_flush = Off
unserialize_callback_func=
serialize_precision = 100
allow_call_time_pass_reference = On
safe_mode = Off
safe_mode_gid = Off
safe_mode_include_dir =
safe_mode_exec_dir =
safe_mode_allowed_env_vars = PHP_
safe_mode_protected_env_vars = LD_LIBRARY_PATH
disable_functions =
disable_classes =
expose_php = On
max_execution_time = 300 ; Maximum execution time of each script, in seconds
max_input_time = 60 ; Maximum amount of time each script may spend parsing request data
memory_limit = 256M ; Maximum amount of memory a script may consume (256MB)
error_reporting = E_ALL & ~E_NOTICE
display_errors = On
display_startup_errors = Off
log_errors = On
log_errors_max_len = 1024
ignore_repeated_errors = Off
ignore_repeated_source = Off
report_memleaks = On
track_errors = Off
variables_order = "EGPCS"
register_globals = On
register_long_arrays = On
register_argc_argv = On
auto_globals_jit = On
post_max_size = 50M
magic_quotes_gpc = On
magic_quotes_runtime = Off
magic_quotes_sybase = Off
auto_prepend_file =
default_mimetype = "text/html"
include_path =
doc_root =
user_dir =
extension_dir =
extension = "memcache.so"
extension = "pdo_mysql.so"
enable_dl = On
cgi.fix_pathinfo=0
file_uploads = On
upload_tmp_dir = /tmp
upload_max_filesize = 50M
max_file_uploads = 30
allow_url_fopen = On
allow_url_include = On
default_socket_timeout = 60
date.timezone = Asia/Taipei
define_syslog_variables= Off
SMTP = localhost
smtp_port = 25
sendmail_from = service@linsanity.tw
sendmail_path = localhost
sql.safe_mode = Off
odbc.allow_persistent = On
odbc.check_persistent = On
odbc.max_persistent = -1
odbc.max_links = -1
odbc.defaultlrl = 4096
odbc.defaultbinmode = 1
mysql.allow_persistent = On
mysql.max_persistent = -1
mysql.max_links = -1
mysql.default_port =
mysql.default_socket =
mysql.default_host =
mysql.default_user =
mysql.default_password =
mysql.connect_timeout = 60
mysql.trace_mode = Off
session.save_handler = files
session.save_path = "3;/tmp"
session.use_cookies = 1
session.name = PHPKEY
session.auto_start = 1
session.cookie_lifetime = 0
session.cookie_path = /
session.cookie_domain =
session.cookie_httponly =
session.serialize_handler = php
session.gc_probability = 1
session.gc_divisor = 100
session.gc_maxlifetime = 7200
session.bug_compat_42 = 1
session.bug_compat_warn = 1
session.referer_check =
session.entropy_length = 0
session.entropy_file =
session.cache_limiter = nocache
session.cache_expire = 180
session.use_trans_sid = 0
session.hash_function = 0
session.hash_bits_per_character = 4
url_rewriter.tags = "a=href,area=href,frame=src,input=src,form=,fieldset="
zend_optimizer.optimization_level=1
zend_extension="/usr/local/zend/ZendOptimizer.so" ●(4). /etc/sysctl.conf有设定 ulimit -SHn 65535
kernel.sysrq = 0
kernel.core_uses_pid = 1
net.bridge.bridge-nf-call-ip6tables = 0
net.bridge.bridge-nf-call-iptables = 0
net.bridge.bridge-nf-call-arptables = 0
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.shmmax = 68719476736
kernel.shmall = 4294967296
net.ipv4.icmp_echo_ignore_broadcasts = 1
net.ipv4.icmp_ignore_bogus_error_responses = 1
net.ipv4.tcp_syncookies = 1
net.ipv4.conf.all.log_martians = 1
net.ipv4.conf.default.log_martians = 1
net.ipv4.conf.all.accept_source_route = 0
net.ipv4.conf.default.accept_source_route = 0
net.ipv4.conf.all.rp_filter = 1
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.default.accept_redirects = 0
net.ipv4.conf.all.secure_redirects = 0
net.ipv4.conf.default.secure_redirects = 0
net.ipv4.ip_forward = 0
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
kernel.exec-shield = 1
kernel.randomize_va_space = 1
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.router_solicitations = 0
net.ipv6.conf.default.accept_ra_rtr_pref = 0
net.ipv6.conf.default.accept_ra_pinfo = 0
net.ipv6.conf.default.accept_ra_defrtr = 0
net.ipv6.conf.default.autoconf = 0
net.ipv6.conf.default.dad_transmits = 0
net.ipv6.conf.default.max_addresses = 1
fs.file-max = 65535
kernel.pid_max = 65536
net.ipv4.ip_local_port_range = 2000 65000
net.ipv4.tcp_rmem = 4096 87380 8388608
net.ipv4.tcp_wmem = 4096 87380 8388608
net.ipv4.tcp_window_scaling = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_fin_timeout = 1
net.core.rmem_max = 16777216
net.core.rmem_default = 16777216
net.core.netdev_max_backlog = 262144
net.core.somaxconn = 262144
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_orphans = 262144
net.ipv4.tcp_max_syn_backlog = 262144
net.ipv4.tcp_max_tw_buckets = 5000
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 2
fs.file-max =165535
net.ipv4.ip_local_port_range = 102465535
kernel.panic = 5 一般负载 = CPU个数 * 核数 只有不超这个数就算是在正常范围,看你发的htop里CPU占用也正常
回复 4# 的帖子
可是有時CPU會升到10幾甚至20幾.... 然後網站就整個變慢 :Q想問一下, 升級 Nginx 1.0.13 會不會有幫助?(爬您的文是不建議)
另外究竟 8核CPU + 12GB RAM 該設多少 max_children ? 網路上有的說 200, 有的說 300, 我目前調到100也可以運作... 反而設太高時 CPU 馬上就上升! 我忽然發現, 我沒有裝memcached !
然後在 /var/log/php-fpm.log 裡出現很多錯誤 =>
Mar 09 10:35:34.397744 fpm_php_trace(), line 139: about to trace 9639
Mar 09 10:35:34.397805 fpm_php_trace(), line 167: finished trace of 9639
Mar 09 10:35:34.485881 fpm_request_check_timed_out(), line 146: child 9621, script '/www/www.linsanity.tw/htdocs/obj.php' (pool default) executing too slow (6.335522 sec), logging
Mar 09 10:35:34.485940 fpm_got_signal(), line 48: received SIGCHLD
Mar 09 10:35:34.485959 fpm_children_bury(), line 194: child 9621 stopped for tracing
Mar 09 10:35:34.485965 fpm_php_trace(), line 139: about to trace 9621
Mar 09 10:35:34.486051 fpm_php_trace(), line 167: finished trace of 9621
Mar 09 10:36:47.014197 fpm_request_check_timed_out(), line 146: child 9606, script '/www/www.linsanity.tw/htdocs/p/list.php' (pool default) executing too slow (5.041801 sec), logging
1395609 Mar 09 10:36:47.014257 fpm_request_check_timed_out(), line 146: child 9558, script '/www/www.linsanity.tw/htdocs/p/news.php' (pool default) executing too slow (5.095562 sec), logging
Mar 09 10:36:47.014298 fpm_got_signal(), line 48: received SIGCHLD
Mar 09 10:36:47.014325 fpm_children_bury(), line 194: child 9558 stopped for tracing
Mar 09 10:36:47.014332 fpm_php_trace(), line 139: about to trace 9558
Mar 09 10:36:47.014426 fpm_php_trace(), line 167: finished trace of 9558
Mar 09 10:36:47.014439 fpm_children_bury(), line 194: child 9606 stopped for tracing
Mar 09 10:36:47.014445 fpm_php_trace(), line 139: about to trace 9606
Mar 09 10:36:47.014522 fpm_php_trace(), line 167: finished trace of 9606
Mar 09 10:36:47.014542 fpm_got_signal(), line 48: received SIGCHLD
Mar 09 10:36:47.276197 fpm_request_check_timed_out(), line 146: child 9573, script '/www/www.linsanity.tw/htdocs/p/seo.php' (pool default) executing too slow (5.076616 sec), logging
Mar 09 10:36:47.577429 fpm_got_signal(), line 48: received SIGCHLD
那需要補裝嗎?還是可以直接在 php.ini 裡停用!
; Directory in which the loadable extensions (modules) reside.
501 extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/"
502 extension = "memcache.so"<= 停用這個?
503 extension = "pdo_mysql.so" 按 https://bbs.vpser.net/thread-1654-1-1.html 这个调整试试
里面/www/www.linsanity.tw/htdocs/p/list.php 这样的几个类似的文件貌似执行很慢
也可以试试将nginx.conf里的
fastcgi_connect_timeout
fastcgi_send_timeout
fastcgi_read_timeout都调大一点。 我早已修改 /etc/security/limits.conf
把值都改成 65535, 但沒重啟主機, 這樣會生效嗎?
另外 nginx.conf 裡面已經都設為 300, 如下:
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
請問這樣還不夠嗎?^_^
页:
[1]