PHP-FPM (PHP FastCGI Process Manager) means: PHP FastCGI process manager, software used to manage the PHP process pool and used to accept requests from web servers.
Function
PHP-FPM provides a better PHP process management method, which can effectively control memory and processes, and smoothly reload PHP configuration.
[Related recommendations: PHP Tutorial]
(1). Why does php-fpm
fpm appear all because of php-fastcgi. A program implemented to manage php-fastcgi well
(2). What is php-fastcgi
php-fastcgi is just a cgi program that only parses php requests and returns As a result, it won't be managed (hence php-fpm).
(3)Why not call it php-cgi
In fact, there was a php-cgi before php-fastcgi appeared, but its execution efficiency was low, so it was replaced by php-fastcgi .
(4)What is the difference between fastcgi and cgi?
Dear friends, the difference is huge. When a service web-server (nginx) distributes a request, it knows that the request is a dynamic php request by matching the suffix, and will forward the request to php.
In the era of CGI, the thinking was relatively conservative. After a request came in, the basic configuration information in php.ini was read, the execution environment was initialized, and a process was created every time. Reading the configuration, initializing the environment, returning data, and exiting the process, over time, the work of starting the process becomes tedious and particularly tiring.
When PHP came to the era of 5, everyone was particularly disgusted with this way of working. People who wanted to be lazy would desperately think, can I let cgi start one main process (master) at a time and make it read-only? Get the configuration once and then start multiple worker processes. When a request comes, it is passed to the worker through the master to avoid duplication of work. So fastcgi was born.
(5) Fastcgi is so good, what should I do if the started workers run out?
When there are not enough workers, the master will dynamically start the workers through the information in the configuration, and can take back the workers when they are idle
(6) I still don’t understand what php-fpm is?
It is to manage the program that starts a master process and multiple worker processes. PHP-FPM will create a main process to control when and how to forward HTTP requests to one or more child processes for processing.
PHP-FPM main process also controls when to create (handle more traffic from the Web application) and destroy (the child process has been running for too long or is no longer needed)PHP child processes. Each process in the PHP-FPM process pool exists longer than a single HTTP request and can handle 10, 50, 100, 500 or more HTTP requests.
Installation
PHP has incorporated php-fpm into the core code of PHP after 5.3.3. So php-fpm does not require a separate download and installation. If you want php to support php-fpm, you only need to bring --enable-fpm when compiling the php source code.
Global configuration
In Centos, the main configuration file of PHP-FPM is /etc/php7/php-fpm.conf. The specified sub-process fails within a specified period of time, PHP-FPM restarts:
#在指定的一段时间内,如果失效的PHP-FPM子进程数超过这个值,PHP-FPM主进程将优雅重启。 emergency_restart_threshold = 10 #设定emergency_restart_interval 设置采用的时间跨度。 emergency_restart_interval = 1m
Configure the process pool
The remainder of the PHP-FPM configuration file is an area called Pool Definitions. This area configures user settings for each PHP-FPM process pool. The PHP-FPM process pool is a series of related PHP sub-processes.
Usually a PHP application has its own process poolCentos introduces the process pool definition file at the top of the PHP-FPM main configuration file: include=/etc/php7/php-fpm.d/*.conf
for the PHP-FPM process pool.
user= nobody #拥有这个 PHP-FPM进程池中子进程的系统用户。要把这个设置的值设用的非根用户的用户名。 group = nobody #拥有这个 PHP-FPM进程池中子进程的系统用户组。要把这个设置的值设应用的非根用户所属的用户组名。 listen=[::]]:9000 #PHP-FPM进程池监听的IP地址和端口号,让 PHP-FPM 只接受 nginx从这里传入的请求。 listen. allowed clients =127.0.0.1 #可以向这个 PHP-FPM进程池发送请求的IP地址(一个或多个)。 pm.max children =51 #这个设置设定任何时间点 PHP-FPM进程池中最多能有多少个进程。这个设置没有绝对正确的值,你应该测试你的PHP应用,确定每个PHP进程需要使用多少内存,然后把这个设置设为设备可用内存能容纳的PHP进程总数。对大多数中小型PHP应用来说,每个PHP进程要使用5~15MB内存(具体用量可能有差异)。 假设我们使用设备为这个PHP-FPM进程池分配了512MB可用内存,那么可以把这个设置设为(512MB总内存)/(每个进程使用10MB) = 51个进程。 ...
Edit and save, restart the PHP-FPM main process: sudo systemctl restart php-fpm.service
Reference Company development environment
The configuration of the test environment is as follows: [www]
user = nobody #进程的发起用户和用户组,用户user是必须设置,group不是 nobody 任意用户
group = nobody
listen = [::]:9000 #监听ip和端口,[::] 代表任意ip
chdir = /app #在程序启动时将会改变到指定的位置(这个是相对路径,相对当前路径或chroot后的“/”目录)
pm = dynamic #选择进程池管理器如何控制子进程的数量 #static: 对于子进程的开启数路给定一个锁定的值(pm.max_children) #dynamic: 子进程的数目为动态的,它的数目基于下面的指令的值(以下为dynamic适用参数)
pm.max_children = 16 #同一时刻能够存货的最大子进程的数量
pm.start_servers = 4 #在启动时启动的子进程数量
pm.min_spare_servers = 2 #处于空闲"idle"状态的最小子进程,如果空闲进程数量小于这个值,那么相应的子进程会被创建
pm.max_spare_servers = 16 #最大空闲子进程数量,空闲子进程数量超过这个值,那么相应的子进程会被杀掉。
catch_workers_output = Yes #将worker的标准输出和错误输出重定向到主要的错误日志记录中,如果没有设置,根据FastCGI的指定,将会被重定向到/dev/null上
Forward the request to PHP-FPM
nginx as an example: server {
listen 83;
server_name mobile.com;
root /app/mobile/web/;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location / {
index index.html index.htm index.php;
# Redirect everything that isn't a real file to index.php
try_files $uri $uri/ /index.php$is_args$args;
}
#把HTTP请求转发给PHP-FPM进程池处理
location ~ .*\.php include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass 192.168.33.30:9000; #监听9000端口
fastcgi_index index.php;
try_files $uri =404;
#include fastcgi.conf;
}
location ~ /\.(ht|svn|git) {
deny all;
}
access_log /app/wwwlogs/access.log;
error_log /app/wwwlogs/error.log;
}
php-fpm starts, restarts, terminates Operation
Start php-fpm:/usr/sbin/php-fpm
或
/usr/local/php/sbin/php-fpm
The master process can understand the following signals
INT, TERM Terminate immediatelyQUIT Terminate smoothly
USR1 Reopen the log fileUSR2 Smoothly reload all worker processes and reload configuration and binary modulesCheck the master process number of php-fpm first
A simple Direct restart method:
# ps aux|grep php-fpm root 21891 0.0 0.0 112660 960 pts/3 R+ 16:18 0:00 grep --color=auto php-fpm root 42891 0.0 0.1 182796 1220 ? Ss 4月18 0:19 php-fpm: master process (/usr/local/php/etc/php-fpm.conf) nobody 42892 0.0 0.6 183000 6516 ? S 4月18 0:07 php-fpm: pool www nobody 42893 0.0 0.6 183000 6508 ? S 4月18 0:17 php-fpm: pool www
Restart php-fpm:
kill -USR2 42891
The above scheme generally does not generate php-fpm.pid file, if you want to generate php-fpm.pid, use the following solution:
上面master进程可以看到,matster使用的是/usr/local/php/etc/php-fpm.conf这个配置文件,cat /usr/local/php/etc/php-fpm.conf 发现:
[global] ; Pid file ; Note: the default prefix is /usr/local/php/var ; Default Value: none ;pid = run/php-fpm.pid
pid文件路径应该位于/usr/local/php/var/run/php-fpm.pid,由于注释掉,所以没有生成,我们把注释去除,再kill -USR2 42891 重启php-fpm,便会生成pid文件,下次就可以使用以下命令重启,关闭php-fpm了:
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编程从入门到精通
The above is the detailed content of Detailed explanation of what PHP-FPM is in PHP? What is the use?. For more information, please follow other related articles on the PHP Chinese website!