A slow server is always a nightmare for any server owner. And, when the slowness occurs due to applications like PHP, it adds fuel to the fire.
Unfortunately, any problem with PHP affects all PHP-based websites on the server. Therefore, a timely solution is always necessary.
That’s why, at Bobcares we often get requests from customers to fix php-cgi.exe high CPU usage in servers as part of our Server Administration Services.
Today, we’ll see the top reasons that cause high php-cgi.exe CPU usage and how our Support Engineers fix it.
Understanding php-cgi.exe
Often high load in Windows servers attributes to php-cgi.exe processes. Therefore, it’s worth to learn more on php-cgi.exe.
In simple words, php-cgi.exe is the executable for processing PHP requests on the server. This file is needed at the time of php configuration with web servers like IIS. The path to this binary depends on the folder at which PHP is installed on the server. By default, installation path will be at C:\php\php-cgi.exe
Every time a user requests a website page via browser, it is this back-end php binary that show the results. Naturally, any problems with PHP binary will lead to higher resource usage on the server.
What causes php-cgi.exe high CPU usage?
From our experience in managing servers, our Support Engineers often deal with high CPU usage Windows servers. When checking the status of servers using Task Manager in Windows, in many cases, it will be the php-cgi.exe process that consumes 100% CPU.
Let’s now take a look on what causes the php-cgi.exe high resource usage.
1. Malformed PHP scripts
Unfortunately, high CPU usage by PHP happens due to bad scripts on some on the domains on the server. PHP demands couple of standards when developing any websites. When some developers call PHP functions in the wrong way, or put them in an infinite loop, the PHP process fails. These poorly coded scripts will call the php-cgi.exe PHP worker program on the server and will eat 100% of CPU.
Similarly, hackers often upload malicious PHP scripts that deliberately create high load on the server. Eventually, this would slow down the entire server and affect all the domains too.
2. Large number of PHP processes
Yet another reason for high CPU usage happens due to large number of PHP processes on the server.
In any server, there will be an ideal number of requests that PHP can process considering the physical memory capacity of the server. When there are more users beyond this capacity, each connection creates a new instance of php-cgi.exe
In other words, more users means more instances of php-cgi.exe. And all these processes take up considerable amount of server resources. It would take up all up all the available RAM. As a result, the CPU usage shoots up.
3. Problem in fetching content
PHP can cause high CPU usage when there are problems in fetching the desired result from the server. This particularly happens due to messed up permissions on some folders and lack of proper user privileges too.
Another scenario for PHP website problems arise when certain content fetching is done from a different server. In such cases, if there is a firewall or antivirus that is stops the content from arriving on the site, PHP permanently waits to finish loading the page. As a result, it would also cause high CPU usage.
How we fix high CPU usage by php-cgi.exe
We already saw the different reasons that would cause high CPU usage by PHP processes. Now, let’s see the typical measures that our Dedicated Engineers take to fix the resource usage on the server.
1. Identifying the problem user
In any case of high CPU usage on servers with many websites, the first step is to identify the exact user that runs the PHP script. Identifying this user helps to solve the problem immediately. That’s why, we open Task Manager in Windows, go to details tab, and then take a look at column “User name” for php-cgi.exe process that consumes 100% CPU.
Again, when it’s a Plesk Windows server, more details are available from the Plesk Panel. In Plesk ->Tools&Settings-> Assistance and troubleshooting section we can see Process list screen. It shows information already according to domains. Our Support Engineers check the domain that consumes most CPU time. We then check the script and suggest customer to disable or modify it.
Also, if that happens to be a malicious script, we straightaway disable it and that would bring the server CPU usage back to normal.
2. Adjusting PHP settings
When the problem happens due to high number of users, we tweak the PHP settings on the server. For example, in older versions of IIS, we set the parameter maxInstances to 10 times the number of CPU processors on the server. That is your server has 4 CPU processors, this value would be 40. This helps to limit the amount of CGI processes that can be spinned up on the server. Thus, if there are more users beyond the server capacity, it will still keep the CPU usage under limits.
However, IIS 7.5 and higher handles the maxInstances dynamically by default. Therefore, our Support Engineers set the value as 0. This would equip the PHP FastCGI module capable of monitoring system state and adjust this number dynamically to give best possible performance.
A sample set of FastCGI parameters on a server we manage look like:
Again, for certain customers, we recommend customers to use a WAMP package that uses PHP as an Apache module. As PHP working mode becomes thread based, it will greatly reduce the resources usage and can generally handle more connections.
3. Fixing permissions and internal links
Last and not the least, fixing permissions of the files and folders also solve PHP resource usage problem in some cases. We analyze the user permissions and assign proper privileges to the Windows users. Similarly, we also adjust the firewall settings to allow certain IP addresses or tweak antivirus on the server. This would help to prevent any block on receiving content from another server.
[Is your Windows server always on 100% CPU usage? We’ll help you identify the reason and fix it.]
Conclusion
In short, php-cgi.exe high CPU usage happens mainly due to malformed PHP scripts, wrong IIS server settings and more. Today, we saw the typical reasons that would shoot up CPU usage on the server and how our Support Engineers identify and fix the problem.
AWS billed me exponentially higher this month for my ec2 burstable instance. After reading this article, i checked my cpu utilization which was 100%, that was the reason for higher bill. Did the iis settings you mentioned and cpu utilization decreased dramatically. My wordpress site got hacked sometime ago and i am sure thats the reason for cgi / fast cgi was consuming higher cpu.
Can you help me find that malicious script?
Hello Muhammad,
Glad to know that the post was useful.
Yes, we can help you find the malicious script. If you still need help, we’ll be happy to talk to you on chat (click on the icon at right-bottom).
2. Adjusting PHP settings
This post is usefull for me.