A large number of php-cgi.exe processes on the server lead to 100% CPU consumption. Solution

Time:2021-10-16

Optimize the fastcgi configuration file fcgiext.ini in Windows 2003 + IIS6 to reduce the number and memory size of php-cgi.exe processes

It was said that fastcgi is better than ISAPI, so it was installed in the server. The configuration environment is Windows 2003 + IIS6 + fastcgi (fcgi) + php5.2.17. After a long period of observation, it is found that PHP working in fastcgi mode will occupy more and more memory, with slightly more accesses and n more PHP CGI processes. In the same case, it can be hundreds of m more than when ISAPI mode was used, My server memory is only 2G. I can’t afford it.

I searched the Internet and found that many people are facing the same problem. A more formal explanation from the PHP official is that there is no memory leak in the PHP CGI process. PHP CGI will recycle all the memory used by the script at the end of each request, but will not release it to the operating system, but will continue to hold it in response to the next PHP request. This is probably done to reduce memory fragmentation or solve the uncontrollable time required to release memory from the system and back to the operating system. However, if a PHP request occasionally uses a large memory operation such as FTP or zlib, a large block of system memory will be continuously occupied by PHP CGI and cannot be utilized.
The solution to this problem is to optimize the fastcgi configuration file parameters in the web server configuration.

In the C: \ windows \ system32 \ inetsrv \ fcgiext.ini file, you can set parameters related to the PHP CGI process, such as:

[Types]
php = PHP
[PHP]
ExePath=C:\php-5.3.8-nts-Win32-VC9-x86\php-cgi.exe
maxInstances=100
InstanceMaxRequests=10000
EnvironmentVars=PHP_FCGI_MAX_REQUESTS:10000
RequestTimeout=600
ActivityTimeout=900

In the above configuration:

ExePath   Specifies the path to the fastcgi resolver,
instanceMaxRequests   Specifies the maximum number of requests that can be processed by each instance,
Maxinstances specifies the maximum number of instances that can be started,
Environmentvars creates an environment variable PHP_ FCGI_ MAX_ Requests, the default value is 10000,
requestTimeout   Specifies that the timeout for the request is 600 seconds,
activityTimeout   An active session timeout of 900 seconds was specified.
The following are the recommended values:
maxInstances=
Reduce this value

proposal

Maxinstances = 50 for 512M memory
Maxinstances = 80 for 1G memory
Maxinstances = 140 for 2G memory

Modify again
InstanceMaxRequests=
Reduce this value

proposal

Instancemaxrequests = 200 for 512M memory
Instancemaxrequests = 300 for 1G memory
Instancemaxrequests = 500 for 2G memory

After modification, restart IIS.

instanceMaxRequests   PHP_ FCGI_ MAX_ REQUESTS   These two parameters determine the maximum number of PHP requests accepted after a PHP CGI process is created. The default configuration in lighttpd is 10000. That is, the PHP CGI process will terminate every 10000 PHP requests, release all memory, and be restarted by the management process. If you reduce it to 100, for example, the restart cycle of PHP CGI will be greatly shortened, and the impact time of problems caused by occasional high memory operations will be shortened.
The maxinstances parameter specifies the maximum number of instances that can be started, that is, the number of php-cgi.exe processes. If you reduce it to 100, for example, there is only php-cgi.exe in the process of task manager, and the total memory occupied by php-cgi.exe will be greatly reduced.
The server I use now is the windows 2003 operating system with 4G memory. A php-cgi.exe program occupies 7-25m memory. I set the value of maxinstances to 300, which reduces the total number of php-cgi.exe processes and the memory occupied. The reaction speed is much faster than before. At least how much to adjust can be determined according to the number of visits to your site.

Win2008 server, fastcgi perfect setup tutorial

Using fastcgi to call php-cgi.exe on IIS7 of win2008, there are only four processes by default. Therefore, for websites with large traffic, the process queuing phenomenon caused by insufficient number of processes is very serious. The solution is as follows.

32-bit system   http://www.iis.net/Downloads/files/AdminPack/TP2/AdminPack_ x86.msi
64 bit system   http://www.iis.net/Downloads/files/AdminPack/TP2/AdminPack_ amd64.msi

Download the following tools. After installation, see the figure below.

修改WIN2008下IIS调用FASTCGI进程数! - 小三子 - 怪叟博客
修改WIN2008下IIS调用FASTCGI进程数! - 小三子 - 怪叟博客

add to

PHP_FCGI_MAX_REQUESTS = 10000

After adding, you can see the effect in the process manager without restarting IIS.

修改WIN2008下IIS调用FASTCGI进程数! - 小三子 - 怪叟博客

It doesn’t seem to have reached the set value, but browsing the website obviously feels the hint of performance.

修改WIN2008下IIS调用FASTCGI进程数! - 小三子 - 怪叟博客

Memory usage~~   In fact, one disadvantage of Microsoft’s system is that,   The memory occupation of the server should be used by the service as much as possible, instead of leaving enough memory for the user’s application as the client.

Here I would like to remind you that the following two parameters are worth setting:

Instancemaxrequests: the maximum number of requests that the fastcgi process (php-fcgi. Exe) can handle. If it exceeds, it will be recycled. The default is 200

PHP_ FCGI_ MAX_ Requests: the maximum number of requests controlled by PHP itself. The default is 500

Since you want to use fastcgi to run CGI, you certainly don’t want PHP to limit the maximum number of requests!!

Therefore, you must ensure that instancemaxrequests is less than or equal to PHP_ FCGI_ MAX_ Requests, so that PHP itself will never have a chance to recycle the process.

You can see what micorsoft said:

Make sure that FastCGI always recycles Php-cgi.exe processes before native PHP recycling starts. The configuration property instanceMaxRequests controls the FastCGI process-recycling behavior. This property specifies how many requests FastCGI will process before recycling. PHP also has a similar process-recycling functionality that is controlled by the environment variable PHP_FCGI_MAX_REQUESTS. By setting instanceMaxRequests to a value that is smaller than or equal to PHP_FCGI_MAX_REQUESTS, you can make sure that the native PHP process-recycling logic will never start.

There are too many php-cgi.exe processes on the server, resulting in 100% CPU consumption. Solution

There are often too many php-cgi.exe processes in the IIS server, resulting in 100% CPU consumption, and eventually the website runs too slowly or even gets stuck. Restarting IIS will take a long time. Why does this happen and how to solve it? At the beginning, I have been manually ending the PHP CGI program, although it can solve the problem temporarily, But it’s not a long-term solution. I Baidu. The problem is generally a program problem. For example, the program uses fastcgi to run PHP, and the startprocesses set by the program is too large, such as 6 or even larger. The following is my solution to the server cup% caused by too much php-cgi.ext, which I hope will be helpful to you; Most cases occur in the fourth item;

Solution reference:

1. Check the system disk space. Check whether there are too many temporary files in the system. The temporary files of MySQL database are saved to C: / Windows / temp by default, resulting in the accumulation of tens of thousands or even millions of small files and crushing the system disk.

Del *. * delete all files

If the above command is not strong enough, you can use the following code

del /f /q /s tmp\*.*

2. PHP is a widely used dynamic scripting language, but there is no built-in support for PHP language in IIS. Therefore, if you need to use PHP, you must install it yourself. PHP can be installed in CGI mode or ISAPI mode. Because ISAPI mode has higher performance, I suggest you use ISAPI mode.

3. If the program can be modified, it is recommended to reduce the startprocesses value in the program configuration file to 2 to see whether it can be improved.

4. Check whether there is a problem attack on the server.