转 php-fpm配置 http://www.cnblogs.com/xiangxiaodong/p/3493039.html

php 5.3.6 中 php-fpm 配置 成 服务启动

从php5.3.3开始 源码中开始包含 php-fpm,不用专门再打补丁了,只需要解开源码直接configure, 关于php-fpm的编译参数有 –enable-fpm –with-fpm-user=www –with-fpm-group=www –with-libevent-dir=libevent位置。

这个php-fpm 不再支持 php-fpm 补丁具有的 /usr/local/php/sbin/php-fpm (start|stop|reload)等命令,需要使用信号控制:

master进程可以理解以下信号

SIGINT, SIGTERM 立刻终止 SIGQUIT 平滑终止 SIGUSR1 重新打开日志文件 SIGUSR2 平滑重载所有worker进程并重新载入配置和二进制模块

示例:

1
2
3
4
5
# php-fpm 关闭:
kill -SIGINT `cat /Data/apps/php/var/run/php-fpm.pid`

# php-fpm 重启:
kill -SIGUSR2 `cat /Data/apps/php/var/run/php-fpm.pid`

其次配置文件不再使用的xml 格式,改为了INI,但是配置参数几乎和以前一样,可参照xml格式的格式配置。


补充内容php 5.3.6 中 php-fpm 配置 成 服务启动

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
cp -f /tools/php-5.3.6/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
php-fpm的启动:
##vim /usr/local/webserver/php/etc/php-fpm.conf
php-fpm.pid 目录必须是:/usr/local/webserver/php/var/run/php-fpm.pid
 
chmod 755 /etc/init.d/php-fpm
/etc/init.d/php-fpm start
/sbin/chkconfig --add php-fpm (添加到开机服务列表)
/sbin/chkconfig php-fpm on
 
php-fpm的启动、停止和重启:
/etc/init.d/php-fpm start
/etc/init.d/php-fpm stop
 
/etc/init.d/php-fpm reload
 
启动效果如下:
 
Hai_ISD_7_9_Cent56_64:/usr/local/webserver/php/etc#service php-fpm start
Starting php-fpm  done
Hai_ISD_7_9_Cent56_64:/usr/local/webserver/php/etc#service php-fpm stop
Gracefully shutting down php-fpm . done
Hai_ISD_7_9_Cent56_64:/usr/local/webserver/php/etc#service php-fpm start
Starting php-fpm  done
Hai_ISD_7_9_Cent56_64:/usr/local/webserver/php/etc#service php-fpm reload
Reload service php-fpm  done

php-fpm配置文件参数

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/usr/local/php/etc/php-fpm.conf
 
pid = run/php-fpm.pid
pid设置,默认在安装目录中的var/run/php-fpm.pid,建议开启
 
error_log = log/php-fpm.log
错误日志,默认在安装目录中的var/log/php-fpm.log
 
log_level = notice
错误级别. 可用级别为: alert(必须立即处理), error(错误情况), warning(警告情况), notice(一般重要信息), debug(调试信息). 默认: notice.
 
emergency_restart_threshold = 60
emergency_restart_interval = 60s
表示在emergency_restart_interval所设值内出现SIGSEGV或者SIGBUS错误的php-cgi进程数如果超过emergency_restart_threshold个,php-fpm就会优雅重启。这两个选项一般保持默认值。
 
process_control_timeout = 0
设置子进程接受主进程复用信号的超时时间. 可用单位: s(秒), m(分), h(小时), 或者 d(天) 默认单位: s(秒). 默认值: 0.
 
daemonize = yes
后台执行fpm,默认值为yes,如果为了调试可以改为no。
 
在FPM中,可以使用不同的设置来运行多个进程池。 这些设置可以针对每个进程池单独设置。
 
listen = 127.0.0.1:9000
fpm监听端口,即nginx中php处理的地址,一般默认值即可。
可用格式为: ‘ip:port’, ‘port’, ‘/path/to/unix/socket’. 每个进程池都需要设置.
 
listen.backlog = -1
backlog数,-1表示无限制,由操作系统决定,此行注释掉就行。backlog含义参考:
 
listen.allowed_clients = 127.0.0.1
允许访问FastCGI进程的IP,设置any为不限制IP,如果要设置其他主机的nginx也能访问这台FPM进程,listen处要设置成本地可被访问的IP。默认值是any。
每个地址是用逗号分隔. 如果没有设置或者为空,则允许任何服务器请求连接
 
