# IIS FastCGI 跑 PHP 8.4：别信 Windows php-fpm 的传说

> 在 Windows 上安装 PHP 8.4 NTS、IIS FastCGI 与处理器，配置安全的 php.ini，验证 phpinfo，然后删掉测试页。

**测试环境**

- 版本号: 26100.33650
- 版本: Standard / Datacenter
- 架构: x64
- 证据: E1
- 核验日期: 2026-10-29
- 测试机: PHP 8.4.27 NTS + IIS FastCGI 在 VM 361 Windows 11 26H2 验证；Server 2025 角色/cmdlet 为 E1

VM 361：PHP 8.4.27 NTS 由 IIS FastCGI 提供，Server API 为 CGI/FastCGI，加载 `C:\PHP\8.4\php.ini`。宿主是 Windows 11 26H2；Server 2025 角色步骤仍为 E1。

## 一句话结论

Windows 上 PHP 官方走 **FastCGI/CGI**，不是 php-fpm。没有官方 Windows php-fpm 可装。

可用栈：

1. IIS + **CGI/FastCGI** 角色服务。
2. PHP **NTS** Windows 包 + VC++ 运行库。
3. `php.ini`：`fastcgi.impersonate=1`、`cgi.fix_pathinfo=0`。
4. IIS FastCGI 应用 + `*.php` 处理器。
5. 测试页，然后删掉 `phpinfo()`。

## 安装 IIS 侧

Server 2025：

```powershell
Install-WindowsFeature Web-CGI
```

Windows 客户端实验室（VM 361 实际用的）：

```powershell
Enable-WindowsOptionalFeature -Online -FeatureName IIS-CGI -All -NoRestart
```

处理器没出现就 `iisreset` 或重启 W3SVC。

## 下载 PHP

FastCGI 用 **NTS**（Non-Thread Safe）zip。本次实验：

- `php-8.4.27-1-nts-Win32-vs17-x64.zip`
- 解压到 `C:\PHP\8.4`
- `php.exe -v` 返回 `PHP 8.4.27 (cli) (NTS Visual C++ 2022 x64)`

用同目录 `sha256sum.txt` 核对 SHA-256。不要用随机“PHP for Windows”重打包。

PHP 需要 **Visual C++ 2015–2022 x64 运行库**。没有它，`php.exe` 会以 `0xC0000135`（缺 DLL）退出。装完再测 `php.exe -v`。

## php.ini

复制 `php.ini-development` 为 `php.ini`，至少设：

```ini
fastcgi.impersonate=1
cgi.fix_pathinfo=0
date.timezone=UTC
extension_dir="ext"
expose_php=Off
```

生产再加：

```ini
display_errors=Off
log_errors=On
error_log="C:\PHP\logs\php-error.log"
```

不要把所有扩展开满。应用需要 `mysqli`、`pdo_mysql`、`curl`、`mbstring`、`openssl` 时再加，用 `php -m` 验证。

## FastCGI + 处理器

用 WebAdministration（比手写 `appcmd` 引号可靠）：

```powershell
Import-Module WebAdministration
Add-WebConfiguration -PSPath 'MACHINE/WEBROOT/APPHOST' `
  -Filter 'system.webServer/fastCgi' `
  -Value @{ fullPath = 'C:\PHP\8.4\php-cgi.exe' }

Add-WebConfiguration -PSPath 'MACHINE/WEBROOT/APPHOST' `
  -Filter 'system.webServer/handlers' `
  -Value @{
    name = 'PHP_via_FastCGI'; path = '*.php'; verb = '*';
    modules = 'FastCgiModule';
    scriptProcessor = 'C:\PHP\8.4\php-cgi.exe';
    resourceType = 'Either'
  }
```

然后：

```powershell
& 'C:\PHP\8.4\php-cgi.exe' -v
Set-Content C:\inetpub\lab\info.php '<?php phpinfo();' -Encoding ASCII
Invoke-WebRequest http://localhost:8080/info.php -UseBasicParsing | Select-Object StatusCode
```

预期：`200`，phpinfo 显示 **Server API: CGI/FastCGI**、**Loaded Configuration File: C:\PHP\8.4\php.ini**。

## 权限

- 应用池身份对 `C:\PHP\8.4` 和站点根目录需要**读/执行**。
- 不要给 PHP 目录写权限。
- 上传目录单独授权，只在那里给 modify。
- 不要为了“让 PHP 能跑”把应用池改成 LocalSystem。

## 安全

- 测完删 `info.php`；phpinfo 会泄露路径、模块和配置。
- `expose_php=Off` 去掉 `X-Powered-By` 版本提示。
- 生产 `display_errors=Off`，写日志。
- `cgi.fix_pathinfo=0` 缓解经典路径解析问题，但不能代替上传校验。
- 定期打 PHP 补丁；EOL 分支没有安全修复。

> **警告: Windows 没有 php-fpm**
>
> 有指南让你在 Windows 装 php-fpm，要么是第三方构建，要么把 FastCGI 和 FPM 混了。本页用官方 Windows PHP + IIS FastCGI。

## 实验室说明

VM 361 验证：PHP 8.4.27 NTS、IIS FastCGI、`info.php` 返回 200。这是 **Windows 11 FastCGI 机制的 E2**，不是 Server 2025 复现。Server 2025 用 `Install-WindowsFeature Web-CGI`，处理器配置同族。

## 来源

- [PHP — Windows 下载](https://windows.php.net/downloads/releases/)
- [PHP 手册 — Windows 安装](https://www.php.net/manual/zh/install.windows.php)

---

来源页面: https://winbook.fyi/zh/server/web/iis-php/
