Working principle of php FPM in php

1.CGI

It is a protocol for the interaction between the server and the background language. With this protocol, developers can use any language to process the requests forwarded by the server and dynamically generate content,

It ensures that the transmitted data is in standard format (specifies which data is transmitted in what format (URL, query string, POST data, HTTP header, etc.), which is convenient for developers.

2.fastCGI

First, FastCGI will start a master process, parse the configuration file, initialize the execution environment, and then start multiple worker processes. When the request comes, the master will pass it to a worker, and then the next request can be accepted immediately.
In this way, repeated labor is avoided, and the efficiency is naturally high.
Moreover, when the workers are not enough, the master can start several workers in advance according to the configuration.
Of course, when there are too many idle worker s, some will be stopped, which improves the performance and saves resources. This is the process management of FastCGI.


3.php-fpm

There is only one master process, which is responsible for listening to the port and receiving requests from the server, while there are generally multiple worker processes (the specific number is configured according to the actual needs), and a PHP interpreter is embedded in each process,

This is where the PHP code is actually executed. Here is the process of FPM on my local machine: 1 master process and 2 worker processes.

The specific process from receiving the request from FPM to processing is as follows

1). The master process of FPM received the request.

2). The master process assigns a specific worker process to process the request according to the configuration. If there is no available process, an error will be returned. This is also the reason why we encounter more 502 errors with Nginx.

3). The worker process processes the request. If it times out, it returns a 504 error.

4). The request processing ends and the result is returned.

4 PHP FPM configuration file

pid = run/php-fpm.pid
#pid setting. The default setting is var / run / PHP FPM pid, recommended on
 
error_log = log/php-fpm.log
#The error log is var / log / PHP fpm.exe in the installation directory by default log
 
log_level = notice
#Error level Available levels are: alert, error, warning, notice, debug Default: Notice
 
emergency_restart_threshold = 60
emergency_restart_interval = 60s
#Indicates in emergency_ restart_ If the number of PHP CGI processes with SIGSEGV or SIGBUS errors within the value set by interval exceeds emergency_restart_threshold, PHP FPM will restart gracefully. These two options generally remain the default values.
 
process_control_timeout = 0
#Set the timeout for the subprocess to accept the multiplexed signal of the main process Available units: s (seconds), m (minutes), H (hours), or D (days). Default unit: s (seconds) Default: 0
 
daemonize = yes
#Execute FPM in the background. The default value is yes. If it is for debugging, it can be changed to no. In FPM, different settings can be used to run multiple process pools. These settings can be set separately for each process pool.
 
listen = 127.0.0.1:9000
#fpm listening port, that is, the address processed by php in nginx, which is generally the default value. The available formats are: 'ip:port', 'port', 'path/to/unix/socket' Each process pool needs to be set
 
listen.backlog = -1
#The number of backlog, - 1 means unlimited, which is determined by the operating system. Just comment out this line. Backlog meaning reference:
 
http://www.3gyou.cc/?p=41
 
listen.allowed_clients = 127.0.0.1
#Allow access to the IP of FastCGI process. Set any as unlimited IP. If you want to set nginx of other hosts to access this FPM process, set the local accessible IP at listen. The default value is any. Each address is separated by a comma If not set or empty, any server is allowed to request a connection
 
listen.owner = www
listen.group = www
listen.mode = 0666
#unix socket setting options. If you use tcp to access, you can comment here.
 
user = www
group = www
#Account and group to start the process
 
pm = dynamic #For dedicated servers, pm can be set to static.
#How to control sub processes? The options are static and dynamic. If static is selected, PM max_ Children specifies a fixed number of child processes. If dynamic is selected, it is determined by the following parameters:
pm.max_children #, maximum number of child processes
pm.start_servers #, number of processes at startup
pm.min_spare_servers #, ensure the minimum number of idle processes. If the idle process is less than this value, a new child process will be created
pm.max_spare_servers #, ensure the maximum number of idle processes. If the number of idle processes is greater than this value, clean it up
 
pm.max_requests = 1000
#Set the number of service requests before each child process is reborn It is very useful for third-party modules that may have memory leaks If set to '0', the request is always accepted Equivalent to PHP_FCGI_MAX_REQUESTS environment variable Default: 0
 
pm.status_path = /status
#The URL of the FPM status page If it is not set, the status page cannot be accessed Default: none Munin monitoring will use
 
ping.path = /ping
#ping URL of FPM monitoring page If it is not set, the ping page cannot be accessed This page is used to externally detect whether the FPM is alive and can respond to requests Note that it must start with a slash (/).
 
ping.response = pong
#Used to define the corresponding return of ping request Return text in text/plain format of HTTP 200 Default: pong
 
request_terminate_timeout = 0
#Set the timeout for a single request to abort This option may affect PHP 'MAX 'in ini setting_ execution_ 'time 'is not useful for aborting scripts for some special reasons Setting to '0' means' Off ' Try changing this option when 502 errors occur frequently.
 
request_slowlog_timeout = 10s
#After a request for the timeout, the corresponding PHP call stack information will be completely written to the slow log Setting to '0' means' Off '
 
slowlog = log/$pool.log.slow
#Record the log of slow request and cooperate with the request_slowlog_timeout use
 
rlimit_files = 1024
#Sets the rlimit limit for file open descriptors Default value: the default openable handle of the system definition value is 1024, which can be viewed by ulimit -n and modified by ulimit -n 2048.
 
rlimit_core = 0
#Set the maximum limit of core rlimit Available values: 'unlimited', 0 or positive integer Default value: system defined value
 
chroot =
#Chroot directory at startup The defined directory needs to be an absolute path If not set, chroot is not used
 
chdir =
#Set the startup directory, which will be automatically Chdir to at startup The defined directory needs to be an absolute path Default value: current directory or / directory (when chroot)
 
catch_workers_output = yes
#Redirect stdout and stderr in the running process to the main error log file If not set, stdout and stderr will be redirected to / dev/null according to FastCGI rules Default: empty

The above contents would like to help you, more PHP PDF interview documents, PHP advanced architecture video data, PHP wonderful and good free access to WeChat search official account: PHP open source community, or visit:

2021 gold, silver and four major factories interview true questions collection, must see!

Four years essence PHP technical articles collation Collection - PHP framework article

Four years essence PHP technology collection - micro service architecture chapter

Four years essence PHP technology collection - distributed architecture chapter

Four years essence PHP technology collection - high concurrency scenario

Four years essence PHP technical articles collation Collection - database articles

 

Keywords: PHP Interview php-fpm fastcgi

Added by jini01 on Thu, 03 Mar 2022 22:46:47 +0200