search
HomeBackend DevelopmentPHP TutorialDirectly click on the PHP process manager php-fpm

Directly click on the PHP process manager php-fpm

What is php-fpm

php-fpm is a process manager for PHP. Many work processes under php are managed by the php-fpm process manager.

Related learning recommendations: PHP programming from entry to proficiency

The working principle of php-fpm

The full name of php-fpm is PHP FastCGI process manager. After php-fpm is started, it will first read php.ini, and then read the corresponding conf configuration file. The conf configuration can override the configuration of php.ini.
After starting php-fpm, a master process will be created to listen to port 9000 (configurable). The master process will create several sub-processes based on fpm.conf/www.conf. The sub-processes are used to handle actual business.
When a client (such as nginx) connects to port 9000, the idle child process will accept it by itself. If all child processes are busy, the new connection to be accepted will be put into the queue by the master and wait for fpm The child process is idle; the length of the queue that stores semi-connections waiting to be accepted is configured by listen.backlog.

How to view the php-fpm process and child processes

View all processes related to php-fpm. As shown in the picture below

here pool www are all sub-processes of php-fpm, which is what we often call the work process.

View the sub-processes under php-fpm

Through the above command, we can actually see the processes related to php-fpm. If we need to be more intuitive To view the master process and work process of php-fpm, you can view the process in the following way.
The 5370 here is the master process number of php-fpm. It can be clearly seen through the above command.

Through the above command, you can see that php-fpm is the master process and has 15 sub-processes below. The number of child processes here can be customized by the process. Process configuration through the following parameters:


pm = dynamic # 动态创建子进程
pm.max_children = 20 # 最大子进程数
pm.start_servers = 15 # 初始化php-fpm进程时,默认的子进程数

php-fpm parameter configuration instructions

php-fpm global configuration Parameters


#php-fpm的运行权限。
#以什么用户什么组的权限来运行池fpm。
user = www
group = www

#php-fpm的运行方式,可以使端口,也可以使socke文件。
#如果是端口则是走tcp,如果是socket则直接读socket文件,这样性能相对更好。
listen = 127.0.0.1:9000 

#拥有socket权限的用户,需要和上面的user、group配置相结合。
#如果采用的端口的方式,则不配置。
listen.owner = www
listen.group = www
listen.mode = 0660

#这是php-fpm端口连接的地址。多个用","隔开。默认任意地址都可以连接。
#例如Nginx和php-fpm不在同一台服务器上,这里的值就是Nginx服务的ip地址。
#当Nginx和php-fpm配置在同一台服务器上,则直接写127.0.0.1即可。
listen.allowed_clients = 127.0.0.1 

#pid进程文件存放的位置,当我们启用一个php服务,
#则会自动创建一个该pid文件,其实我们可以直接把该文件理解理解php-fpm的进程号文件,
#两则是等价的。默认为none。
pid = /opt/remi/php72/root/var/run/php-fpm/php-fpm.pid 

#错误日志位置,默认:安装路径 #INSTALL_PREFIX#/log/php-fpm.log。
#如果设置为syslog,log就会发送给syslogd服务而不会写进文件里。
error_log = /opt/remi/php72/root/var/log/php-fpm/error.log 

#PHP限制的文件扩展名
security.limit_extensions = .php .php3 .php4 .php5 .php7

#系统日志标示,如果跑了多个fpm进程,需要用这个来区分日志是谁的。
syslog.ident = php-fpm 

#日记登记,可选:alert, error, warning, notice, debug。
log_level = notice 

#紧急重启阈值,需要与下面emergency_restart_interval参数一起配置。
emergency_restart_threshold = 60 

# 紧急重启阈值的时间范围。在此参数设置的时间内,
# 出现SIGSEGV或SIGBUS的子进程数超过emergency_restart_threshold参数设置的值。
# 那么fpm就会优雅的重启,值是0表示off这个功能,可用的单位有:s秒,m分,h时,d天。
emergency_restart_interval = 60s 

#设置子进程接受主进程复用信号的超时时间。
process_control_timeout = 0 

#当动态管理子进程时,fpm最多能fork多少个进程,0表示无限制,
# 这是所有进程池能启动子进程的总和,谨慎使用。
process.max = 128 

#设置子进程的优先级,在master进程以root用户启动时有效;
#如果没有设置,子进程会继承master进程的优先级,值范围-19(最高)到20(最低),默认不设置。
process.priority = -19 

#设置成no用于调试bug,默认为yes。
daemonize = yes 

#master进程最多能打开的文件数量。默认采用系统设置的值。
rlimit_files = 1024 

#master进程核心rlimit限制值;可选unlimited或>=0的整数,默认为系统的值。
rlimit_core = 0

