CGI Timeout When Running PHP Script With IIS

Date Initially Written: 8 January 2003.

Problem: When running a PHP script with IIS a CGI Timeout error occurs after 5 minutes.

Operating System: Windows 2000 Professional with service pack 3, IIS 5.0.

Background: The PHP script I was running takes a long time to complete as it was checking and verifying that a large number of web pages exist. It could take an hour or more to complete.

Resolution Steps: I first tried setting the script's time limit with the following PHP code:

set_time_limit(10800); // Three hour timeout

But this didn't help. It seemed to me that IIS was killing the script after 5 minutes. I looked in the IIS management console (Start Settings Control Panel Administrative Tools Internet Services Manager) but couldn't find anything related to the CGI timeout period.

Searching Google Groups I found several people that had this problem but no helpful solutions. One particular MVP (Microsoft Most Valued Professional) insisted that it was impossible for a script to run for more than 5 minutes and said:

you just can't run stuff across http that takes longer than five minutes to run. get over it.

I knew this wasn't true as I'd run long executing PHP scripts under Apache before. After much more digging around I finally found where the CGI Timeout was specified. This is how you change it:

  1. Start the IIS management console (Start Settings Control Panel Administrative Tools Internet Services Manager).
  2. In the tree on the left-hand side of the console you should see "Internet Information Services" at the very top, then below this the name of your computer. Right-click your computer name and select "Properties".
  3. Under "Master Properties" is a listbox; ensure that "WWW Service" is showing, then click the "Edit" button.
  4. Click on the "Home Directory" tab.
  5. Click on the "Configuration" button in the Application Settings area.
  6. Click on the "Process Options" tab.
  7. In the CGI Configurations area there is a "CGI script timeout" value specified in seconds. The default value is 300 seconds (5 minutes).
  8. Change this value to whatever you would like your timeout to be.
  9. Work your way out by clicking the "Ok" button on each window until you get right back to the IIS management console.
  10. Close the management console.
  11. Restart IIS by opening a command prompt (Start Programs Accessories Command Prompt) and type the command: iisreset

The above instructions work for IIS 5.0, which ships with Windows 2000. If you have Windows XP then you've probably got IIS 5.1 and should follow these instuctions:

  1. Open IIS.
  2. Expand computer name.
  3. Right-click on "Web Sites" and choose Properties.
  4. Go to "Home Directory" tab.
  5. Click the "Configuration" button, brings up another window.
  6. Go to the "Process Options" tab.
  7. Set as your heart desires.
  8. Ok out.

If you are running Windows Server 2003 then you are running IIS 6.0. Microsoft decided to remove the option to configure the CGI timeout value in IIS 6.0 from the GUI interface. See this article for information on how to set the timeout value in IIS 6 / 7 / 7.5.

Recommended Reading For PHP Programmers: Programming PHP