您的位置:首页 > 编程语言 > PHP开发

php-fpm介绍及配置

2012-08-17 10:37 363 查看
php-fpm是什么

全称是php fastcgi process manager即php fastcgi进程管理器,相比fastcgi静态的唤起cgi,fpm能根据访问的压力动态的唤起cgi进程和销毁以到达动态的调整cgi数量,这样可以有效的使用内存。除此之外还有其它的一些优点,比如,fpm还可以平滑的重载php配置;由于fpm是使用Unix-Socket来和服务器通讯,所以也不用再配置cgi端口;fpm有更好的状态输出和slowlog日志,502的时候能给出更多的错误细节。

php-fpm配置

从php5.3.3版本开始就已经集成了fpm,不再是第三方的包了,如果使用的是php5.3.3以前的版本就得自己去安装fpm包了,对于php5.2.x的版本,fpm的配置文件还是xml格式,php5.3.x开始fpm配置文件就已经支持ini格式了。

下面是一个ini格式的配置文件

;this configuration is for PHP 5.3.10 for heavy loaded servers

[global]
error_log = syslog
log_level = error
rlimit_files = 655360
rlimit_core = 0

[myapp]
user = nobody
group = nobody
listen = fpm.sock
listen.allowed_clients = 127.0.0.1

pm = dynamic
pm.max_children = 1000
pm.start_servers = 20
pm.min_spare_servers = 10
pm.max_spare_servers = 30
pm.max_requests = 500
; system max load (request number) = max_children * max_requests = 500,000

pm.status_path = /fpm_status
ping.path = /status
ping.response = ok

slowlog = /data/phplogs/phpfpm_slow.log
request_slowlog_timeout = 3s
;listen.backlog = 262144

; The timeout for serving a single request after which the worker process will
; be killed. This option should be used when the 'max_execution_time' ini option
; does not stop script execution for some reason. A value of '0' means 'off'.
; Available units: s(econds)(default), m(inutes), h(ours), or d(ays)
; Default Value: 0
;request_terminate_timeout = 0

rlimit_files = 655360
rlimit_core = 0

security.limit_extensions = .php .html

php_admin_value['date.timezone'] = 'Asia/Shanghai'

参数说明:

error_log string

错误日志的位置. 默认: 安装路径#INSTALL_PREFIX#/log/php-fpm.log.

log_level string

错误级别. 可用级别为: alert(必须立即处理), error(错误情况), warning(警告情况), notice(一般重要信息), debug(调试信息). 默认: notice.

emergency_restart_threshold int

如果子进程在emergency_restart_interval设定的时间内收到该参数设定次数的SIGSEGV 或者 SIGBUS退出信息号,则FPM会重新启动。 0 表示 ‘关闭该功能’. 默认值: 0 (关闭).

emergency_restart_interval mixed

emergency_restart_interval用于设定平滑重启的间隔时间. 这么做有助于解决加速器中共享内存的使用问题. 可用单位: s(秒), m(分), h(小时), 或者 d(天). 默认单位: s(秒). 默认值: 0 (关闭).

process_control_timeout mixed

设置子进程接受主进程复用信号的超时时间. 可用单位: s(秒), m(分), h(小时), 或者 d(天) 默认单位: s(秒). 默认值: 0.

daemonize boolean

设置FPM在后台运行. 设置 ‘no’ 将 FPM 保持在前台运行用于调试. 默认值: yes.

运行配置区段

在FPM中,可以使用不同的设置来运行多个进程池。 这些设置可以针对每个进程池单独设置。

listen string

设置接受FastCGI请求的地址. 可用格式为: ‘ip:port’, ‘port’, ‘/path/to/unix/socket’. 每个进程池都需要设置.

listen.backlog int

设置 listen(2) 的半连接队列长度. ‘-1′ 表示无限制. 默认值: -1.

listen.allowed_clients string

设置允许连接到FastCGI的服务器IPV4地址. 等同于PHP FastCGI (5.2.2+)中的 FCGI_WEB_SERVER_ADDRS环境变量. 仅对TCP监听起作用. 每个地址是用逗号分隔. 如果没有设置或者为空,则允许任何服务器请求连接. 默认值: any.

listen.owner string

如果使用,表示设置Unix套接字的权限. 在Linux中,读写权限必须设置,以便用于WEB服务器连接. 在很多BSD派生的系统中可以忽略权限允许自由连接. 默认值: 运行所使用的用户合租, 权限为0666.

listen.group string

参见 listen.owner.

listen.mode string

参见 listen.owner.

user string

FPM 进程运行的Unix用户. 必须设置.

group string

FPM 进程运行的Unix用户组. 如果没有设置,则默认用户的组被使用.

pm string

设置进程管理器如何管理子进程. 可用值: static, dynamic. 必须设置.

static – 子进程的数量是固定的 (pm.max_children).

