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
For the configuration details of the PHP-FPM process pool, please see http://php.net/manual/install.fpm.configuration.php
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上
Production environment configuration:
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
php 5.3.3 and later php-fpm no longer supports /usr/local/php/ that php-fpm had before sbin/php-fpm (start|stop|reload) and other commands, so don’t look at this old-fashioned command anymore, you need to use signal control:
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!

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version
SublimeText3 Linux latest version

Notepad++7.3.1
Easy-to-use and free code editor

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6
Visual web development tools
