萬盛學電腦網

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

IIS 7.5 PHP 環境搭建圖文教程:FastCGI 模式完整配置指南

Windows 7/8/Server 2008 R2 IIS 7.5 + PHP FastCGI 環境搭建全記錄

IIS 7.5 引入模塊化架構,PHP 配置方式與 IIS 6.0 截然不同。本文提供從零開始的圖文級配置指南,涵蓋 ISAPI/CGI 限制、處理程序映射、虛擬主機、權限設置全流程。

準備工作

  • PHP 版本:Non Thread Safe (NTS) 版本 (如 php-5.6.40-nts-Win32-VC11-x64.zip)
  • 解壓至:C:\PHP (路徑無空格、中文)
  • 復制 php.ini-production → php.ini

步驟 1:啟用 IIS 核心功能

控制面板 → 程序 → 啟用或關閉 Windows 功能:

  • ☑ Internet Information Services
  • ☑ World Wide Web Services → Application Development → ☑ CGI (關鍵:FastCGI 依賴)
  • ☑ ISAPI Extensions
  • ☑ ISAPI Filters
  • ☑ Common HTTP Features → ☑ Static Content, Default Document, Directory Browsing, HTTP Errors
  • ☑ Health and Diagnostics → ☑ HTTP Logging, Request Monitor
  • ☑ Performance → ☑ Static Content Compression, Dynamic Content Compression
  • ☑ Security → ☑ Request Filtering, Basic Authentication, Windows Authentication

步驟 2:配置 PHP 核心參數 (php.ini)

; 必須修改項
extension_dir = "C:\PHP\ext"
cgi.force_redirect = 0
cgi.fix_pathinfo = 1
fastcgi.impersonate = 1
date.timezone = Asia/Shanghai
extension=curl
extension=gd2
extension=mbstring
extension=mysqli
extension=pdo_mysql
extension=openssl
extension=fileinfo

; 上傳與安全
upload_max_filesize = 50M
post_max_size = 50M
max_execution_time = 300
disable_functions = exec,system,passthru,shell_exec,proc_open,proc_get_status,popen,pclose,dl,chmod
open_basedir = "C:\inetpub\wwwroot\;C:\PHP\temp\;C:\Windows\Temp\"

; Session
session.save_path = "C:\PHP\temp"
session.gc_maxlifetime = 1440

創建目錄並設置權限:C:\PHP\temp (IIS_IUSRS: 修改權限)

步驟 3:IIS 處理程序映射配置 (關鍵步驟)

  1. 打開 IIS 管理器 (inetmgr)
  2. 選中服務器節點 (根節點) → 雙擊 處理程序映射
  3. 右側操作欄 → 添加模塊映射:

點擊 請求限制 → 訪問 選項卡 → 選中 腳本 → 確定

彈出提示:「是否要創建 FastCGI 應用程序?」→ 是**

步驟 4:FastCGI 設置優化

  1. IIS 管理器 → 服務器節點 → FastCGI 設置**
  2. 選中 C:\PHP\php-cgi.exe → 編輯
  3. 設置環境變量:
字段值
請求路徑*.php
模塊FastCgiModule
可執行文件C:\PHP\php-cgi.exe
名稱PHP_via_FastCGI
名稱值
PHP_FCGI_MAX_REQUESTS10000
PHPRCC:\PHP

監視文件變更:添加 C:\PHP\php.ini,修改配置後自動回收進程

步驟 5:創建測試站點與驗證

  1. 右鍵 網站** → 添加網站**
  1. 創建測試文件 C:\inetpub\wwwroot\php_test\info.php:
<?php phpinfo(); ?>
  1. 編輯 C:\Windows\System32\drivers\etc\hosts 添加:
127.0.0.1 php.test.local
  1. 瀏覽器訪問 https://php.test.local/info.php,應顯示 PHP 信息頁面

步驟 6:默認文檔與錯誤頁面

  1. 站點 → 默認文檔** → 添加 index.php 並移至頂部
  2. 站點 → 錯誤頁面** → 編輯功能設置 → 詳細錯誤 (開發環境)

常見錯誤與解決

字段值
站點名稱PHP_Test
物理路徑C:\inetpub\wwwroot\php_test
綁定類型: http, IP: 未分配, 端口: 80, 主機名: php.test.local
錯誤碼原因解決
500.19配置文件錯誤/權限檢查 web.config、applicationHost.config 權限
500.0PHP 致命錯誤查看 PHP 錯誤日誌 (php.ini error_log)、Windows 事件查看器
404.3處理程序映射缺失重新添加 *.php FastCGI 映射
401.3ACL 權限不足物理路徑授予 IUSR/IIS_IUSRS 讀取權限
FastCGI Errorphp-cgi.exe 崩潰檢查 php.ini 語法、擴展依賴 (VC++ Redistributable)、內存限制

生產環境加固清單

  • ✅ 禁用目錄瀏覽
  • ✅ 隱藏 PHP 版本: expose_php = Off
  • ✅ 限制請求大小: max_allowed_packet, post_max_size
  • ✅ 配置請求過濾: 禁止訪問 .git、.env、bak、sql 等敏感文件
  • ✅ 啟用動態壓縮: gzip/deflate
  • ✅ 配置應用程序池: 獨立標識、回收條件、內存限制
  • ✅ 定期更新 PHP、Windows 安全補丁
copyright © 萬盛學電腦網 all rights reserved