listen.owner = www
listen.group = www
listen.mode = 0666
unix socket设置选项,如果使用tcp方式访问,这里注释即可。
 
user = www
group = www
启动进程的帐户和组
 
pm = dynamic
如何控制子进程,选项有static和dynamic。
如果选择static,则由pm.max_children指定固定的子进程数。
如果选择dynamic,则由下开参数决定:
pm.max_children,子进程最大数
pm.start_servers,启动时的进程数
pm.min_spare_servers,保证空闲进程数最小值,如果空闲进程小于此值,则创建新的子进程
pm.max_spare_servers,保证空闲进程数最大值,如果空闲进程大于此值,此进行清理
对于专用服务器,pm可以设置为static。
 
pm.max_requests = 1000
设置每个子进程重生之前服务的请求数. 对于可能存在内存泄漏的第三方模块来说是非常有用的. 如果设置为 ’0′ 则一直接受请求. 等同于 PHP_FCGI_MAX_REQUESTS 环境变量. 默认值: 0.
 
pm.status_path = /status
FPM状态页面的网址. 如果没有设置, 则无法访问状态页面. 默认值: none.
 
ping.path = /ping
FPM监控页面的ping网址. 如果没有设置, 则无法访问ping页面. 该页面用于外部检测FPM是否存活并且可以响应请求. 请注意必须以斜线开头 (/)。
 
ping.response = pong
用于定义ping请求的返回相应. 返回为 HTTP 200 的 text/plain 格式文本. 默认值: pong.
 
request_terminate_timeout = 0
设置单个请求的超时中止时间. 该选项可能会对php.ini设置中的’max_execution_time’因为某些特殊原因没有中止运行的脚本有用. 设置为 ’0′ 表示 ‘Off’.
当经常出现502错误时可以尝试更改此选项。
 
request_slowlog_timeout = 10s
当一个请求该设置的超时时间后,就会将对应的PHP调用堆栈信息完整写入到慢日志中. 设置为 ’0′ 表示 ‘Off’
 
slowlog = log/$pool.log.slow
慢请求的记录日志,配合request_slowlog_timeout使用
 
rlimit_files = 1024
设置文件打开描述符的rlimit限制. 默认值: 系统定义值
系统默认可打开句柄是1024,可使用 ulimit -n查看,ulimit -n 2048修改。
 
rlimit_core = 0
设置核心rlimit最大限制值. 可用值: ‘unlimited’ 、0或者正整数. 默认值: 系统定义值.
 
chroot =
启动时的Chroot目录. 所定义的目录需要是绝对路径. 如果没有设置, 则chroot不被使用.
 
chdir =
设置启动目录,启动时会自动Chdir到该目录. 所定义的目录需要是绝对路径. 默认值: 当前目录,或者/目录(chroot时)
 
catch_workers_output = yes
重定向运行过程中的stdout和stderr到主要的错误日志文件中. 如果没有设置, stdout 和 stderr 将会根据FastCGI的规则被重定向到 /dev/null . 默认值: 空.

