linux中php-cgi佔用cpu 100%

來源:互聯網
上載者:User


zijidelu 網站日誌目錄/home/hosts_log
記錄檔有對應的網站id

zijidelu php-cgi日誌目錄 /usr/local/php_fcgi/logs
php-fpm.log
slow.log

查看記錄檔基本上就可以解決問題了,我這邊客戶網站是被入侵 上傳了一個php ddos檔案,當然如果不是此問題可以如下查看是不是程式問題有時使用 file_get_contents函數也可能導致cpu 100%哦


在 php.ini 中,有一個參數 max_execution_time 可以設定 PHP 指令碼的最大執行時間,但是,在 php-cgi(php-fpm) 中,該參數不會起效。真正能夠控制 PHP 指令碼最大執行時間的是 php-fpm.conf 設定檔中的以下參數:


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> 
  預設值為 0 秒,也就是說,PHP 指令碼會一直執行下去。這樣,當所有的 php-cgi 進程都卡在 file_get_contents() 函數時,這台 Nginx+PHP 的 WebServer 已經無法再處理新的 PHP 請求了,Nginx 將給使用者返回“502 Bad Gateway”。修改該參數,設定一個 PHP 指令碼最大執行時間是必要的,但是,治標不治本。例如改成 <value name="request_terminate_timeout">30s</value>,如果發生 file_get_contents() 擷取網頁內容較慢的情況,這就意味著 150 個 php-cgi 進程,每秒鐘只能處理 5 個請求,WebServer 同樣很難避免“502 Bad Gateway”。

要做到徹底解決,只能讓 PHP 程式員們改掉直接使用 file_get_contents("http://example.com/") 的習慣,而是稍微修改一下,加個逾時時間,用以下方式來實現 HTTP GET 請求。要是覺得麻煩,可以自行將以下代碼封裝成一個函數。

<?php 
$ctx = stream_context_create(array( 
   'http' => array( 
       'timeout' => 1 //設定一個逾時時間,單位為秒 
       ) 
   ) 
); 
file_get_contents("http://www.111cn.net/", 0, $ctx); 
?> 

當然相如果像dedecms產生html頁面時cpu也會100%的情況哦

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.