Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> 電腦軟體教學 >> 伺服器技術 >> 關於伺服器

IIS 7/7.5 配置 PHP FastCGI 環境完整圖文教程 (效能最佳、支援 PHP 5.3+)

編輯:關於伺服器

為何選擇 FastCGI?

PHP 在 IIS 上有三種執行模式:

  • CGI (php.exe): 每請求建立進程,穩定但效能極差,已淘汰。
  • ISAPI (php5isapi.dll): 多執行緒、效能較好,但不穩定 (進程崩潰影響整個 IIS 工作進程),PHP 5.3+ 官方不再提供 ISAPI 模組。
  • FastCGI (php-cgi.exe): 高效能、高穩定性、進程隔離,PHP 官方推薦、IIS 7.0+ 內建支援,未來標準。

下載正確版本 PHP (關鍵)

  • 官網:https://windows.php.net/download/
  • VC 編譯器版本: Windows + IIS 必選 VC15 (VS2017) 或 VC14 (VS2015) x64 Non Thread Safe (NTS)。
    舊版說法:VC9 給 IIS、VC6 給 Apache 已過時 (PHP 7.2+ 僅提供 VS2015/2017 編譯)。
  • Thread Safe (TS) vs Non Thread Safe (NTS): FastCGI 採用多進程單執行緒模型,**必選 NTS 版本** (官方不建議生產環境用 TS)。

安裝 PHP (含 FastCGI 模組)

  1. 執行下載的 MSI 安裝包 (如 php-7.4.33-nts-Win32-VC15-x64.msi)。
  2. 安裝路徑建議:C:\PHP (避免空格、中文)。
  3. 關鍵步驟: 元件選擇頁面,**務必勾選「IIS FastCGI」** (若未顯示,先在「開啟或關閉 Windows 功能」→ IIS → 万维网服务 → 應用程式開發 → 勾選 CGI)。
PHP 安裝路徑設定

PHP 安裝路徑建議 C:\PHP

PHP 安裝元件選擇:勾選 IIS FastCGI

元件選擇:必須勾選 IIS FastCGI

FastCGI 註冊與配置 (命令列一鍵完成)

  1. 以管理員開啟 CMD,切換目錄:
    cd C:\Windows\System32\inetsrv
  2. 註冊 PHP FastCGI 處理常式:
    appcmd set config /section:system.webServer/fastCgi /+[fullPath='C:\PHP\php-cgi.exe',instanceMaxRequests='10000',environmentVariables.[name='PHP_FCGI_MAX_REQUESTS',value='10000']] /commit:apphost
  3. 新增 .php 副檔名對應:
    appcmd set config /section:system.webServer/handlers /+[name='PHP_FastCGI',path='*.php',verb='*',modules='FastCgiModule',scriptProcessor='C:\PHP\php-cgi.exe',resourceType='Either'] /commit:apphost

說明: 上述指令相當於舊版 fcgiconfig.js 方式,但使用 IIS 7+ 原生 appcmd 寫入 applicationHost.config,更標準、可攜、支援多網站獨立配置。

驗證安裝

  1. 在網站根目錄建立 info.php:
    <?php phpinfo(); ?>
  2. 瀏覽 https://localhost/info.php,顯示 PHP 版本資訊、Server API: CGI/FastCGI 即成功。
phpinfo() 輸出:Server API CGI/FastCGI

phpinfo() 確認 Server API = CGI/FastCGI

常見問題修正

1. PHP 5.3+ 嚴格模式報錯:date.timezone 未設定

編輯 C:\PHP\php.ini,搜尋 date.timezone,設定:

date.timezone = Asia/Taipei

重啟 IIS (iisreset) 生效。

2. 權限問題:php-cgi.exe 無法讀取腳本

確認網站實體目錄授予 IIS_IUSRS (或應用程式集區身分識別) 「讀取與執行」權限。

3. 擴展模組載入失敗

檢查 php.ini 中 extension_dir = "ext" 路徑正確,所需 .dll 存在於 C:\PHP\ext\,並取消註解 extension=xxx。

進階調優參數 (applicationHost.config / php.ini)

參數建議值位置
instanceMaxRequests10000FastCGI 應用程式 (applicationHost.config)
activityTimeout300FastCGI 應用程式
requestTimeout90FastCGI 應用程式
memory_limit256Mphp.ini
max_execution_time60php.ini
opcache.enable1php.ini (生產環境必開)
opcache.memory_consumption128php.ini
Copyright © Windows教程網 All Rights Reserved