PHP低于5.3版本

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?xml version="1.0" ?>
<configuration>

  All relative paths in this config are relative to php's install prefix

  <section name="global_options">

    Pid file
    <value name="pid_file">/usr/local/webserver/php/logs/php-fpm.pid</value>

    Error log file
    <value name="error_log">/usr/local/webserver/php/logs/php-fpm.log</value>

    Log level
    <value name="log_level">notice</value>

    When this amount of php processes exited with SIGSEGV or SIGBUS ...
    <value name="emergency_restart_threshold">10</value>

    ... in a less than this interval of time, a graceful restart will be initiated.
    Useful to work around accidental curruptions in accelerator's shared memory.
    <value name="emergency_restart_interval">1m</value>

    Time limit on waiting child's reaction on signals from master
    <value name="process_control_timeout">5s</value>

    Set to 'no' to debug fpm
    <value name="daemonize">yes</value>

  </section>

  <workers>

    <section name="pool">

      Name of pool. Used in logs and stats.
      <value name="name">default</value>

      Address to accept fastcgi requests on.
      Valid syntax is 'ip.ad.re.ss:port' or just 'port' or '/path/to/unix/socket'
      <value name="listen_address">127.0.0.1:9000</value>

      <value name="listen_options">

        Set listen(2) backlog
        <value name="backlog">-1</value>

        Set permissions for unix socket, if one used.
        In Linux read/write permissions must be set in order to allow connections from web server.
        Many BSD-derrived systems allow connections regardless of permissions.
        <value name="owner"></value>
        <value name="group"></value>
        <value name="mode">0666</value>
      </value>

      Additional php.ini defines, specific to this pool of workers.
      <value name="php_defines">
        <value name="sendmail_path">/usr/sbin/sendmail -t -i</value>
        <value name="display_errors">0</value>
      </value>

      Unix user of processes
      <value name="user">www</value>

      Unix group of processes
      <value name="group">www</value>

      Process manager settings
      <value name="pm">

        Sets style of controling worker process count.
        Valid values are 'static' and 'apache-like'
        <value name="style">static</value>

        Sets the limit on the number of simultaneous requests that will be served.
        Equivalent to Apache MaxClients directive.
        Equivalent to PHP_FCGI_CHILDREN environment in original php.fcgi
        Used with any pm_style.
        <value name="max_children">128</value>

        Settings group for 'apache-like' pm style
        <value name="apache_like">

          Sets the number of server processes created on startup.
          Used only when 'apache-like' pm_style is selected
          <value name="StartServers">20</value>

          Sets the desired minimum number of idle server processes.
          Used only when 'apache-like' pm_style is selected
          <value name="MinSpareServers">5</value>

          Sets the desired maximum number of idle server processes.
          Used only when 'apache-like' pm_style is selected
          <value name="MaxSpareServers">35</value>

        </value>

      </value>

      The timeout (in seconds) for serving a single request after which the worker process will be terminated
      Should be used when 'max_execution_time' ini option does not stop script execution for some reason
      '0s' means 'off'
      <value name="request_terminate_timeout">0s</value>

      The timeout (in seconds) for serving of single request after which a php backtrace will be dumped to slow.log file
      '0s' means 'off'
      <value name="request_slowlog_timeout">0s</value>

      The log file for slow requests
      <value name="slowlog">logs/slow.log</value>

      Set open file desc rlimit
      <value name="rlimit_files">65535</value>

      Set max core size rlimit
      <value name="rlimit_core">0</value>

      Chroot to this directory at the start, absolute path
      <value name="chroot"></value>

      Chdir to this directory at the start, absolute path
      <value name="chdir"></value>

      Redirect workers' stdout and stderr into main error log.
      If not set, they will be redirected to /dev/null, according to FastCGI specs
      <value name="catch_workers_output">yes</value>

      How much requests each process should execute before respawn.
      Useful to work around memory leaks in 3rd party libraries.
      For endless request processing please specify 0
      Equivalent to PHP_FCGI_MAX_REQUESTS
      <value name="max_requests">1024</value>

      Comma separated list of ipv4 addresses of FastCGI clients that allowed to connect.
      Equivalent to FCGI_WEB_SERVER_ADDRS environment in original php.fcgi (5.2.2+)
      Makes sense only with AF_INET listening socket.
      <value name="allowed_clients">127.0.0.1</value>

      Pass environment variables like LD_LIBRARY_PATH
      All $VARIABLEs are taken from current environment
      <value name="environment">
        <value name="HOSTNAME">$HOSTNAME</value>
        <value name="PATH">/usr/local/bin:/usr/bin:/bin</value>
        <value name="TMP">/tmp</value>
        <value name="TMPDIR">/tmp</value>
        <value name="TEMP">/tmp</value>
        <value name="OSTYPE">$OSTYPE</value>
        <value name="MACHTYPE">$MACHTYPE</value>
        <value name="MALLOC_CHECK_">2</value>
      </value>

    </section>

  </workers>

</configuration>

PHP高于5.3版本

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
[global]
pid = /Data/apps/php/var/run/php-fpm.pid
error_log = /Data/apps/php/var/log/php-fpm.log
log_level = notice
[www]
listen = 127.0.0.1:9000
;listen.allowed_clients = 192.168.1.17,127.0.0.1,192.168.1.75
user = www
group = www
pm = static
pm.max_children = 128
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 1024
pm.process_idle_timeout = 10s
request_terminate_timeout = 30
request_slowlog_timeout = 30s
slowlog = /Data/apps/php/var/log/php-fpm.log.slow