win2003下PHP使用preg_match_all導致apache崩潰問題的解決方案_Linux

來源:互聯網
上載者:User

小編的平台是windows server 2003(32位系統) + Apache/2.2.9 (Win32) + PHP/5.2.17,在使用Regex preg_match_all (如 preg_match_all("/ni(.*?)wo/", $html, $matches);)進行分析匹配比較長的字串 $html 時(大於10萬位元組,一般用於分析採集回來的網頁源碼),Apache伺服器會崩潰自動重啟。
    在Apache錯誤記錄檔裡有這樣的提示:

複製代碼 代碼如下:
[Thu Apr 11 18:31:31 2013] [notice] Parent: child process exited with status 128 -- Restarting.
[Thu Apr 11 18:31:31 2013] [notice] Apache/2.2.9 (Win32) PHP/5.2.17 configured -- resuming normal operations
[Thu Apr 11 18:31:31 2013] [notice] Server built: Jun 13 2008 04:04:59
[Thu Apr 11 18:31:31 2013] [notice] Parent: Created child process 2964
[Thu Apr 11 18:31:31 2013] [notice] Disabled use of AcceptEx() WinSock2 API
[Thu Apr 11 18:31:31 2013] [notice] Child 2964: Child process is running
[Thu Apr 11 18:31:31 2013] [notice] Child 2964: Acquired the start mutex.
[Thu Apr 11 18:31:31 2013] [notice] Child 2964: Starting 350 worker threads.
[Thu Apr 11 18:31:31 2013] [notice] Child 2964: Listening on port 80.

    經過查閱Apache官方以及論壇資料後,發現win平台下用正則 preg_match_all 或preg_match 分析比較長的字串時,導致apache崩潰重啟的原因是windows平台下預設分配的線程堆棧空間 ThreadStackSize 太小導致的。 win32預設只有256KB,而在 linux下預設值是 8M,這就是為什麼同樣的程式在 linux平台下正常,而在 win平台下不正常的原因。
    根據PCRE library的官方說明:256 KB 的堆棧空間對應的pcre.recursion_limit大小應該不超過524。
Here is a table of safe values of pcre.recursion_limit for a variety of executable stack sizes:
下面就是一張Stacksize和pcre.recursion_limit對應的建議安全值,超過這個數值就極有可能發生堆疊溢位,apache crash:
複製代碼 代碼如下:
Stacksize   pcre.recursion_limit
 64 MB      134217
 32 MB      67108
 16 MB      33554
  8 MB      16777
  4 MB      8388
  2 MB      4194
  1 MB      2097
512 KB      1048
256 KB      524

如果你沒有調整堆棧大小,就必須在使用正則的PHP頁面最開頭加入:

複製代碼 代碼如下:
<?php
ini_set("pcre.recursion_limit", "524"); // PHP default is 100,000.
?>

查看具體的錯誤可以使用下面的代碼:

複製代碼 代碼如下:
$resultsArray = preg_match_all("/table.*?<a>/isU", $html, $contents);
if ($resultsArray === 0){
echo get_pcre_err();
}
function get_pcre_err(){
        $pcre_err = preg_last_error();  // PHP 5.2 and above.
        if ($pcre_err === PREG_NO_ERROR) {
            $msg = 'Successful non-match.';
        } else {
            // preg_match error!
            switch ($pcre_err) {
                case PREG_INTERNAL_ERROR:
                    $msg = 'PREG_INTERNAL_ERROR';
                    break;
                case PREG_BACKTRACK_LIMIT_ERROR:
                    $msg = 'PREG_BACKTRACK_LIMIT_ERROR';
                    break;
                case PREG_RECURSION_LIMIT_ERROR:
                    $msg = 'PREG_RECURSION_LIMIT_ERROR';
                    break;
                case PREG_BAD_UTF8_ERROR:
                    $msg = 'PREG_BAD_UTF8_ERROR';
                    break;
                case PREG_BAD_UTF8_OFFSET_ERROR:
                    $msg = 'PREG_BAD_UTF8_OFFSET_ERROR';
                    break;
                default:
                    $msg = 'Unrecognized PREG error';
                    break;
            }
        }
    return($msg);
}

對於正則的修飾符 isU 說明:

複製代碼 代碼如下:
i: 表示in-casesensitive,即大小寫不敏感
s: PCRE_DOTALL,表示點號可以匹配分行符號。
U: 表示PCRE_UNGREEDY,表示非貪婪,相當於perl/python語言的.*?,在匹配過程中,對於.*正則,一有匹配立即執行,而不是等.*搜尋了所有字元再一一返回

    在使用Regex時,我們應該盡量避免遞迴調用,遞迴容易導致堆疊溢位。比如:
/<table((?!<table).)*?<\/a>/isU 就會發生錯誤,而使用 /<table.*?<\/a>/i 就正常。

    那麼如何增加win平台下 ThreadStackSize 的大小呢? 在apache的設定檔 httpd.conf 裡啟用 “Include conf/extra/httpd-mpm.conf”(刪除前面的注釋#),然後在 httpd-mpm.conf 檔案裡的 mpm_winnt_module 配置模組裡設定 “ThreadStackSize 8400000”即可(大約8M)。

複製代碼 代碼如下:
<IfModule mpm_winnt_module>
    ThreadStackSize 8400000
    ThreadsPerChild      200
    MaxRequestsPerChild    10000
    Win32DisableAcceptEx
</IfModule>

    這裡需要注意的是,32位的Apache程式只能最多使用大約2GB記憶體空間! 因此,ThreadStackSize 和ThreadsPerChild 的值相乘後(8M * 200)不應該超過2G,否則無法啟動apache,出現的錯誤記錄檔如下:
複製代碼 代碼如下:
[Thu Apr 11 20:02:45 2013] [crit] (OS 8)儲存空間不足,無法處理此命令。  : Child 4832: _beginthreadex failed. Unable to create all worker threads. Created 212 of the 220 threads requested with the ThreadsPerChild configuration directive.

    通過上面的提示,小編可以告訴大家的是在我的這台伺服器上,當線程堆棧大小設為8M時,我可以設定的線程數最多是212個。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.