dynamic – 子进程的数量在下面配置的基础上动态设置: pm.max_children, pm.start_servers, pm.min_spare_servers, pm.max_spare_servers.

pm.max_children int

子进程的数量,pm 设置为 static 时表示创建的, pm 设置为 dynamic 时表示最大可创建的. 必须设置.

该选项设置可以同时提供服务的请求数限制. 类似 Apache 的 mpm_prefork 中 MaxClients 的设置和 普通PHP FastCGI中的 PHP_FCGI_CHILDREN 环境变量.

pm.start_servers in

设置启动时创建的子进程数目. 仅在 pm 设置为 dynamic 时使用. 默认值: min_spare_servers + (max_spare_servers – min_spare_servers) / 2.

pm.min_spare_servers int

设置空闲服务进程的最低数目. 仅在 pm 设置为 dynamic 时使用. 必须设置.

pm.max_spare_servers int

设置空闲服务进程的最大数目. 仅在 pm 设置为 dynamic 时使用. 必须设置.

pm.max_requests int

设置每个子进程重生之前服务的请求数. 对于可能存在内存泄漏的第三方模块来说是非常有用的. 如果设置为 ’0′ 则一直接受请求. 等同于 PHP_FCGI_MAX_REQUESTS 环境变量. 默认值: 0.

pm.status_path string

FPM状态页面的网址. 如果没有设置, 则无法访问状态页面. 默认值: none.

ping.path string

FPM监控页面的ping网址. 如果没有设置, 则无法访问ping页面. 该页面用于外部检测FPM是否存活并且可以响应请求. 请注意必须以斜线开头 (/).

ping.response string

用于定义ping请求的返回相应. 返回为 HTTP 200 的 text/plain 格式文本. 默认值: pong.

request_terminate_timeout mixed

设置单个请求的超时中止时间. 该选项可能会对php.ini设置中的’max_execution_time’因为某些特殊原因没有中止运行的脚本有用. 设置为 ’0′ 表示 ‘Off’. Available units: s(econds)(default), m(inutes), h(ours), or d(ays). Default value: 0.

request_slowlog_timeout mixed

当一个请求该设置的超时时间后,就会将对应的PHP调用堆栈信息完整写入到慢日志中. 设置为 ’0′ 表示 ‘Off’. 可用单位: s(秒)(默认), m(分), h(小时), 或者 d(天). 默认值: 0.

slowlog string

慢请求的记录日志. 默认值: #INSTALL_PREFIX#/log/php-fpm.log.slow.

rlimit_files int

设置文件打开描述符的rlimit限制. 默认值: 系统定义值.

rlimit_core int

设置核心rlimit最大限制值. 可用值: ‘unlimited’ 、0或者正整数. 默认值: 系统定义值.

chroot string

启动时的Chroot目录. 所定义的目录需要是绝对路径. 如果没有设置, 则chroot不被使用.

chdir string

设置启动目录,启动时会自动Chdir到该目录. 所定义的目录需要是绝对路径. 默认值: 当前目录,或者/目录(chroot时).

catch_workers_output boolean

重定向运行过程中的stdout和stderr到主要的错误日志文件中. 如果没有设置, stdout 和 stderr 将会根据FastCGI的规则被重定向到 /dev/null . 默认值: 空.

nginx配置

如果使用unix-socket来与nginx配合,nginx中的配置也要做相应的更改

1. 在ngnix配置的server_name下面声明全局的fpm变量

server {
listen 80 ;
server_name xxx;
#在这里插入,设置为全局变量
set    $fpm_sock 'unix:/var/run/myapp/fpm.sock';
...
}
2. 然后改造所有 fastcgi_pass 字段

location = / {
include        fastcgi_params;
root           xxx/apps/fnt;
fastcgi_pass   $fpm_sock;
fastcgi_index  index.php;
fastcgi_param  SCRIPT_FILENAME  xxx/apps/fnt/index.php ;
}


fpm status

在nginx.conf里配置

server {
listen       8360;
server_name  localhost;

location /fpm_status {
if ( $arg_sid = '' ) {
rewrite ^ /fpm_list last;
}
include fastcgi_params;
fastcgi_pass unix:/var/run/$arg_sid/fpm.sock;
}
#列出所有正在运行的fpm-sock
location /fpm_list {
default_type text/html;
content_by_lua "
ngx.print('<h2>Runing FPM instances:</h2>')
local cmd = [[/bin/ls -1 /var/run/ | grep rgapp- | awk '{print \"<p><a href=?sid=\"$1\">\"$1\"</a>\"}']]
local f = io.popen(cmd, 'r')
local log = f:read('*a')
f:close()
ngx.print(log)
";
}
...
}
因为应用池里面会有多个应用,所以通过sid参数来参看指定应用的fpm状态。

通过地址http://xxx:8360/fpm_status?sid=myapp查看fpm状态

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: