Nginx

Nginx+PHP-FPM+APC+Varnish+Memcached:Mem/Swap消耗高?

  • December 30, 2011

我在 Linode 上執行了一個具有 1024 MB RAM、Ubuntu 11.04 和 Nginx+PHP-FPM+APC Varnish+Memcached 的 VPS。我用 WordPress 3.2.1 設置了幾個測試部落格,一切正常,性能測試看起來很有希望:

# ab -n 1000 -c 100 http://mydomain.com/
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking mydomain.com (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests


Server Software:        nginx
Server Hostname:        mydomain.com
Server Port:            80

Document Path:          /
Document Length:        5677 bytes

Concurrency Level:      100
Time taken for tests:   0.186 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Total transferred:      5989000 bytes
HTML transferred:       5677000 bytes
Requests per second:    5376.14 [#/sec] (mean)
Time per request:       18.601 [ms] (mean)
Time per request:       0.186 [ms] (mean, across all concurrent requests)
Transfer rate:          31443.08 [Kbytes/sec] received

Connection Times (ms)
             min  mean[+/-sd] median   max
Connect:        6    9   0.9      8      13
Processing:     4    9   1.3      9      14
Waiting:        1    5   2.5      5      13
Total:         12   18   1.6     18      22
WARNING: The median and mean for the initial connection time are not within a normal deviation
       These results are probably not that reliable.

Percentage of the requests served within a certain time (ms)
 50%     18
 66%     19
 75%     19
 80%     19
 90%     20
 95%     21
 98%     22
 99%     22
100%     22 (longest request)

安裝 Memcached 和 php5-memcache 包並啟用 WP Memcache 外掛後,我可以看到 memcache 伺服器已啟動並執行,命中率約為 50%。memcache 服務使用 64 MB 記憶體:

# ps xa |grep memcached
12259 ?        Sl     0:01 /usr/bin/memcached -m 64 -p 11211 -u memcache -l 127.0.0.1

不過,我可以看到的是主記憶體和交換的高消耗,我想知道這是否正常。伺服器大部分時間都是空閒的(測試部落格正在執行,但現在沒有人關心它們,所以我是唯一的訪問者),所以我不明白為什麼記憶體和交換這麼高。

#free -m
            total       used       free     shared    buffers     cached
Mem:           927        906         21          0          6         58
-/+ buffers/cache:        840         87
Swap:          255        255          0

有任何想法嗎?

更新:似乎 Memcached 不是原因。我已經停止了伺服器,該站點當然正在執行,但記憶體/交換消耗仍然幾乎 100%。所以應該有問題,可能是PHP-FPM或Varnish?

更新 2:似乎 PHP-FPM 是問題,看看這個(感謝命令,Tomtezky)

# ps aux --sort=-rss | head -15
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
www-data 12414  0.0  9.6 247840 91772 ?        S    01:25   0:06 php-fpm: pool www                                      
www-data 12405  0.0  9.5 244012 91212 ?        S    01:25   0:06 php-fpm: pool www                                      
www-data 12418  0.0  9.0 241820 86132 ?        S    01:25   0:05 php-fpm: pool www                                      
www-data 12417  0.0  8.5 236724 81500 ?        S    01:25   0:07 php-fpm: pool www                                      
www-data 12409  0.0  8.2 241980 78784 ?        S    01:25   0:05 php-fpm: pool www                                      
www-data 12407  0.0  8.1 236968 77880 ?        S    01:25   0:05 php-fpm: pool www                                      
www-data 12400  0.0  8.1 234344 77860 ?        S    01:25   0:06 php-fpm: pool www                                      
www-data 12408  0.0  8.1 237120 77700 ?        S    01:25   0:06 php-fpm: pool www                                      
www-data 12410  0.0  8.0 232248 76224 ?        S    01:25   0:06 php-fpm: pool www                                      
www-data 12413  0.0  7.6 242092 72452 ?        S    01:25   0:07 php-fpm: pool www                                      
www-data 12416  0.0  7.1 241812 68148 ?        S    01:25   0:05 php-fpm: pool www                                      
www-data 12412  0.0  7.0 220684 66644 ?        S    01:25   0:04 php-fpm: pool www                                      
www-data 12402  0.0  6.9 241556 66156 ?        S    01:25   0:05 php-fpm: pool www                                      
www-data 12419  0.0  6.2 238016 59304 ?        S    01:25   0:04 php-fpm: pool www

看到了……如何減少 PHP-FPM 的記憶體使用?

它不是 PHP-FPM,它是 PHP-FPM 中載入的 Wordpress 實例。限制 php-fpm worker 的數量,或者減少你的 wordpress 安裝的記憶體佔用。

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