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!

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version
God-level code editing software (SublimeText3)