Home >Backend Development >PHP Problem >What is the installation path of php-fpm?
The installation path of php-fpm: 1. In Linux platforms such as centos, it is in the "/usr/local/php/etc/" and "/usr/local/php/sbin/" directories; 2. On the mac platform, it is in the "/etc/" and "/usr/bin/" directories.
Recently researching and learning about the performance of php , saw factcgi and php-fpm, and found that I had very little understanding of them. It can be said that I almost knew nothing about them. It is quite scary to think about it. Decided to study this aspect carefully.
Referenced and studied the following articles:
1. Introduction, comparison, and performance data of mod_php, mod_fastcgi and php-fpm
2. Actual Nginx_replacement
How to To introduce fastcgi and php-fpm step by step, I will first talk about the surroundings of PHP bit by bit. Why. Suddenly I feel that life is so tiring!
php is a back-end language born for the web. Of course, we php dogs know it best. Therefore, PHP is only a back-end language, so it must rely on a web server to provide web functions. Of course, if other back-end languages are used for web applications, they must also rely on web servers. Okay, the web server is introduced from php, good!
So what are the common web servers? The most commonly used PHP dog is Apache, and there are others:
- apache
- nginx
- IIS
- lighttpd
- tomcat
Basically the above ones, the most commonly used ones associated with php are Apache and Nginx.
Let’s first use apache as a web server as an example to illustrate a complete PHP access situation:
The picture explains PHP well. A complete web access flow chart combining Apache with mysql database
It is clearly stated above that php must rely on the web server to provide web functional services. Now let’s take a look at what they are. How to become gay.
The one we use most is Apache. So recall, how to enable apache to recognize php code? Is it necessary to add or modify the following sentences in the apache configuration file httpd.conf:
//加入以下2句 LoadModule php5_module D:/php/php5apache2_2.dll AddType application/x-httpd-php .php //将下面的 <IfModule dir_module> DirectoryIndex index.html </IfModule> //将其修改为: <IfModule dir_module> DirectoryIndex index.html index.htm index.php index.phtml </IfModule>
The above manual configuration after installing php and apache environment under windows, the source code installation under Linux is roughly configured like this:
./configure --with-mysql=/usr/local --with-apache=/usr/local/apache --enable-track-vars
So, in this way, their common essence is to use LoadModule
to load php5_module
, which is to run php as a sub-module of apache. When accessing a php file through the web, apache will call php5_module
to parse the php code.
So how does php5_module
pass the data to the php parser to parse the php code?
The answer is through sapi
Let’s look at another picture and talk about apache, php and sapi in detail Relationship:
From the above figure, we can see that sapi
is such an intermediate process. SAPI provides an interface for external communication , somewhat similar to socket
, allowing PHP to interact with other applications (apache, nginx, cli, etc.). PHP provides many kinds of SAPI by default, including the common php5_module for apache and nginx, CGI, ISAPI for IIS, and Shell CLI.
So, the above apache call php execution process is as follows:
apache -> httpd -> php5_module -> sapi -> php
Okay. Let’s figure out apache and php through php5_module!
We call this mode of operation mod_php
mode
We have carefully mentioned above that php and apache pass php5_module, php5_module accesses php through sapi to achieve the entire process of php web.
As mentioned above, sapi is also mentioned. sapi
is a unified interface provided by php. It provides php5_module and cgi for web servers to link and parse php code. The php5_module
loading mode mentioned above is called the mod_php
mode.
So! Dang Dang Dang! Let’s talk about fastcgi mode soon. Hahahahaha, that’s too unacceptable.
Then another way of PHP's sapi is to provide cgi mode. Since cgi is relatively old, fastcgi appeared to replace it.
So, hey. No way, what is CGI again?
CGI (Common Gateway Interface). CGI is an interface standard between external applications (CGI programs) and Web servers. It is a procedure for transferring information between CGI programs and Web servers. The CGI specification allows Web servers to execute external programs and send their output to Web browsers. CGI turns the Web's simple set of static hypermedia documents into a complete new interactive media.
It hurts to read the official explanation. Simply put, it is: cgi is specially used to deal with web servers. When the web server receives a user request, it will submit the request to the cgi program (php's fastcgi). The cgi program will process (parse php) according to the parameters submitted in the request, and then output a standard html statement and return it to the web server, and then return To the client, this is how normal cgi works.
The advantage of cgi is that it is completely independent of any server and only acts as an intermediary. Provides interfaces to apache and php. They used CGI wiring to complete the gay action. The advantage of this is to minimize the association between the two and make them more independent.
But there is a troublesome thing about cgi, that is, every web request will have a startup and exit process, which is the most criticized fork-and-execute
mode. When the scale of concurrency is high, it will be a dead end.
so. At this time fastcgi
application was born. It is started early in advance, and can start multiple cgi modules. It keeps running there, waiting for requests from the web, and then completes the parsing operation of php to generate html for the web, and it will not exit. , and continue to wait for the next web request. Moreover, the startup of these CGI modules is controllable and monitorable. This technology also allows the web server and PHP to be run on different hosts to massively scale and improve security without losing productivity.
So now most operating systems are in fastcgi mode. The cig mode has also slowly withdrawn from the stage of history! When we say cgi in our article, it generally refers to fastcgi.
So this mode of operation is called mod_fastcgi
Mode
I will talk about how to use fastcgi mode to connect php and apache in the next paragraph (or nginx)
To summarize: When php is combined with apache or ngix, sapi will be used to provide 2 connection methods: mod_php and mod_fastcgi
. mod_php
The mode will install the php module to run under apache, and the two have a greater degree of integration. mod_fastcgi
The mode is an intermediate process. After apache introduces the user request, it sends it to fastcgi, and then connects to php to complete the access.
mod_php mode installs the php module into apache, so every time apache terminates the request, a message will be generated Process, this process completely includes various calculations and other operations of PHP.
We can clearly see from the picture that every time apache receives a request, a process will be generated to connect to php to complete the request through sapi. As you can imagine, If there are too many users and too many concurrent users, the server will be unable to bear it.
Moreover, when mod_php is compiled into apache, it is difficult to determine whether it is a problem with PHP or apache when a problem occurs.
mod_fastcgi mode is just the opposite. Fastcgi is an independent entity independent of apache and php. It starts with apache and generates multiple cig modules, waiting for apache's request. :
In the picture, fastcgi has been started early, and it is waiting quietly. The httpd request sent by apache is immediately received, and is sent by calling sapi. php, complete the operation. And won't quit. This can handle large-scale concurrent requests, because the web server has less to do, so it can process the next request faster, which greatly improves concurrency.
Because apache and php are independent. If there is a problem, it is easy to locate where the problem is. This is one of the reasons why this model is popular.
I have done a lot, and finally want to talk about php-fpm. ^....^
Let’s get straight to the point and talk about what php-fpm is good for. It is specially designed to assist the mode_fastcgi
mode.
Um. Very good, let's go back to mode_fastcgi
mode after knowing what it does. Through the previous explanations, I have figured out what this mode looks like.
fastcgi is platform-independent and language-independent. As long as any language is implemented according to its interface, it can realize the fastcgi capability of its own language and communicate with the web server.
PHP-CGI is the FastCGI manager implemented by PHP.
Although it is an official product of PHP and comes with it, it is not powerful at all, the performance is too poor, and it is also very troublesome and impersonal, mainly reflected in:
- php-cgi After changing the php.ini configuration, you need to restart php-cgi to make the new php-ini take effect, and smooth restart is not possible.
- Kill the php-cgi process directly, and php will not be able to run.
The above two problems have troubled many people for a long time, so many people still use the mode_php
method.
直到 2004年(确定是这么早吗?)一个叫 Andrei Nigmatulin的屌丝发明了PHP-FPM ,这神器的出现就彻底打破了这种局面,这是一个PHP专用的fastcgi管理器,它很爽的克服了上面2个问题,而且,还表现在其他方面更表现强劲. 请戳官网
我擦,这一篇貌似又瞎比比的说超时了啊。好吧。那windows和linux下安装配置php-fpm就下一节来说吧。反正我已经已经把php-fpm和fastcgi给讲清楚了。
===============================================================================
在前面我学习过了php中的mod_php模式
和mode_fastcgi和php-fpm模式
:地址,中大致的讲述了几种模式的区别,也明白了php-fpm是fastcgi模式的管理器。今天就来看下php如何安装php-fpm,以及运行。
我的机器是centos 6.2 之前就已经安装过了php 5.4.11,PHP在 5.3.3 之后已经讲php-fpm写入php源码核心了。所以已经不需要另外下载了。我这里是5.4.11所以就可以直接用。
由于我php已经安装好了,而且之前编译的时候没有带上fpm模式,所以我必须找到源码重新编译一下:
要想使php支持php-fpm,只需要在编译的时候带上
--enable-fpm
就可以了。
所以,我需要找到之前的编译参数, 后面加上--enable-fpm
,重新编译下就可以了。之前就讲过,有2种方式可以找到之前的编译参数:
- 在源码 /lamp/php-5.4.11/中找到
config.nice
,这个就是之前的编译参数- 在php.ini配置文件中找到
Configure
相关的配置 :/usr/local/php/bin/php -i |grep 'Configure'
好,我们开始,找到之前的编译参数:
[root@localhost /]# cd /lamp/php-5.4.11 & vi config.nice './configure' \ '--prefix=/usr/local/php' \ '--with-config-file-path=/usr/local/php/etc/' \ '--with-apxs2=/usr/local/apache/bin/apxs' \ '--with-mysql=/usr/local/mysql/' \ '--with-libxml-dir=/usr/local/libxml2/' \ '--with-png-dir=/usr/local/libpng/' \ '--with-jpeg-dir=/usr/local/jpeg8/' \ '--with-freetype-dir=/usr/local/freetype/' \ '--with-gd=/usr/local/gd/' \ '--with-zlib-dir=/usr/local/zlib/' \ '--with-mcrypt=/usr/local/libmcrypt/' \ '--with-mysqli=/usr/local/mysql/bin/mysql_config' \ '--enable-soap' \ '--enable-mbstring=all' \ '--enable-sockets' \
加上--enable-fpm
后,重新编译:
[root@localhost /]# cd /lamp/php-5.4.11 [root@localhost php-5.4.11]# './configure' \ '--prefix=/usr/local/php' \ '--with-config-file-path=/usr/local/php/etc/' \ '--with-apxs2=/usr/local/apache/bin/apxs' \ '--with-mysql=/usr/local/mysql/' \ '--with-libxml-dir=/usr/local/libxml2/' \ '--with-png-dir=/usr/local/libpng/' \ '--with-jpeg-dir=/usr/local/jpeg8/' \ '--with-freetype-dir=/usr/local/freetype/' \ '--with-gd=/usr/local/gd/' \ '--with-zlib-dir=/usr/local/zlib/' \ '--with-mcrypt=/usr/local/libmcrypt/' \ '--with-mysqli=/usr/local/mysql/bin/mysql_config' \ '--enable-soap' \ '--enable-mbstring=all' \ '--enable-sockets' \ '--enable-fpm' [root@localhost php-5.4.11] make && make install
安装完成之后,我们尝试着启动:
启动命令是:
/usr/local/php/sbin/php-fpm
报错了:
[26-Feb-2015 15:39:55] ERROR: failed to open configuration file '/usr/local/php/etc/php-fpm.conf': No such file or directory (2) [26-Feb-2015 15:39:55] ERROR: failed to load configuration file '/usr/local/php/etc/php-fpm.conf' [26-Feb-2015 15:39:55] ERROR: FPM initialization failed
错误信息说找不到 php-fpm.conf
哦,原来是php-fpm.conf还没有,我们到 /usr/local/php/etc目录下将php-fpm.conf.default
拷贝也一份成php-fpm.conf
cd /usr/local/php/etc/cp php-fpm.conf.default php-fpm.conf
编辑一下这个配置文件:
vim php-fpm.conf pid = run/php-fpm.pid user = www group = www
再次尝试启动:
/usr/local/php/sbin/php-fpm
再次报错说www 用户不存在:
[26-Feb-2015 15:57:38] ERROR: [pool www] cannot get uid for user 'www' [26-Feb-2015 15:57:38] ERROR: FPM initialization failed
好,那我们新建www 用户组:
groupadd wwwuseradd -g www www
再次启动:
/usr/local/php/sbin/php-fpm
没有任何的输出,表示成功了!!!
php-fpm占用的是9000端口,我们查看下进程:
[root@localhost php-5.4.11]# ps -ef|grep php-fpm root 1377 1231 0 11:19 pts/1 00:00:00 grep php-fpm root 29249 1 0 06:22 ? 00:00:00 php-fpm: master process (/usr/local/php/etc/php-fpm.conf) www 29250 29249 0 06:22 ? 00:00:00 php-fpm: pool www www 29251 29249 0 06:22 ? 00:00:00 php-fpm: pool www root 32132 6158 0 08:25 pts/2 00:00:00 vi php-fpm.conf
[root@localhost php-5.4.11]# netstat -tnl | grep 9000 tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN [root@localhost php-5.4.11]#
好,安装和启动都OK了。
之前接说过php-fpm是独立于web服务器和php之前的一层服务器,所以,我们需要开机启动它
开机启动的配置文件是:/etc/rc.local
,加入 /usr/local/php/sbin/php-fpm
即可
[root@localhost init]# vi /etc/rc.local 1 #!/bin/sh 2 # 3 # This script will be executed *after* all the other init scripts. 4 # You can put your own initialization stuff in here if you don't 5 # want to do the full Sys V style init stuff. 6 7 touch /var/lock/subsys/local 8 /usr/local/apache/bin/apachectl start 9 /usr/local/bin/redis-server /etc/redis.conf 10 /usr/local/php/sbin/php-fpm
光安装好php-fpm也是没用的,得配合web服务器使用,下一节,我要学习nginx的安装,以及nginx连接php-fpm来使用php。
我们在新安装扩展后,是需要重新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`
===============================================================================
我在前面几篇中,很详细的讲述了php-fpm的各种介绍,和安装。今天来看一下它的配置文件php-fpm
的各种配置以及一些常见的优化。
下面是我的平时的环境搭建php的各种安装目录,大家的基本也差不多。
/usr/local/php/php /usr/local/php/etc/php.ini /usr/local/php/sbin/php-fpm /usr/local/php/etc/php-fpm.conf
/usr/bin/php /etc/php.ini /usr/bin/php-fpm /etc/php-fpm.conf
由于我开发以Mac为主,所以就用Mac的环境配置来学习。
这是搜索的一份还算算比较详细的php-fpm.conf
配置详解,我会针对性的修改下,当然php手册上也有详细的讲解:http://php.net/manual/zh/install.fpm.configuration.php
pid = /usr/local/var/run/php-fpm.pid #pid设置,一定要开启,上面是Mac平台的。默认在php安装目录中的var/run/php-fpm.pid。比如centos的在: /usr/local/php/var/run/php-fpm.pid error_log = /usr/local/var/log/php-fpm.log #错误日志,上面是Mac平台的,默认在php安装目录中的var/log/php-fpm.log,比如centos的在: /usr/local/php/var/log/php-fpm.log log_level = notice #错误级别. 上面的php-fpm.log纪录的登记。可用级别为: 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就会优雅重启。这两个选项一般保持默认值。0 表示 '关闭该功能'. 默认值: 0 (关闭). 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'. 每个进程池都需要设置。如果nginx和php在不同的机器上,分布式处理,就设置ip这里就可以了。 listen.backlog = -1 #backlog数,设置 listen 的半连接队列长度,-1表示无限制,由操作系统决定,此行注释掉就行。backlog含义参考:http://www.3gyou.cc/?p=41 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 #启动进程的用户和用户组,FPM 进程运行的Unix用户, 必须要设置。用户组,如果没有设置,则默认用户的组被使用。 pm = dynamic #php-fpm进程启动模式,pm可以设置为static和dynamic和ondemand #如果选择static,则进程数就数固定的,由pm.max_children指定固定的子进程数。 #如果选择dynamic,则进程数是动态变化的,由以下参数决定: pm.max_children = 50 #子进程最大数 pm.start_servers = 2 #启动时的进程数,默认值为: min_spare_servers + (max_spare_servers - min_spare_servers) / 2 pm.min_spare_servers = 1 #保证空闲进程数最小值,如果空闲进程小于此值,则创建新的子进程 pm.max_spare_servers = 3 #,保证空闲进程数最大值,如果空闲进程大于此值,此进行清理 pm.max_requests = 500 #设置每个子进程重生之前服务的请求数. 对于可能存在内存泄漏的第三方模块来说是非常有用的. 如果设置为 '0' 则一直接受请求. 等同于 PHP_FCGI_MAX_REQUESTS 环境变量. 默认值: 0. pm.status_path = /status #FPM状态页面的网址. 如果没有设置, 则无法访问状态页面. 默认值: none. munin监控会使用到 ping.path = /ping #FPM监控页面的ping网址. 如果没有设置, 则无法访问ping页面. 该页面用于外部检测FPM是否存活并且可以响应请求. 请注意必须以斜线开头 (/)。 ping.response = pong #用于定义ping请求的返回相应. 返回为 HTTP 200 的 text/plain 格式文本. 默认值: pong. access.log = log/$pool.access.log #每一个请求的访问日志,默认是关闭的。 access.format = "%R - %u %t \"%m %r%Q%q\" %s %f %{mili}d %{kilo}M %C%%" #设定访问日志的格式。 slowlog = log/$pool.log.slow #慢请求的记录日志,配合request_slowlog_timeout使用,默认关闭 request_slowlog_timeout = 10s #当一个请求该设置的超时时间后,就会将对应的PHP调用堆栈信息完整写入到慢日志中. 设置为 '0' 表示 'Off' request_terminate_timeout = 0 #设置单个请求的超时中止时间. 该选项可能会对php.ini设置中的'max_execution_time'因为某些特殊原因没有中止运行的脚本有用. 设置为 '0' 表示 'Off'.当经常出现502错误时可以尝试更改此选项。 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 . 默认值: 空.
当然还有一些无关紧要的设置,用到了再说吧。
在之前的文章中就说过了。在fasgcgi模式下,php会启动多个php-fpm进程,来接收nginx发来的请求,那是不是进程越多,速度就越快呢?这可不一定!得根据我们的机器配置和业务量来决定。
我们先来看下,设定进程的配置在哪里?
pm = static | dynamic | ondemand
pm可以设置成这样3种,我们用的最多的就上前面2种。
pm = static
表示我们创建的php-fpm子进程数量是固定的,那么就只有pm.max_children = 50
这个参数生效。你启动php-fpm的时候就会一起全部启动51(1个主+50个子)个进程,颇为壮观。
pm = dynamic
模式,表示启动进程是动态分配的,随着请求量动态变化的。他由 pm.max_children
,pm.start_servers
,pm.min_spare_servers
,pm.max_spare_servers
这几个参数共同决定。
上面已经讲过,这里再重申一下吧:
pm.max_children = 50
是最大可创建的子进程的数量。必须设置。这里表示最多只能50个子进程。
pm.start_servers = 20
随着php-fpm一起启动时创建的子进程数目。默认值:min_spare_servers + (max_spare_servers - min_spare_servers) / 2。这里表示,一起启动会有20个子进程。
pm.min_spare_servers = 10
设置服务器空闲时最小php-fpm进程数量。必须设置。如果空闲的时候,会检查如果少于10个,就会启动几个来补上。
pm.max_spare_servers = 30
设置服务器空闲时最大php-fpm进程数量。必须设置。如果空闲时,会检查进程数,多于30个了,就会关闭几个,达到30个的状态。
很多人恐惧症来袭,不知道选什么好?
一般原则是:动态适合小内存机器,灵活分配进程,省内存。静态适用于大内存机器,动态创建回收进程对服务器资源也是一种消耗。
如果你的内存很大,有8-20G,按照一个php-fpm进程20M算,100个就2G内存了,那就可以开启static模式。如果你的内存很小,比如才256M,那就要小心设置了,因为你的机器里面的其他的进程也算需要占用内存的,所以设置成dynamic是最好的,比如:pm.max_chindren = 8, 占用内存160M左右,而且可以随时变化,对于一半访问量的网站足够了。
我们有时候会经常饱受500,502问题困扰。当nginx收到如上错误码时,可以确定后端php-fpm解析php出了某种问题,比如,执行错误,执行超时。
这个时候,我们是可以开启慢日志功能的。
slowlog = /usr/local/var/log/php-fpm.log.slow request_slowlog_timeout = 15s
当一个请求该设置的超时时间15秒后,就会将对应的PHP调用堆栈信息完整写入到慢日志中。
php-fpm慢日志会记录下进程号,脚本名称,具体哪个文件哪行代码的哪个函数执行时间过长:
[21-Nov-2013 14:30:38] [pool www] pid 11877 script_filename = /usr/local/lnmp/nginx/html/www.quancha.cn/www/fyzb.php [0xb70fb88c] file_get_contents() /usr/local/lnmp/nginx/html/www.quancha.cn/www/fyzb.php:2
通过日志,我们就可以知道第2行的file_get_contents 函数有点问题,这样我们就能追踪问题了。
The above is the detailed content of What is the installation path of php-fpm?. For more information, please follow other related articles on the PHP Chinese website!