Linux php-cgi accounted for 100% of the memory solution

  

When the Linux system is in use, the computer becomes very card, found that the php-cgi process is occupying cpu, the occupancy rate has reached 100%, what is it? What causes this situation? The following small series will introduce you to the Linux system php-cgi takes up 100% of the solution.

Web logs directory /home /hosts_log

log file has a corresponding website id

php-cgi log directory /usr /local /php_fcgi /logs

php-fpm.log

slow.log

Viewing the log file basically solves the problem, the website is hacked and uploaded a php ddos ​​file, of course, if this is not the problem You can check if it is a program problem. Sometimes using the file_get_contents function may also cause the cpu to be 100%.

In php.ini, there is a parameter max_execution_time that sets the maximum execution time of a PHP script, but in php-cgi(php-fpm) this parameter won't work. The only parameter in the php-fpm.conf configuration file that really controls the maximum execution time of a PHP script is:

The timeout (in seconds) for serving a single request after which the worker process will be terminated

Should be used when ‘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. Thus, when all php-cgi processes are stuck in the file_get_contents() function, the Nginx+PHP WebServer can no longer process new PHP requests, and Nginx will return "502 Bad Gateway" to the user. Modifying this parameter and setting a maximum execution time for a PHP script is necessary, but the palliative is not a cure. For example, change to "value name=“request_terminate_timeout”"30s "/value". If file_get_contents() occurs to get slower web content, this means 150 php-cgi processes, which can only process 5 per second. Requests, WebServer is also difficult to avoid < 502 Bad Gateway”.

To be completely solved, you can only change the habit of directly using file_get_contents, but modify it a bit, add a timeout, and implement HTTP GET request in the following way. If you feel trouble, you can package the following code into 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 the html page is generated like dedecms, the cpu will also be 100%.

Linux php-cgi process takes up 100% of the solution to the cpu introduced here, there is a possibility of poisoning, which requires anti-virus treatment.

Copyright © Windows knowledge All Rights Reserved