在 IIS 6+FastCGI 環境執行 PHP 時,長時間執行的腳本常遇到「The FastCGI process exceeded configured request timeout」錯誤。本文說明透過修改 `fcgiext.ini` 解決逾時問題的完整步驟。
| 錯誤訊息 | 相關參數 | 說明 |
|---|---|---|
| FastCGI process exceeded configured request timeout | RequestTimeout | 單一請求允許的最大執行秒數 |
| FastCGI process exceeded configured activity timeout | ActivityTimeout | 進程無回應的最大秒數(含網路等待) |
C:\WINDOWS\system32\inetsrv\fcgiext.ini
[Types]
php=PHP
[PHP]
ExePath=C:\PHP\php-cgi.exe
InstanceMaxRequests=10000
EnvironmentVars=PHP_FCGI_MAX_REQUESTS:10000
RequestTimeout=500
ActivityTimeout=900
在 `php.ini` 中同步調整:
; php.ini
max_execution_time = 300 ; 對應 RequestTimeout
max_input_time = 300 ; 輸入資料處理時間
memory_limit = 256M ; 視需求調整
; 確保 FastCGI 環境變數一致
; PHP_FCGI_MAX_REQUESTS=10000 (在 fcgiext.ini 已設定)
新版 IIS 使用 `applicationHost.config` 或網站 `web.config`:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<fastCgi>
<application fullPath="C:\PHP\php-cgi.exe"
requestTimeout="00:10:00"
activityTimeout="00:15:00"
instanceMaxRequests="10000">
<environmentVariables>
<environmentVariable name="PHP_FCGI_MAX_REQUESTS" value="10000" />
</environmentVariables>
</application>
</fastCgi>
<handlers>
<add name="PHP_via_FastCGI"
path="*.php"
verb="*"
modules="FastCgiModule"
scriptProcessor="C:\PHP\php-cgi.exe"
resourceType="Either" />
</handlers>
</system.webServer>
</configuration>
<?php
// test_timeout.php
// 測試長時間執行腳本
ini_set('max_execution_time', 600);
echo "開始測試: " . date('H:i:s') . "<br>";
sleep(120); // 睡眠 2 分鐘
echo "結束測試: " . date('H:i:s') . "<br>";
echo "若看到此訊息,逾時設定已生效";
?>
| 現象 | 可能原因 | 解決方向 |
|---|---|---|
| 修改後仍逾時 | 未重啟 IIS | 執行 `iisreset` 或重啟網站 |
| 修改後仍逾時 | php.ini max_execution_time 較小 | 同步調整 php.ini 設定 |
| 500 錯誤 | fcgiext.ini 格式錯誤 | 檢查編碼為 ANSI/UTF-8 無 BOM,語法正確 |
| 記憶體耗盡 | 長時間腳本累積記憶體 | 調高 memory_limit、分批處理、加入 gc_collect_cycles() |