#事件处理机制,默认自动检测,可选值:select,poll,
#epoll(linux>=2.5.44),kqueue,/dev/poll,port
events.mechanism = epoll 

#fpm想系统发送状态的频率。单位有s,m,h。
#前提是fpm被设置会系统服务。
systemd_interval = 10s

Process pool configuration of php-fpm


#php-fpm的队列长度。
listen.backlog = 65535 

#php进程池权限,同样要master进程是root用户才有效,
#和上面的全局设置一样,不设置的话会继承master进程的优先级。
process.priority = -19 

#子进程管理方式
#static(静态配置,在启动php-fpm时根据该值创建固定的子进程数量);
#dynamic(动态配置,在启动php-fpm时根据pm.start_servers的值初始化对应的子进程数,至少一个子进程);
#ondemand(按需配置,在启动php-fpm时不创建子进程,而是根据请求动态fork子进程);
pm = dynamic 

#最大子进程数量
pm.max_children = 5 

#初始化子进程数量,与上面的pm = dynamic配置使用。
pm.start_servers = 2 

#服务器闲置时最少保持2个子进程,不够这个数就会创建,只适用动态dynamic管理方式
pm.min_spare_servers = 2 

#服务器闲置时最多要有几个,多了会kill,只适用动态dynamic管理方式
pm.max_spare_servers = 3 

#子进程闲置时间,也就是说子进程没有可处理的任务时,在该之间使就会被killed。
pm.process_idle_timeout = 10s

#每个子进程最大的处理请求数量。在一定程度上可以防止内存泄漏。
pm.max_requests = 500 

#php-fpm状态监控的uri
pm.status_path string

#php-fpm监控页面的 ping 网址。
#如果没有设置,则无法访问 ping 页面。
#该页面用于外部检测php-fpm是否存活并且可以响应请求。请注意必须以斜线开头(/)。
ping.path string

#用于定义ping请求的返回响应。返回为 HTTP 200 的 text/plain 格式文本。默认值:pong。
ping.response string

#设置worker的nice(2)优先级(如果设置了的话)。
#该值从 -19(最高优先级) 到 20(更低优先级)。 
#默认值:不设置
process.priority int

#检测路径时使用的前缀
prefix string

#访问文件日志,没啥用处,比如yii2每次都记录访问index.php,只是记录真实的PHP文件。
access.log = var/log/$pool.access.log 

#php的慢日志
slowlog = var/log/$pool.log.slow 

#慢日志时间阈值
request_slowlog_timeout = 2s 

#单个请求的超时时间,当php.ini设置的最大执行时间未生效,则交由它来处理。
request_terminate_timeout = 3s 

#最大打开句柄数,默认为系统值。
rlimit_files = 1024 

#最多的核心使用数,默认为系统分配。
rlimit_core = 0

Partial configuration demonstration

php-fpm’s backlog size setting

php-fpm’s backlog size setting is related to php-fpm’s processing capabilities. Rather, bigger is better.

When the value is set too large, php-fpm cannot handle it, and nginx will wait for a timeout, disconnect, and report a 504 gateway timeout error. At the same time, when php-fpm finished processing and prepared the write data for nginx, it found that the TCP connection was disconnected and reported "Broken pipe".

When this value is set too small, client requests such as nginx cannot enter the accept queue of php-fpm at all, and a "502 Bad Gateway" error is reported. Therefore, the size of the backlog must be determined based on the QPS of php-fpm. The best calculation method is QPS=backlog.

php-fpm startup mode

php-fpm starts by socket or port. These two methods are configured according to the actual situation.

nginx and php-fpm are on the same server. At this time, unix socket inter-process communication can be used directly instead of tcp port communication, which can save the time of creating a connection and thereby improve performance. The sock file can be created anywhere, as long as fpm has the permission to write the file in that directory and nginx has the permission to read it. The tcp connection will be more stable because the TCP protocol ensures the correctness of the data, but sock has fewer data copies and context switches, and consumes less resources. However, socket can only be used when nginx and fpm are on the same machine.

How to choose socket startup or port startup.

Because the tcp method has higher concurrency than the unix method, it is recommended to use the tcp method for projects with high concurrency. Now the Nginx configuration sample file defaults to the tcp method.

Using the unix method, the optimization point is to place the socket file under the /dev/shm directory. The general meaning is that the files under the directory are not stored on the hard disk, but are stored on the hard disk. in memory. As for hard disk reading and memory reading, who is faster and who is slower? Memory is definitely the fastest.

How to view the socket file when starting in socket mode.

The socket file is based on the pid configuration item mentioned above. We can directly use the cat command to view the process number.

The default number of child processes to start. Through the above pm = dynamic configuration, we know that this method dynamically configures the size of the child process. At the same time, we can also set the default number of child processes. Number of processes.


