Win2003 using Preg_match_all to lead to Apache crash resolution _php Tutorial

Source: Internet
Author: User
This article to introduce to you in Win2003 use Preg_match_all lead to Apache crash solution, have encountered this kind of problem friend can enter reference.

apache/2.2.9 (WIN32) + php/5.2.17, in the use of regular expressions Preg_match_all (such as Preg_match_all ("/ni" (. *?) wo/", $html, $matches);) perform analysis to match long string $html (greater than 100,000 bytes, commonly used to analyze the collected Web page source), the Apache server will crash automatically restart.

There is a hint in the Apache error log:

[Thu APR 11 18:31:31 2013] [Notice] Parent:child process exited with status--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, worker threads.
[Thu APR 11 18:31:31 2013] [Notice] Child 2964:listening on port 80.

After reviewing the official Apache and the Forum information, found that the win platform with regular preg_match_all or preg_match analysis of a longer string, the cause of the Apache crash Restart is the default allocated thread stack space under the Windows platform Threadstacksize is too small to cause. Win32 default is only 256KB, and in Linux the default value is 8M, which is why the same program is normal under the Linux platform, and under the win platform is not normal reason.

According to the official description of the Pcre library, the size of the stack space corresponding to the pcre.recursion_limit should not exceed 524.
Here are a table of safe values of pcre.recursion_limit for a variety of executable stack sizes:
The following is a recommended security value for StackSize and Pcre.recursion_limit, which is most likely to occur when a stack overflow is exceeded, Apache crash:

Stacksize Pcre.recursion_limit
134217 MB
67108 MB
33554 MB
8 MB 16777
4 MB 8388
2 MB 4194
1 MB 2097
1048 KB
524 KB
If you do not resize the stack, you must start with a regular PHP page:

Ini_set ("Pcre.recursion_limit", "524"); PHP default is 100,000.
?>

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

The code is as follows Copy 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_er ROR ';
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);
}

For the regular modifier IsU Description:
I: Denotes in-casesensitive, which is case insensitive
A s:pcre_dotall that indicates that the dot number can match the newline character.
U: denotes pcre_ungreedy, which means non-greedy, equivalent to Perl/python language. *, during the matching process, for. * Regular, a match is executed immediately instead of waiting. * Search all characters and return one by one
When using regular expressions, we should try to avoid recursive calls, and recursion can lead to stack overflow. Like what:
/<> < p="">


So how to increase the threadstacksize of the win platform under the size of it? Enable "Include conf/extra/httpd-mpm.conf" (delete previous comment #) in Apache config file httpd.conf, then mpm_winnt_module in httpd-mpm.conf file Set "Threadstacksize 8400000" in the configuration module (approximately 8M).

copy code



T Hreadstacksize 8400000
Threadsperchild
Maxrequestsperchild 10000
Win32disableacceptex

It is important to note that the 32-bit Apache program can only use up to approximately 2GB of memory space! Therefore, after multiplying the value of Threadstacksize and Threadsperchild (8M * 200) should not exceed 2G, otherwise it will not start Apache, the error log appears as follows:
[Thu APR 11 20:02:45 2013] [Crit] (OS 8) There is not enough storage space to process this command. : Child 4832: _beginthreadex failed. Unable to create all worker threads. Created 212 of the threads requested with the Threadsperchild configuration directive.
Through the above tips, float can tell everyone is on my server, when the thread stack size is set to 8M, I can set the maximum number of threads is 212.

http://www.bkjia.com/PHPjc/632107.html www.bkjia.com true http://www.bkjia.com/PHPjc/632107.html techarticle this article to introduce to you in Win2003 use Preg_match_all lead to Apache crash solution, have encountered this kind of problem friend can enter reference. apache/2.2.9 (WIN32) + php/5.2.17, in ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.