萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> PHP 程式設計

Windows Server 2003 運行 PHP 的最佳選擇:IIS 7.5 Express + FastCGI 實戰配置

為什麼選 IIS 7.5 Express?

  • Windows Server 2003 不支持 IIS 7.0+ 完整版,但支持 IIS 7.5 Express(輕量級、用戶態進程)。
  • 原生支持 FastCGI,PHP 性能遠超 ISAPI/CGI 模式。
  • 內建 URL Rewrite Module,無需第三方 ISAPI Rewrite 組件。
  • 內存佔用極低,適合資源受限的老舊服務器。

安裝與核心配置文件定位

  1. 下載:IIS 7.5 Express 官方安裝包(x86/x64)。
  2. 安裝後,核心配置文件 applicationhost.config 位於:
    • %USERPROFILE%\Documents\IISExpress\config\applicationhost.config(優先)
    • 或安裝目錄 C:\Program Files\IIS Express\applicationhost.config

關鍵配置修改

1. 默認文檔 <defaultDocument>

<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" /> <!-- 關鍵:添加 PHP 入口 -->
  </files>
</defaultDocument>

2. FastCGI 應用程序池 <fastCgi>

<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" /> <!-- PHP.ini 目錄 -->
    </environmentVariables>
  </application>
</fastCgi>

注意: 路徑請替換為實際 PHP 安裝路徑,建議使用短路徑無空格(如 C:\php)。

3. 處理程序映射 <handlers>

<handlers accessPolicy="Read, Script">
  <!-- 必須放在第一位,避免被 StaticFile/其他處理程序搶先 -->
  <add name="PHP_FastCGI" 
       path="*.php" 
       verb="GET,HEAD,POST" 
       modules="FastCgiModule" 
       scriptProcessor="C:\php\php-cgi.exe" 
       resourceType="Either" 
       requireAccess="Script" />
  <!-- 其他 handlers ... -->
</handlers>

4. 站點綁定 <site>

<site name="MySite" id="1">
  <application path="/">
    <virtualDirectory path="/" physicalPath="D:\wwwroot\mysite" />
  </application>
  <bindings>
    <binding protocol="http" bindingInformation=":8080:" /> <!-- 端口 8080,避免衝突 -->
  </bindings>
</site>

或使用命令行快速建站:

appcmd.exe add site /name:"MySite" /physicalPath:"D:\wwwroot\mysite" /bindings:http/*:8080:

WordPress 完整 web.config(含 Rewrite + Gzip)

放置於 WordPress 根目錄:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="WordPress" patternSyntax="Wildcard">
          <match url="*" />
          <conditions logicalGrouping="MatchAll">
            <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" />
    
    <defaultDocument>
      <files>
        <add value="index.php" />
      </files>
    </defaultDocument>
  </system.webServer>
</configuration>

啟動與驗證

  1. 進入 IIS Express 安裝目錄:cd "C:\Program Files\IIS Express"
  2. 啟動指定站點:iisexpress.exe /site:"MySite"
  3. 或直接雙擊 IISExpress.exe(會加載所有已配置站點)。
  4. 瀏覽器訪問 https://localhost:8080 或 https://服務器IP:8080。

性能對比與優勢

指標Apache 2.2 (Prefork) + mod_phpIIS 7.5 Express + FastCGI
內存佔用 (空閒)~80-120 MB~15-30 MB
PHP 進程模型嵌入 Apache 進程,每請求一線程獨立 php-cgi.exe 池,進程復用
靜態文件處理Apache 內部IIS 內核緩存 + 靜態壓縮
Rewrite 支持需 mod_rewrite內建 URL Rewrite Module
Windows 整合一般原生 (IUSR、ACL、ETW 追蹤)

已知限制與避坑

  • IIS Express 不支持作為 Windows 服務自動啟動,需寫腳本放入「啟動」文件夾或用 NSSM 封裝為服務。
  • PHP 版本建議 5.4+(含 php-cgi.exe),PHP 5.2 兼容性差。
  • PHPRC 環境變量必須指向包含 php.ini 的目錄,否則 PHP 會讀取系統目錄下的錯誤配置。
  • 大文件上傳需調整 requestTimeout、activityTimeout 及 PHP max_execution_time、upload_max_filesize。
copyright © 萬盛學電腦網 all rights reserved