vps 負載過高,博客無法訪問

最近晚上要整理整理博客, 發現博客經常無法打開。 查看 vps 進程發現 php5-fpm 進程佔用了 CPU 的大部分時間,於是去網上搜了一圈,改了改配置文件,現在 CPU 使用率基本在 20% 以下,各服務都算正常,順手做一個總結。

  1. 首先查看 /var/log/php5-fpm.log 文件,判斷大概是什麼問題。我的日誌當時一直報這個 warning:

    WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 32 children, there are 0 idle, and 10 total children
    WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 32 children, there are 0 idle, and 11 total children
    WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 32 children, there are 0 idle, and 12 total children
  2. 在 stackoverflow 上找到了一個帖子。推測是 php5-fpm 配置不當引起的。修改 php5-fpm 配置 /etc/php5/fpm/pool.d/www.conf:

    listen = /dev/shm/php5-fpm.sock
    ;用內存文件作 unix socket, 對應的 /etc/nginx/sites-available/wordpress
    ;文件也要配置:fastcgi_pass unix:/dev/shm/php5-fpm.sock;
    prefix = /var ;這個是日誌文件的路徑前綴,以前沒配置~
    pm = dynamic
    pm.max_children = 20
    pm.start_servers = 5
    pm.min_spare_servers = 3
    pm.max_spare_servers = 7
    pm.max_requests = 100
    request_slowlog_timeout = 5s
    access.log = /var/log/$pool.access.log
    access.format = "%R - %u %t \"%m %r%Q%q\" %s %f %{mili}d %{kilo}M %C%%"
    slowlog = log/$pool.log.slow
    request_terminate_timeout = 60 ; 默認單位是 seconds

    配置完記得重啟 php5-fpm 服務。
    中間幾個數值的配置可以參考上面說的那篇帖子,其它的配置看配置文件中的說明就好了。這些數值的配置可能需要不斷嘗試才能達到理想的效果,後面有機會再優化一下吧。

更新
這幾天發現vps的CPU使用率又升到 90% 以上了,感覺並不是 php5-fpm 配置的問題。查看 nginx 日誌發現有世界各地的 ip 在對我的 wordpress 發動 xmlrpc 攻擊。去網上搜了一下解決辦法,要麼把xmlrpc.php 文件刪除或者改名,要麼在 wordpress 主題的 functions.php 中添加代碼:

/* turn off ping back */
all_filter('xmlrpc_methods', 'remove_xmlrpc_pingback_ping');
function remove_xmlrpc_pingback_ping( $methods ) {
    unset( $methods['pingback.ping']);
    return $methods;
}

或者

/* turn off xmlrpc */
add_filter('xmlrpc_enabled', '__return_false');

但是,我這樣做後並沒有什麼效果。而且我用第三方 wordpress 客戶端進行寫作,關閉 xmlrpc 會導致第三方客戶端無法連接到 wordpress,也就無法用客戶端發佈文章,所以我並沒有採用上面的方法。後面嘗試安裝了 Login Security SolutionminiOrange Login Security 插件,配置好後 vps 的 CPU 使用率降到了1%以下。問題暫時解決了,後面有問題再更。

第二次更新
後來發現 vps cpu佔用率過高其實是 post xmlrpc ddos 引起的,上面的方法其實不一定有用,最簡單也最有效的辦法是屏蔽IP。通過查看 /var/log/nginx/access.log 文件發現異常IP,然後用 iptables 直接屏蔽即可。

發表評論

郵箱地址不會被公開。 必填項已用*標註