Linux php-cgi consumes 100% of CPU

Source: Internet
Author: User
Tags fpm php script stop script


Zijidelu Web site Log directory/home/hosts_log
The log file has a corresponding site ID

Zijidelu php-cgi Log Directory/usr/local/php_fcgi/logs
Php-fpm.log
Slow.log

Viewing log files Basically solves the problem, my side of the client site is invaded upload a php DDoS file, of course, if not this issue can be viewed as follows is not a program problem sometimes use file_get_contents function may also lead to CPU 100% OH


In PHP.ini, there is a parameter max_execution_time can set the maximum execution time for a PHP script, but in php-cgi (PHP-FPM), the parameter does not work. The real ability to control the maximum execution time of a PHP script is the following parameters in the php-fpm.conf configuration file:


The timeout (in seconds) to serving a single request after the which the worker process would be terminated
Should be used as ' max_execution_time ' ini option does not stop script execution for some reason
' 0s ' means ' off '
<value name= "Request_terminate_timeout" >0s</value>
The default value is 0 seconds, which means that the PHP script will continue to execute. In this way, when all the php-cgi processes are stuck in the file_get_contents () function, the WebServer of this nginx+php is no longer able to process the new PHP request, and Nginx will return "502 bad Gateway" to the user. Modify this parameter to set a PHP script maximum execution time is necessary, but the symptom does not cure the root causes. For example, change to <value name= "Request_terminate_timeout" >30s</value>, if file_get_contents () gets the content of the Web page is slow, which means 150 A php-cgi process that can handle only 5 requests per second, WebServer is also difficult to avoid "502 bad Gateway".

To achieve a thorough solution, PHP programmers can only get rid of the habit of directly using file_get_contents ("http://example.com/"), but modify it slightly, add a time-out, and implement HTTP GET requests in the following ways. If you find yourself in trouble, you can encapsulate the following code as a function.

<?php
$ctx = stream_context_create (Array (
' http ' => array (
' Timeout ' => 1//Set a timeout in seconds
)
)
);
File_get_contents ("http://www.111cn.net/", 0, $ctx);
?>

Of course, if it's like Dedecms generates HTML pages, the CPU will be 100%.

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.