pm = dynamic
pm.max_children = 20
### 默认15个子进程,演示的效果就是上面的shell命令的结果图。
pm.start_servers = 15

When we try to set it to 3, the following error message is displayed.

Explanation, there is a certain relationship between the start_servers configuration item here and the min_spare_servers configuration. We set it to a minimum of 10, and php-fpm can be started normally.


The above is the detailed content of Directly click on the PHP process manager php-fpm. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:jb51. If there is any infringement, please contact admin@php.cn delete
如何使用php-fpm进行高性能调优如何使用php-fpm进行高性能调优Jul 08, 2023 am 11:30 AM

如何使用php-fpm进行高性能调优PHP是一种非常流行的服务器端脚本语言,广泛用于开发网页应用和动态网站。然而,随着访问量的增加,PHP应用程序的性能可能会受到影响。为了解决这个问题,我们可以使用php-fpm(FastCGIProcessManager)来进行高性能调优。本文将介绍如何使用php-fpm来提升PHP应用程序的性能,并提供代码示例。一、

如何使用PHP-FPM优化提高WooCommerce应用的性能如何使用PHP-FPM优化提高WooCommerce应用的性能Oct 05, 2023 am 08:24 AM

如何使用PHP-FPM优化提高WooCommerce应用的性能概述WooCommerce是一个非常流行的电子商务插件,用于在WordPress网站上创建和管理在线商店。然而,随着商店的增长和流量的增加,WooCommerce应用可能会变得缓慢和不稳定。为了解决这个问题,我们可以使用PHP-FPM来优化和提高WooCommerce应用的性能。什么是PHP-FP

利用php-fpm连接池提升数据库访问性能利用php-fpm连接池提升数据库访问性能Jul 07, 2023 am 09:24 AM

利用php-fpm连接池提升数据库访问性能概述:在Web开发中,数据库的访问是非常频繁且耗时的操作之一。传统的方法是每次数据库操作都新建一个数据库连接,使用完毕后再关闭连接。这种方式会造成数据库连接的频繁建立和关闭,增加了系统的开销。为了解决这个问题,可以利用php-fpm连接池技术来提升数据库访问性能。连接池的原理:连接池是一种缓存技术,将一定数量的数据库

php-fpm调优方法详解php-fpm调优方法详解Jul 08, 2023 pm 04:31 PM

PHP-FPM是一种常用的PHP进程管理器,用于提供更好的PHP性能和稳定性。然而,在高负载环境下,PHP-FPM的默认配置可能无法满足需求,因此我们需要对其进行调优。本文将详细介绍PHP-FPM的调优方法,并给出一些代码示例。一、增加进程数默认情况下,PHP-FPM只启动少量的进程来处理请求。在高负载环境下,我们可以通过增加进程数来提高PHP-FPM的并发

什么是php-fpm?如何进行优化来提升性能?什么是php-fpm?如何进行优化来提升性能?May 13, 2022 pm 07:56 PM

什么是php-fpm?下面本篇带大家了解一下php-fpm,介绍一下优化 php-fpm 时我们到底要优化什么,希望对大家有所帮助!

ubuntu没有php-fpm怎么办ubuntu没有php-fpm怎么办Feb 03, 2023 am 10:51 AM

ubuntu没有php-fpm的解决办法:1、通过执行“sudo apt-get”命令添加php的源地址;2、查看有没有php7的包;3、通过“sudo apt-get install”命令安装PHP;4、修改配置监听9000端口来处理nginx的请求;5、通过“sudo service php7.2-fpm start”启动“php7.2-fpm”即可。

利用php-fpm进程管理实现负载均衡利用php-fpm进程管理实现负载均衡Jul 09, 2023 pm 01:07 PM

利用php-fpm进程管理实现负载均衡随着互联网应用的日益复杂和用户量的增加,负载均衡成为一个不可或缺的技术。负载均衡的目标是将流量分配到多个服务器上,以提高系统的稳定性和性能。在PHP应用中,php-fpm(PHPFastCGIProcessManager)是一种常见的进程管理工具,可以被用于实现负载均衡,并且提供了灵活的配置选项。本文将介绍如何利用

php-fpm性能监控与调优策略php-fpm性能监控与调优策略Jul 07, 2023 am 08:39 AM

php-fpm性能监控与调优策略引言:随着互联网的发展,PHP作为一种高效的服务器端脚本语言,被广泛应用于Web开发领域。而php-fpm作为php运行环境的一种解决方案,具有较高的并发处理能力。然而,在高并发的情况下,php-fpm会面临性能瓶颈的问题。本文将介绍php-fpm的性能监控与调优策略,旨在提高php-fpm的性能和稳定性。一、php-fpm性

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SecLists

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.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!