Nginx 默认内置 FastCGI 模块,但建议显式添加 --with-http_fastcgi_module 参数确保启用;可用 nginx -V | grep with-http_fastcgi_module 验证,配合 fastcgi_pass 等配置即可支持 PHP-FPM。

Nginx 源码编译时启用 FastCGI 模块非常简单,因为 ngx_http_fastcgi_module 是默认内置的核心模块,不需要额外启用。
但为确保万无一失,或在极简定制编译中避免误删,你仍应在 ./configure 命令中显式加上:
./configure --with-http_fastcgi_module
这行指令明确告诉编译系统:请包含 FastCGI 支持模块。
注意:
- 大多数主流发行版(如 Ubuntu/Debian/CentOS)提供的预编译 Nginx 包都已默认启用该模块;
- 你可以用
nginx -V 2>&1 | grep -o with-http_fastcgi_module快速验证是否已编译进当前 Nginx; - 如果输出为空,说明未启用,必须重新编译并加入该参数。
常见搭配建议
- 若你还要支持 PHP-FPM 缓存,顺带加上缓存相关模块:
--with-http_fastcgi_module --with-http_ssl_module --with-http_gzip_module
- 不需要 FastCGI?可用
--without-http_fastcgi_module禁用(极少用)。
编译安装典型流程
- 安装依赖(以 Ubuntu 为例):
sudo apt update && sudo apt install build-essential libpcre3-dev zlib1g-dev libssl-dev
- 下载源码、解压后进入目录:
./configure --prefix=/usr/local/nginx --with-http_fastcgi_module make && sudo make install
- 启动验证:
/usr/local/nginx/sbin/nginx -t # 检查语法 /usr/local/nginx/sbin/nginx # 启动
验证模块是否生效
写一段最简 FastCGI 配置(如处理 .php 文件),放入 nginx.conf 的 server 块内:
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}若 nginx -t 不报错,且能正常转发请求到 PHP-FPM,说明模块已成功启用并可用。
不复杂但容易忽略


















