在 Windows Server 2003 這一老舊平台上高效運行 PHP,同時兼顧記憶體佔用,確實是個挑戰。好在 IIS 7.5 Express 支持在 Windows 2003 上安裝,它不僅原生支持 FastCGI 協議運行 PHP,還內建 URL Rewrite 模組,僅需簡單修改 web.config 的規則即可實現偽靜態與壓縮優化。本文將詳細記錄從安裝到配置的完整流程,助你在舊環境中構建高性能 PHP 運行環境。
下載並安裝 IIS 7.5 Express 後,核心配置文件 applicationHost.config 通常位於以下兩個路徑之一:
我的文檔\IISExpress\config\applicationHost.configIIS Express 安裝路徑\config\applicationHost.config找到該文件後,建議先備份,再進行下列修改。
在 <defaultDocument> 節點下的 <files> 集合中加入 <add value="index.php" />,確保 PHP 入口文件被識別為默認首頁。
<defaultDocument enabled="true">
<files>
<add value="Default.htm" />
<add value="Default.asp" />
<add value="index.htm" />
<add value="index.html" />
<add value="iisstart.htm" />
<add value="default.aspx" />
<add value="index.php" />
</files>
</defaultDocument>
在 <fastCgi> 節點下註冊 PHP-CGI 可執行文件,關鍵參數說明:
fullPath:指向 php-cgi.exe 的絕對路徑(請替換為實際路徑)monitorChangesTo:監視 php.ini 變更自動重載activityTimeout / requestTimeout:超時時間設為 600 秒instanceMaxRequests:單進程最大請求數 10000PHP_FCGI_MAX_REQUESTS 與 PHPRC(指定 php.ini 目錄)<fastCgi>
<application fullPath="C:\php\php-cgi.exe" monitorChangesTo="php.ini" activityTimeout="600" requestTimeout="600" instanceMaxRequests="10000">
<environmentVariables>
<environmentVariable name="PHP_FCGI_MAX_REQUESTS" value="10000" />
<environmentVariable name="PHPRC" value="C:\Program Files (x86)\IIS Express\PHP\v5.4" />
</environmentVariables>
</application>
</fastCgi>
在 <handlers> 節點最頂部插入以下映射,確保優先被處理,避免被其他處理程序搶先:
<add name="PHP_FastCGI" path="*.php" verb="GET,HEAD,POST" modules="FastCgiModule" scriptProcessor="C:\PHP\php-cgi.exe" resourceType="Either" />
注意:務必將路徑替換為你實際的 php-cgi.exe 位置。
在 <site> 節點下配置站點物理路徑、綁定端口等,或使用命令行工具 appcmd.exe 添加站點。配置完成後,進入 IIS Express 目錄,雙擊 IISExpress.exe 或以命令行啟動指定站點:
iisexpress.exe /site:"站點名稱1"
iisexpress.exe /site:"站點名稱2"
web.config 配置(偽靜態 + Gzip)在 WordPress 根目錄建立 web.config,內容如下:
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="wordpress" patternSyntax="Wildcard">
<match url="*" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="index.php" />
</rule>
</rules>
</rewrite>
<urlCompression doStaticCompression="true" doDynamicCompression="true" />
</system.webServer>
</configuration>
此配置同時啟用靜態/動態內容 Gzip 壓縮與 WordPress 友好鏈接重寫。
實測顯示,該方案在 Windows 2003 上運行 WordPress 等 PHP 應用,響應速度明顯優於 Apache + mod_php,且無需額外部署 Nginx 反向代理,整體記憶體佔用大幅降低,極適合資源受限的老舊服務器環境。