Solution to apache crash caused by using preg_match_all in win2003_PHP tutorial

WBOY
Release: 2016-07-13 10:57:12
Original
1101 people have browsed it

This article will introduce to you the solution to the crash of apache caused by using preg_match_all in win2003. Friends who have encountered such problems can refer to it for reference.

Apache/2.2.9 (Win32) + PHP/5.2.17, using the regular expression preg_match_all (such as preg_match_all("/ni(.*?)wo/", $html, $matches);) for analysis When matching the relatively long string $html (larger than 100,000 bytes, generally used to analyze the collected web page source code), the Apache server will crash and automatically restart.

There is this prompt in the Apache error log:

[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.

After consulting Apache official and forum information, I found that when using the regular preg_match_all or preg_match to analyze relatively long strings under the win platform, the cause of apache crash and restart is that the thread stack space ThreadStackSize allocated by default under the windows platform is too small. . The default value of win32 is only 256KB, while the default value under Linux is 8M. This is why the same program works normally under the Linux platform but not under the Win platform.

According to the official description of PCRE library: the pcre.recursion_limit size corresponding to 256 KB of stack space should not exceed 524.
Here is a table of safe values ​​of pcre.recursion_limit for a variety of executable stack sizes:
The following is a recommended safety value corresponding to Stacksize and pcre.recursion_limit. If this value is exceeded, stack overflow is very likely to occur. 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
If you have not adjusted the stack size, you must add:

at the beginning of the PHP page using regular expressions

ini_set("pcre.recursion_limit", "524"); // PHP default is 100,000.
?>

To view specific errors, you can use the following code:

 代码如下 复制代码
$resultsArray = preg_match_all("/table.*?/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);
}

Description of the regular modifier isU:
i: means in-casesensitive, that is, case-insensitive
s: PCRE_DOTALL, indicating that the dot can match newline characters.
U: means PCRE_UNGREEDY, which means non-greedy, equivalent to .*? in perl/python language. During the matching process, for the .* regular expression, it will be executed immediately as soon as there is a match, instead of waiting for .* to search all the characters and then return them one by one. 🎜> When using regular expressions, we should try to avoid recursive calls, which can easily lead to stack overflow. For example:
//isU will cause an error, but using //i will be normal.

So how to increase the size of ThreadStackSize under the win platform? Enable "Include conf/extra/httpd-mpm.conf" in the apache configuration file httpd.conf (delete the previous comment #), and then set "ThreadStackSize 8400000" in the mpm_winnt_module configuration module in the httpd-mpm.conf file. Yes (about 8M).

ThreadsPerChild 200 MaxRequestsPerChild 10000
The code is as follows Copy code
 代码如下 复制代码



    ThreadStackSize 8400000
    ThreadsPerChild      200
    MaxRequestsPerChild    10000
    Win32DisableAcceptEx



ThreadStackSize 8400000
Win32DisableAcceptEx

It should be noted here that the 32-bit Apache program can only use up to about 2GB of memory space! Therefore, the value of ThreadStackSize and ThreadsPerChild multiplied (8M * 200) should not exceed 2G, otherwise apache cannot be started, and the error log that appears is as follows: [Thu Apr 11 20:02:45 2013] [crit] (OS 8) Insufficient storage space to process this command. : Child 4832: _beginthreadex failed. Unable to create all worker threads. Created 212 of the 220 threads requested with the ThreadsPerChild configuration directive. Through the above tips, Piaoyi can tell you that on my server, when the thread stack size is set to 8M, the maximum number of threads I can set is 212.
http://www.bkjia.com/PHPjc/632107.htmlwww.bkjia.com
truehttp: //www.bkjia.com/PHPjc/632107.htmlTechArticleThis article will introduce to you the solution to the apache crash caused by using preg_match_all in win2003. Friends who have encountered such problems References can be entered. Apache/2.2.9 (Win32) + PHP/5.2.17, in...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template