Apache-2.2

如何解決 Apache 和 mySQL 的大記憶體問題

  • August 21, 2015

我已經閱讀了很多這個網站來優化我的伺服器,但沒有什麼能真正幫助我:(我認為我有記憶體問題,我只有一個網站(Drupal),每月的頁面瀏覽量為 860K,但是當流量增加時,負載平均上升超過 +40、+70 等,100% 的記憶體使用導致伺服器完全停機

實際上,當我剛剛重新啟動伺服器時,總記憶體使用量約為 80%

我不知道該怎麼辦 !我真的不敢相信這個伺服器不能處理這種流量,請幫助我!

眼鏡

Processor #1 to #24 
Intel Dual Xeon E5645 @ 2.40GHz
Cache 12288 KB
4GB Total RAM
Apache/2.2.19 -prefork- (Unix) mod_ssl/2.2.19 OpenSSL/0.9.8e-fips-rhel5 mod_auth_passthrough/2.1 mod_bwlimited/1.4  PHP/5.2.17
500GB HD RAID 1
Drupal based website with Boost module and Cache Router (INNODB tables)
APC Installed

頂部(移位 - 米)

top - 23:05:37 up 19:42,  1 user,  load average: 0.78, 0.74, 0.64
Tasks: 527 total,   1 running, 524 sleeping,   0 stopped,   2 zombie
Cpu(s):  1.7%us,  0.3%sy,  0.0%ni, 97.9%id,  0.1%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   4034276k total,  3774668k used,   259608k free,   279060k buffers
Swap:  6088624k total,   103616k used,  5985008k free,  1316080k cached

 PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                                       
8768 mysql     15   0 1211m 880m 4784 S 12.0 22.4  20:43.44 mysqld                                        
20475 nobody    16   0  429m 176m  27m S  0.0  4.5   0:00.44 httpd                                         
20846 nobody    15   0  427m 176m  28m S  1.0  4.5   0:01.13 httpd                                         
20775 nobody    15   0  422m 171m  29m S  0.0  4.4   0:01.22 httpd                                         
20826 nobody    15   0  422m 171m  29m S  0.7  4.4   0:01.00 httpd                                         
20827 nobody    15   0  423m 171m  28m S  0.7  4.4   0:00.61 httpd                                         
20578 nobody    15   0  422m 171m  29m S  0.0  4.3   0:01.73 httpd                                         
20833 nobody    15   0  422m 170m  28m S  0.0  4.3   0:00.84 httpd                                         
20830 nobody    15   0  421m 170m  28m S  0.0  4.3   0:00.84 httpd                                         
20681 nobody    15   0  422m 170m  28m S  1.0  4.3   0:00.93 httpd                                         
20913 nobody    15   0  422m 170m  27m S  0.0  4.3   0:00.34 httpd                                         
20914 nobody    15   0  422m 169m  27m S  0.0  4.3   0:00.60 httpd                                         
20854 nobody    15   0  423m 167m  23m S  0.0  4.2   0:00.36 httpd                                         
20911 nobody    16   0  418m 167m  28m S  0.3  4.2   0:00.70 httpd 

httpd.conf

Timeout 300
TraceEnable On
ServerSignature Off
ServerTokens Full
FileETag All
StartServers 5
<IfModule prefork.c>
MinSpareServers 5
MaxSpareServers 10
</IfModule>
ServerLimit 256
MaxClients 150
MaxRequestsPerChild 800
KeepAlive On
KeepAliveTimeout 5
MaxKeepAliveRequests 100

我的.cnf

[mysqld]
max_connections = 120
safe-show-database
skip-locking
key_buffer = 148M
max_allowed_packet = 14M
table_cache = 596
sort_buffer_size = 2M
read_buffer_size = 2M
read_rnd_buffer_size = 2M
myisam_sort_buffer_size = 64M
thread_cache_size = 24
query_cache_size= 128M
thread_concurrency = 48
wait_timeout = 45
innodb_file_per_table
innodb_log_file_size = 10485760
open_files_limit = 8192
tmp_table_size=200M
max_heap_table_size=200M
innodb_buffer_pool_size=596M
local-infile=1
log_slow_queries = /var/log/slow.log
long_query_time = 3

[mysqldump]
quick
max_allowed_packet = 16M

[mysqld_safe]
log-error=/var/log/mysqld.log

[mysql]
no-auto-rehash

[isamchk]
key_buffer = 128M
sort_buffer_size = 64M
read_buffer = 2M
write_buffer = 2M

[myisamchk]
key_buffer = 128M
sort_buffer_size = 64M
read_buffer = 2M
write_buffer = 2M

一些圖表(本週)

更新:

伺服器負載超過 200 的頂部

top - 12:27:13 up 5 days,  9:04,  1 user,  load average: 219.36, 189.93, 130.56
Tasks: 750 total,   1 running, 749 sleeping,   0 stopped,   0 zombie
Cpu(s):  1.3%us,  1.0%sy,  0.1%ni, 49.7%id, 47.8%wa,  0.0%hi,  0.1%si,  0.0%st
Mem:   4034276k total,  4014052k used,    20224k free,    13404k buffers
Swap:  6088624k total,  3036872k used,  3051752k free,    71272k cached

 PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                                       
15653 mysql     15   0 1444m 154m 3384 S  0.0  3.9 192:42.76 mysqld                                        
23800 nobody    16   0  422m  96m  29m D  0.3  2.4   0:06.96 httpd                                         
23527 nobody    16   0  422m  93m  29m D  0.0  2.4   0:05.11 httpd                                         
23759 nobody    15   0  421m  91m  26m S  0.7  2.3   0:03.97 httpd                                         
23842 nobody    16   0  422m  91m  26m D  0.3  2.3   0:04.88 httpd                                         
23819 nobody    16   0  421m  91m  26m D  0.3  2.3   0:07.11 httpd                                         
23739 nobody    16   0  421m  91m  26m D  0.0  2.3   0:10.27 httpd                                         
23778 nobody    15   0  421m  91m  26m S  0.0  2.3   0:04.81 httpd                                         
23790 nobody    15   0  421m  91m  26m S  0.0  2.3   0:03.86 httpd                                         
23754 nobody    16   0  421m  91m  26m D  0.0  2.3   0:08.19 httpd                                         
23700 nobody    16   0  421m  90m  26m D  0.3  2.3   0:05.45 httpd                                         
23843 nobody    16   0  420m  90m  26m S  0.0  2.3   0:06.39 httpd                                         
23510 nobody    16   0  426m  90m  24m D  0.0  2.3   0:04.98 httpd                                         
23841 nobody    16   0  416m  89m  29m D  0.0  2.3   0:03.53 httpd                                         
23836 nobody    15   0  414m  89m  30m S  0.0  2.3   0:05.82 httpd                                         
23849 nobody    15   0  418m  88m  25m S  0.0  2.3   0:05.78 httpd                                         
23833 nobody    16   0  429m  88m  24m D  0.0  2.3   0:05.59 httpd                                         
23832 nobody    16   0  418m  88m  25m S  0.0  2.2   0:09.25 httpd                                         
23746 nobody    16   0  428m  88m  25m D  0.0  2.2   0:04.13 httpd                                         
23851 nobody    16   0  428m  88m  24m D  0.0  2.2   0:03.60 httpd                                         
23816 nobody    15   0  418m  88m  25m S  0.0  2.2   0:07.00 httpd                                         
23282 nobody    15   0  416m  87m  28m S  0.0  2.2   0:11.29 httpd                                         
23742 nobody    15   0  416m  86m  26m S  0.0  2.2   0:09.37 httpd                                         
23837 nobody    16   0  425m  86m  25m D  0.3  2.2   0:05.20 httpd                                         
23093 nobody    16   0  430m  86m  24m D  0.0  2.2   0:04.19 httpd                                         
23732 nobody    16   0  421m  86m  24m D  0.0  2.2   0:05.55 httpd                                         
23772 nobody    15   0  415m  85m  29m S  1.0  2.2   0:14.55 httpd   

有人幫我調整了 Apache 設置,但一切看起來都一樣

我啟用了管道日誌記錄,這應該有助於解決記憶體問題。我還縮短了 apache 程序在通過記憶體循環之前將執行的請求數量。

我將非常感謝您的幫助,我幾乎嘗試了所有方法,我並不是真正的系統管理員,但我們現在沒有任何人可以幫助我們。

謝謝!

猜什麼??

mod_security 模組是記憶體問題的原因,我禁用了它,每個 httpd 程序從 180 到 35mb !

它從一開始就由我的託管服務提供商安裝和配置,現在我需要以其他方式提高安全性或正確配置它。

一般經驗法則 -

當伺服器處於負載狀態時執行此命令:

ps -ylC httpd --sort:rss | awk '{sum+=$8; ++n} END {print "Tot="sum"("n")";print "Avg="sum"/"n"="sum/n/1024"MB"}'

這將告訴您大約 Apache 程序的平均大小。

這不是一個專用的 Web 節點,所以說 60% 的 RAM 可用於 Apache。

4096 * .60 / AVERAGE_SIZE_HTTPD_PROCESS = 大約您可以服務的 MaxClients/請求數。通常看起來您的平均大小約為 170MB,所以…

您一次可以處理 14 個請求。將 MaxClients 減少到更合理的數字,例如 20-25。

乾杯

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