Win2003 the solution to the Apache crash problem with PHP using Preg_match_all _linux

Source: Internet
Author: User

The platform for the small weave is the Windows Server 2003 (32-bit system) + apache/2.2.9 (Win32) + php/5.2.17, which uses regular expression preg_match_all (such as Preg_match_all ("/ni (. *?) wo/", $html, $matches); When the analysis matches the longer string $html (more than 100,000 bytes, typically used to analyze the source code of the collected Web page), the Apache server crashes and restarts automatically.
There are hints in the Apache error log:

Copy Code code as follows:
[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 threads worker.
[Thu APR 11 18:31:31 2013] [Notice] Child 2964:listening on port 80.

After consulting the Apache and the Forum data, found that the win platform with regular preg_match_all or preg_match analysis of the longer string, causing the Apache crash restart because the Windows platform under the default allocated thread stack space Threadstacksize is too small to cause. Win32 default is only 256KB, and in Linux, the default is 8M, which is why the same program under the Linux platform Normal, and in the win platform under the abnormal reasons.
According to the official note of the Pcre library, the 256 KB stack space should have a pcre.recursion_limit size of not more than 524.
This is table A, 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 the stack overflows, Apache crash:
Copy Code code as follows:
StackSize Pcre.recursion_limit
MB 134217
MB 67108
MB 33554
8 MB 16777
4 MB 8388
2 MB 4194
1 MB 2097
1048 MB
256 KB 524

If you do not adjust the stack size, you must start with the regular PHP page:

Copy Code code as follows:
<?php
Ini_set ("Pcre.recursion_limit", "524"); PHP default is 100,000.
?>

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

Copy Code code as follows:
$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);
}

For a regular modifier IsU description:

Copy Code code as follows:
I: In-casesensitive, that is, not sensitive to case
S:pcre_dotall, which indicates the point number can be used to match line breaks.
U: pcre_ungreedy, means not greedy, equivalent to Perl/python language. *, in the matching process, for. * Regular, one has a match to execute immediately, not wait. * searched all characters again one by one return

When using regular expressions, we should try to avoid recursive calls, which can easily lead to stack overflows. Like what:
/<table (?! <table).) *?<\/a>/isu will be wrong, and the use of/<table.*?<\/a>/i is normal.

So how to increase the size of the threadstacksize under the win platform? In the Apache configuration file httpd.conf, enable "Include conf/extra/httpd-mpm.conf" (delete the previous comment #), and then in the httpd-mpm.conf file Mpm_winnt_module "Threadstacksize 8400000" is set in the configuration module (approx. 8M).

Copy Code code as follows:
<ifmodule mpm_winnt_module>
Threadstacksize 8400000
Threadsperchild 200
Maxrequestsperchild 10000
Win32disableacceptex
</IfModule>

Note here that the 32-bit Apache program can only use about 2GB of memory space! Therefore, after multiplying the value of Threadstacksize and Threadsperchild (8M * 200) should not exceed 2G, or you cannot start Apache, the error log appears as follows:
Copy Code code 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.

Through the above prompts, small series can tell you that on my server, when the thread stack size is set to 8M, I can set the maximum number of threads is 212.

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.