- CGI(Common Gateway Interface)。CGI是外部应用程序(CGI程序)与Web服务器之间的接口标准,
- 是在CGI程序和Web服务器之间传递信息的规程。CGI规范允许Web服务器执行外部程序,并将它们的输出发送给Web浏览器,
- CGI将Web的一组简单的静态超媒体文档变成一个完整的新的交互式媒体。
-
简单的说,就是:cgi就是专门用来和web 服务器打交道的。。web服务器收到用户请求,就会把请求提交给cgi程序(php的fastcgi), cgi程序根据请求提交的参数作应处理(解析php),然后输出标准的html语句返回给web服服务器,再返回给客户端,这就是普通cgi的工作原理。
cgi的好处就是完全独立于任何服务器,仅仅是做为中间分子。提供接口给apache和php。他们通过cgi搭线来完成搞基动作。
但是cgi有个蛋疼的地方,就是每一次web请求都会有启动和退出过程,也就是最为人诟病的fork-and-execute模式,这样一在大规模并发下,就死翘 翘了。所以。这个时候fastcgi运用而生了。它事先就早早的启动好了,而且可以启动多个cgi模块,在那里一直运行着等着,等着web发过来的请求,然后再给php解析运算完成生成html给web后,也不会退出,而且继续等着下一个web请求。而且这些cgi的模块启动是可控的,可监测的。这种技术还允许把web server和php运行在不同的主机上,以大规模扩展和改进安全性而不损失生产效率。
所以现在一般操作系统都是fastcgi模式。cig模式也慢慢退出了历史舞台!我们文章中说cgi一般也就指fastcgi。所以把这种运行方式叫做mod_fastcgi模式
php-fpm就是专门来辅助mode_fastcgi模式的。fastcgi 是一个与平台无关,与语言无关,任何语言只要按照它的接口来实现,就能实现自己语言的fastcgi能力和web server 通讯。
PHP-CGI就是PHP实现的自带的FastCGI管理器。
虽然是php官方出品,自带的,但是这丫的却一点也不给力,性能太差,而且也很麻烦不人性化,主要体现在:
- 1.php-cgi变更php.ini配置后需重启php-cgi才能让新的php-ini生效,不可以平滑重启。
- 2.直接杀死php-cgi进程,php就不能运行了。
-
上面2个问题,一直让很多人病垢了很久,所以很多人一直还是在用mode_php方式。
直到 2004年(确定是这么早吗?)一个叫 Andrei Nigmatulin的屌丝发明了PHP-FPM ,这神器的出现就彻底打破了这种局面,这是一个PHP专用的 fastcgi管理器,它很爽的克服了上面2个问题,而且,还表现在其他方面更表现强劲。请戳官网。
PHP在 5.3.3 之后已经将php-fpm写入php源码核心了。所以已经不需要另外下载了。(php-fpm占用的是9000端口)
- 之前接说过php-fpm是独立于web服务器和php之前的一层服务器,所以,我们需要开机启动它
-
- 开机启动的配置文件是:/etc/rc.local ,加入 /usr/local/php/sbin/php-fpm 即可
-
- 我们在新安装扩展后,是需要重新php-fpm的,已使扩展生效。
- 最简单粗暴的重新php-fpm的方式是:
- 先找到php-fpm的进程号,kill 掉,再用/usr/local/php/sbin/php-fpm 这样启动。
- 其实还有更多温和的方法,就是使用信号。
-
- INT, TERM 立刻终止
- QUIT 平滑终止
- USR1 重新打开日志文件
- USR2 平滑重载所有worker进程并重新载入配置和二进制模块
-
- 示例:
- php-fpm 关闭:
- kill -INT `cat /usr/local/php/var/run/php-fpm.pid`
-
- php-fpm 重启:
- kill -USR2 `cat /usr/local/php/var/run/php-fpm.pid`
-
- [global]
- ; Pid file
- ; Note: the default prefix is /usr/local/var
- ; Default Value: none
- ;pid = run/php-fpm.pid #pid设置,一定要开启。因为没有默认值
-
- ; Error log file
- ; If it's set to "syslog", log is sent to syslogd instead of being written
- ; in a local file.
- ; Note: the default prefix is /usr/local/var
- ; Default Value: log/php-fpm.log
- ;error_log = log/php-fpm.log #错误日志
-
- ; Log level
- ; Possible Values: alert, error, warning, notice, debug
- ; Default Value: notice
- ;log_level = notice #错误级别。可用级别为
- ;alert(必须立即处理), error(错误情况), warning(警告情况), notice(一般重要信息), debug(调试信息). 默认: notice.
-
- ; If this number of child processes exit with SIGSEGV or SIGBUS within the time
- ; interval set by emergency_restart_interval then FPM will restart. A value
- ; of '0' means 'Off'.
- ; Default Value: 0
- ;emergency_restart_threshold = 0 #
-
- ; Interval of time used by emergency_restart_interval to determine when
- ; a graceful restart will be initiated. This can be useful to work around
- ; accidental corruptions in an accelerator's shared memory.
- ; Available Units: s(econds), m(inutes), h(ours), or d(ays)
- ; Default Unit: seconds
- ; Default Value: 0
- ;emergency_restart_interval = 0 #表示在emergency_restart_interval所设值内出现SIGSEGV或者SIGBUS错误的php-cgi进程数
- #如果超过 emergency_restart_threshold个,php-fpm就会优雅重启。
- #这两个选项一般保持默认值。0 表示 '关闭该功能'. 默认值: 0 (关闭)。
-
- ; Time limit for child processes to wait for a reaction on signals from master.
- ; Available units: s(econds), m(inutes), h(ours), or d(ays)
- ; Default Unit: seconds
- ; Default Value: 0
- ;process_control_timeout = 0 #设置子进程接受主进程复用信号的超时时间。
- #可用单位: s(秒), m(分), h(小时), 或者 d(天) 默认单位: s(秒)
-
- ; Send FPM to background. Set to 'no' to keep FPM in foreground for debugging.
- ; Default Value: yes
- ; daemonize = yes #后台执行fpm,默认值为yes
-
- ; The address on which to accept FastCGI requests.
- ; Valid syntaxes are:
- ; 'ip.add.re.ss:port' - to listen on a TCP socket to a specific IPv4 address on
- ; a specific port;
- ; '[ip:6:addr:ess]:port' - to listen on a TCP socket to a specific IPv6 address on
- ; a specific port;
- ; 'port' - to listen on a TCP socket to all addresses
- ; (IPv6 and IPv4-mapped) on a specific port;
- ; '/path/to/unix/socket' - to listen on a unix socket.
- ; Note: This value is mandatory.
- listen = 127.0.0.1:9000 #fpm监听端口,即nginx中php处理的地址,一般默认值即可。
- #可用格式为: 'ip:port', 'port', '/path/to/unix/socket'
- #如果nginx和php在不同的机器上,分布式处理,就设置ip这里就可以了。
-
- ; Set listen(2) backlog.
- ; Default Value: 511 (-1 on FreeBSD and OpenBSD)
- ;listen.backlog = 511 #backlog数,设置 listen 的半连接队列长度,-1表示无限制,由操作系统决定,此行注释掉就行。
-
- ; List of addresses (IPv4/IPv6) of FastCGI clients which are allowed to connect.
- ; Equivalent to the FCGI_WEB_SERVER_ADDRS environment variable in the original
- ; PHP FCGI (5.2.2+). Makes sense only with a tcp listening socket. Each address
- ; must be separated by a comma. If this value is left blank, connections will be
- ; accepted from any ip address.
- ; Default Value: any
- ;listen.allowed_clients = 127.0.0.1 #允许访问FastCGI进程的IP白名单,设置any为不限制IP,默认值是any。
- #如果要设置其他主机的nginx也能访问这台FPM进程,listen处要设置成本地可被访问的IP。
- #每个地址是用逗号分隔
-
- ; Set permissions for unix socket, if one is used. In Linux, read/write
- ; permissions must be set in order to allow connections from a web server. Many
- ; BSD-derived systems allow connections regardless of permissions.
- ; Default Values: user and group are set as the running user
- ; mode is set to 0660
- ;listen.owner = _www #unix socket设置选项,如果使用tcp方式访问,这里注释即可。
- ;listen.group = _www #unix socket设置选项,如果使用tcp方式访问,这里注释即可。
- ;listen.mode = 0660 #unix socket设置选项,如果使用tcp方式访问,这里注释即可。
- ; When POSIX Access Control Lists are supported you can set them using
- ; these options, value is a comma separated list of user/group names.
- ; When set, listen.owner and listen.group are ignored
- ;listen.acl_users =
- ;listen.acl_groups =
-
- ; Unix user/group of processes
- ; Note: The user is mandatory. If the group is not set, the default user's group
- ; will be used.
- user = _www #启动进程的用户
- group = _www #启动进程的用户组
-
- pm = dynamic #php-fpm进程启动模式,pm可以设置为static和dynamic和ondemand
- #如果选择static,则进程数就数固定的,由pm.max_children指定固定的子进程数。
- #如果选择dynamic,则进程数是动态变化的,由以下参数决定:
- pm.max_children = 5 #子进程最大数
- pm.start_servers = 2 #启动时的进程数(默认值为: min_spare_servers + (max_spare_servers - min_spare_servers) / 2)
- pm.min_spare_servers = 1 #保证空闲进程数最小值,如果空闲进程小于此值,则创建新的子进程
- pm.max_spare_servers = 3 #保证空闲进程数最大值,如果空闲进程大于此值,此进行清理
-
- ; The number of requests each child process should execute before respawning.
- ; This can be useful to work around memory leaks in 3rd party libraries. For
- ; endless request processing specify '0'. Equivalent to PHP_FCGI_MAX_REQUESTS.
- ; Default Value: 0
- ;pm.max_requests = 500 #设置每个子进程重生之前服务的请求数, 对于可能存在内存泄漏的第三方模块来说是非常有用的
- #如果设置为 '0' 则一直接受请求
-
- ; Default Value: not set
- ;pm.status_path = /status #FPM状态页面的网址. 如果没有设置, 则无法访问状态页面. 默认值: none. munin监控会使用到
-
- ; Default Value: not set
- ;ping.path = /ping #FPM监控页面的ping网址. 如果没有设置, 则无法访问ping页面。
- #该页面用于外部检测FPM是否存活并且可以响应请求. 请注意必须以斜线开头 (/)
-
- ; This directive may be used to customize the response of a ping request. The
- ; response is formatted as text/plain with a 200 response code.
- ; Default Value: pong
- ;ping.response = pong #用于定义ping请求的返回相应. 返回为 HTTP 200 的 text/plain 格式文本. 默认值: pong.
-
- ; The access log file
- ; Default: not set
- ;access.log = log/$pool.access.log #每一个请求的访问日志,默认是关闭的。
-
- ; Default: "%R - %u %t \"%m %r\" %s"
- ;access.format = "%R - %u %t \"%m %r%Q%q\" %s %f %{mili}d %{kilo}M %C%%" #设定访问日志的格式。
-
- ; The log file for slow requests
- ; Default Value: not set
- ; Note: slowlog is mandatory if request_slowlog_timeout is set
- ;slowlog = log/$pool.log.slow #慢请求的记录日志,配合request_slowlog_timeout使用,默认关闭
-
- ; The timeout for serving a single request after which a PHP backtrace will be
- ; dumped to the 'slowlog' file. A value of '0s' means 'off'.
- ; Available units: s(econds)(default), m(inutes), h(ours), or d(ays)
- ; Default Value: 0 #设置为 '0' 表示 'Off'
- ;request_slowlog_timeout = 0 #当一个请求该设置的超时时间后,就会将对应的PHP调用堆栈信息完整写入到慢日志中。
-
- ; 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 #设置为 '0' 表示 'Off'
- ;request_terminate_timeout = 0 #设置单个请求的超时中止时间.
- #该选项可能会对php.ini设置中的'max_execution_time'因为某些特殊原因没有中止运行的脚本有用
- #当经常出现502错误时可以尝试更改此选项。
-
- ; Set open file descriptor rlimit.
- ; Default Value: system defined value
- ;rlimit_files = 1024 #设置文件打开描述符的rlimit限制. 默认值: 系统定义值默认可打开句柄是1024
- #可使用 ulimit -n查看,ulimit -n 2048修改。
-
- ; Set max core size rlimit.
- ; Possible Values: 'unlimited' or an integer greater or equal to 0
- ; Default Value: system defined value
- ;rlimit_core = 0 #设置核心rlimit最大限制值. 可用值: 'unlimited' 、0或者正整数. 默认值: 系统定义值.
-
- ; Default Value: not set
- ;chroot = #启动时的Chroot目录. 所定义的目录需要是绝对路径. 如果没有设置, 则chroot不被使用.
-
- ; Chdir to this directory at the start.
- ; Note: relative path can be used.
- ; Default Value: current directory or / when chroot
- ;chdir = /var/www #设置启动目录,启动时会自动Chdir到该目录. 所定义的目录需要是绝对路径.
-
- ; Redirect worker stdout and stderr into main error log. If not set, stdout and
- ; stderr will be redirected to /dev/null according to FastCGI specs.
- ; Note: on highloaded environement, this can cause some delay in the page
- ; process time (several ms).
- ; Default Value: no
- ;catch_workers_output = yes #重定向运行过程中的stdout和stderr到主要的错误日志文件中。
- #如果没有设置, stdout 和 stderr 将会根据FastCGI的规则被重定向到 /dev/null .
-
在之前的文章中就说过了。在fasgcgi模式下,php会启动多个php-fpm进程,来接收nginx发来的请求,那是不是进程越多,速度就越快呢?这可不一定!得根据我们的机器配置和业务量来决定。
我们先来看下,设定进程的配置在哪里?
- pm = static | dynamic | ondemand
-
pm可以设置成这样3种,我们用的最多的就上前面2种。
1.pm = static 模式
pm = static 表示我们创建的php-fpm子进程数量是固定的,那么就只有pm.max_children = 50这个参数生效。你启动php-fpm的时候就会一起全部启动51(1个主+50个子)个进程,颇为壮观。
2.pm = dynamic 模式
pm = dynamic 模式,表示启动进程是动态分配的,随着请求量动态变化的。他由 pm.max_children,pm.start_servers,pm.min_spare_servers,pm.max_spare_servers 这几个参数共同决定。
上面已经讲过,这里再重申一下吧:
很多人恐惧症来袭,不知道选什么好?
一般原则是:动态适合小内存机器,灵活分配进程,省内存。静态适用于大内存机器,动态创建回收进程对服务器资源也是一种消耗。 如果你的内存很大,有8-20G,按照一个php-fpm进程20M算,100 个就 2G 内存了,那就可以开启 static 模式。如果你的内存很小,比如才256M,那就要小心设置了,因为你的机器里面的其他的进程也算需要占用内存的,所以设置成dynamic是最好的,比如:pm.max_chindren = 8, 占用内存160M左右,而且可以随时变化,对于一半访问量的网站足够了。
我们有时候会经常饱受 500、502 问题困扰。当 nginx 收到如上错误码时,可以确定后端 php-fpm 解析 php 出了某种问题,比如,执行错误,执行超时。 这个时候,我们是可以开启慢日志功能的。