--- title: PHP 体系架构 tags: [概念, php, web, fastcgi, php-fpm, nginx] created: 2026-07-22 updated: 2026-08-07 sources:

  • raw/cgi-fastcgi-php-fpm-wsgi-uwsgi-agi-egon.md
  • raw/web层部署php程序 – Egon林海峰.md
  • raw/nginx优化下 – Egon林海峰.md aliases:
  • PHP Web 架构
  • PHP-FPM

PHP 体系架构

架构总览

浏览器
  ↕ HTTP
Nginx(Web 服务)
  ↕ FastCGI 协议(fastcgi_pass)
PHP-FPM(FastCGI 进程管理器)
  ↕ PHP 代码执行
PHP Web 应用(Laravel / ThinkPHP 等)

LNMP 架构

LNMP = Linux + Nginx + MySQL + PHP,搭建 PHP 动态网站的经典架构组合。

组件角色

组件角色监听端口
Linux操作系统,提供运行环境—
NginxWeb 服务 / 上游代理,接收 HTTP 请求80(HTTP)
PHP (php-fpm)应用服务器,执行 PHP 代码9000(FastCGI)
MySQL (MariaDB)数据库,存储数据3306(SQL)

数据流

浏览器
  ↕ HTTP(TCP 80)
Nginx
  ↕ FastCGI 协议(TCP 9000)
php-fpm(内嵌 PHP 解释器)
  ↕ SQL 协议(TCP 3306)
MySQL(MariaDB)

上下游通信的三段协议

段上下游协议说明
1浏览器 → NginxHTTP用户通过浏览器访问 Nginx
2Nginx → php-fpmFastCGINginx 将 PHP 请求转发给 php-fpm
3php-fpm → MySQLSQLPHP 程序通过 SQL 读写数据库

LNMP 的精髓在于 三段上下游 + 三种不同协议,每一层只负责自己擅长的领域。


PHP-FPM

是什么

PHP-FPM(PHP FastCGI Process Manager)是基于 FastCGI 协议实现的进程管理器。它不是协议,而是具体的程序实现。

进程模型

PHP-FPM
  ├── Master 进程:监听端口/Unix Socket,接收请求,指派 Worker
  └── Worker 进程池(多个)
        ├── Worker 1:嵌入 PHP 解释器,执行 PHP 代码
        ├── Worker 2:嵌入 PHP 解释器,执行 PHP 代码
        └── Worker N:嵌入 PHP 解释器,执行 PHP 代码
  • Master:负责监听端口,接收 Web 服务器的请求,分配 Worker
  • Worker:每个 Worker 内嵌 PHP 解释器,实际执行 PHP 代码
  • 可根据访问压力动态调整 Worker 数量(动态/静态/按需模式)

关键特性

特性说明
动态进程管理根据压力自动创建/销毁 Worker 进程
平滑重载重载 PHP 配置时不中断现有请求
Unix Socket支持 Unix Socket 通信,无需配置 TCP 端口
状态输出提供详细的状态页和 slow log
502 错误诊断提供更详细的错误信息

重要:PHP-FPM 不支持 HTTP 协议,不能直接充当 Web 服务。它只支持 FastCGI 协议,必须搭配 Nginx/Apache 使用。


Nginx 与 FastCGI 的协作

Nginx 本身无法直接与 FastCGI 程序通信,需要启用 ngx_http_fastcgi_module 模块。

基本配置

location ~ \.php$ {
    try_files  $uri = 404;
    fastcgi_pass  127.0.0.1:9000;
    fastcgi_index  index.php;
    include  fastcgi.conf;
}

负载均衡配置

upstream php {
    server  127.0.0.1:9000;
    server  127.0.0.1:9001;
    server  127.0.0.1:9002;
    server  127.0.0.1:9003;
}
 
location ~ \.php$ {
    try_files  $uri = 404;
    fastcgi_pass  php;
    fastcgi_index  index.php;
    include  fastcgi.conf;
}

PHP 体系数据流

用户请求 example.com/index.php
              ↓
DNS 解析 → Nginx 接收请求
              ↓
Nginx 解析 HTTP 协议
    ├── 静态请求(.html/.css/.png)→ 直接返回文件
    └── PHP 动态请求(.php)→ fastcgi_pass 转发
              ↓
        FastCGI 协议(127.0.0.1:9000)
              ↓
        PHP-FPM Master 接收请求
              ↓
        指派 Worker 进程
              ↓
        PHP 解释器执行业务逻辑
              ↓
        返回 HTML 给 Nginx → 返回给浏览器

部署提示

LNMP 部署顺序

php-fpm(先部署,准备 FastCGI 端口)
  → Nginx(配置 fastcgi_pass 转发)
  → MySQL(数据库存储)
  → WordPress(PHP 应用,配置数据库连接)

核心配置要点

Nginx 中转发 PHP 请求的关键配置:

location ~ \.php$ {
    fastcgi_pass   127.0.0.1:9000;          # php-fpm 监听地址
    fastcgi_param  SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
    include        fastcgi_params;
}
配置项说明
fastcgi_pass 127.0.0.1:9000将 PHP 请求转发给本机 php-fpm(9000 端口)
fastcgi_param SCRIPT_FILENAME告诉 php-fpm 要执行的 PHP 文件完整路径
include fastcgi_params加载 FastCGI 协议默认参数

常见排错

错误可能原因排查方法
访问 PHP 页面空白或下载Nginx 未正确配置 FastCGI 转发检查 fastcgi_pass 和 fastcgi_param
502 Bad Gatewayphp-fpm 未启动或端口不对systemctl status php74-php-fpm,检查 9000 端口
数据库连接失败MySQL 未启动或账号错误检查 MySQL 服务状态、账号权限和密码
无法安装插件/上传图片目录权限不足chown -R nginx:nginx /usr/share/nginx/html/wordpress
403 Forbidden索引文件缺失或权限不足检查 index.php 是否存在,Nginx 用户有读取权限

排错核心思路

  1. 查端口:netstat -tlnp | grep 端口号 确认服务是否在监听
  2. 查日志:/var/log/nginx/error.log、/var/log/php-fpm/ 等
  3. 查状态:systemctl status 确认服务运行状态

PHP-FPM 优化

慢日志(定位慢脚本)

# /etc/php-fpm.d/www.conf
request_slowlog_timeout = 5s     # php 脚本执行超过 5s 记录
slowlog = /var/log/php_slow.log  # 慢日志文件路径

定位逻辑:页面响应慢 → 看 php_slow.log 找出执行超过阈值的脚本 → 优化该脚本 SQL/逻辑。

状态页(监控 PHP-FPM 运行状态)

# 1. php-fpm 配置开启状态页
pm.status_path = /php_status
systemctl restart php-fpm
# 2. Nginx 代理状态页
location /php_status {
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}

访问 /php_status 返回的关键指标:

指标含义
pool池名称(www)
process manager进程管理模式(dynamic)
accepted conn已接受连接数
listen queue等待队列
idle processes空闲进程数
active processes活跃进程数
total processes总进程数
slow requests慢请求数

Nginx 到 PHP-FPM 长连接

location ~* \.php$ {
    fastcgi_pass php_server;
    fastcgi_keep_conn on;   # 复用与 PHP-FPM 的连接,减少握手开销
    include fastcgi_params;
}

参见