PHP-FPM(FastCGI进程管理器)的配置与优化讲解

2019-03-1520:14:07服务器及运维Comments2,271 views字数 1430阅读模式

PHP-FPM(FastCGI Process Manager:FastCGI进程管理器)是一个PHPFastCGI管理器,用于管理 PHP 进程池、接收和处理 Web 服务器的请求。PHP-FPM 会创建一个主进程,控制何时以及如何把 HTTP 请求转发给一个或多个子进程处理。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9985.html

全局配置文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9985.html

emergency_restart_threshold = 60(建议值)

如果子进程在 emergency_restart_interval 设定的时间内收到该参数设定次数的 SIGSEGV 或者 SIGBUS退出信息号,则FPM会重新启动。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9985.html

emergency_restart_interval = 1m

用于设定平滑重启的间隔时,单位:s(秒),m(分),h(小时)或者 d(天)。默认单位:s(秒)。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9985.html

这个两个设置的作用是,如果在指定的一段时间内指定的子进程失效了,让 PHP-FPM 重启。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9985.html

这个两个设置默认是关闭的,建议开启,文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9985.html

进程池配置文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9985.html

PHP-FPM 中有一个 Pool Definitions 的区域,意为进程池,通常建议一个 PHP 应用使用一个进程池。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9985.html

PHP-FPM 配置文件有默认名为 www 的配置内容。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9985.html

user = 运行 PHP 的非 root 用户文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9985.html

子进程运行的用户。必须设置。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9985.html

group = 运行 PHP 的非 root 用户所属的用户组文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9985.html

子进程运行的用户组。如果没有设置,则默认用户的组被使用。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9985.html

listen = 127.0.0.1:9000或者/path/to/unix/socket文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9985.html

设置接受 FastCGI 请求的地址。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9985.html

listen.allowed_clients = 127.0.0.1文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9985.html

设置允许连接到 FastCGI 的服务器 IP 地址。默认情况,这个字段是注释掉了,建议开启。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9985.html

pm = 生产环境 static文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9985.html

设置进程管理器如何管理子进程。可用值:static,ondemand,dynamic。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9985.html

  • static:子进程的数量是固定的(pm.max_children)。
  • ondemand:进程在有需求时才产生(当请求时,与 dynamic 相反,pm.start_servers 在服务启动时即启动。
  • dynamic:子进程的数量在下面配置的基础上动态设置:pm.max_children,pm.start_servers,pm.min_spare_servers,pm.max_spare_servers。

pm.max_children =文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9985.html

设置 PHP-FPM 进程池中最多能有多少个进程。这个没有绝对正确值,要根据实际情况设置。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9985.html

假设每个 PHP 进程使用 20-30M 内存,为整个 PHP-FPM 环境配置了 512 M 内存,那么可以设置为 17-25 中间值。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9985.html

pm.start_servers =文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9985.html

设置启动时创建的子进程数目。仅在 pm 设置为 dynamic 时使用。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9985.html

pm.min_spare_servers =文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9985.html

设置空闲服务进程的最低数目。仅在 pm 设置为 dynamic 时使用。必须设置。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9985.html

pm.max_spare_servers =文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9985.html

设置空闲服务进程的最大数目。仅在 pm 设置为 dynamic 时使用。必须设置。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9985.html

pm.max_requests = 1000文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9985.html

设置每个子进程重生之前服务的请求数。对于可能存在内存泄漏的第三方模块来说是非常有用的。如果设置为 '0' 则一直接受请求,默认值:0。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9985.html

slowlog =文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9985.html

慢请求的记录日志。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9985.html

注意 PHP-FPM 进程所属的用户和用户组必须具有相应文件的写权限。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9985.html

request_slowlog_timeout = 5文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9985.html

当一个请求该设置的超时时间后,就会将对应的 PHP 调用堆栈信息完整写入到慢日志中。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9985.html

  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/yunwei/9985.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定