Detailed Explanation of PHP FPM Management and Configuration

  • 2021-11-29 06:26:47
  • OfStack

What are PHP-FPM?

PHP-FPM is the process manager for FastCGI.

Characteristics of PHP-FPM

Advanced process management functions supporting smooth stop and start Generation of dynamic and static subprocesses Slow log, which records the abnormal slowness caused by script running You can listen on different ports and use different php. ini

Understanding of Several Concepts

1), CGI

The full name of CGI is "Common Gateway Interface" (Common Gateway Interface), which is the interface between HTTP server and programs on other machines, and its programs must run on the network server.

NOTE: CGI can be written in any one language, as long as that language has standard input, output, and environment variables.

2), FastCGI

FastCGI is a resident CGI, which can be executed directly. As long as it is activated, it will not take time to go to fork1 every time (this is the most criticized fork-and-execute mode of CGI). It also supports distributed computing, i.e. FastCGI programs can be executed on hosts other than Web servers and accept requests from other Web servers.

FastCGI is an open extension of CGI with a language-independent, scalable architecture, and its main behavior is to keep the CGI interpreter process in memory and thus achieve high performance. As we know, the repeated loading of CGI interpreter is the main reason for the poor performance of CGI. If CGI interpreter is stored in memory and scheduled by FastCGI process manager, it can provide good performance, scalability and so on.

Advantages:

1. FastCGI is language independent;

2. FastCGI runs independently of the core web server, providing a more secure environment than API. APIs links the code of the application to the core web server at 1, which means that a wrong API application may damage other applications or the core server. The malicious API application code can even steal the key of another application or core server;

3. At present, the supporting languages of FastCGI technology are: C/C + +, Java, Perl, Tcl, Python, SmallTalk, Ruby and so on. Related modules are also available on popular servers such as Apache, ISS and Lighttpd.

4. FastCGI does not depend on the internal architecture of any Web server, so FastCGI remains stable even if the server technology changes;

Disadvantages:

Because it is multi-process, it consumes more server memory than CGI multithreading. The PHP-CGI interpreter consumes 7 to 25 megabytes of memory per process. Multiplying this number by 50 or 100 is a large amount of memory.

The Nginx 0.8. 46 + PHP 5.2. 14 (FastCGI) server consumes 150M memory (15M*10=150M) for 10 Nginx processes opened, and 1280M memory (20M*64=1280M) for 64 php-cgi processes opened. If the server memory is small, you can start only 25 php-cgi processes, so that the total memory consumed by php-cgi is only 500M.

The above data is taken from Nginx 0.8. x + PHP 5.2. 13 (FastCGI) to build an Web server that is 10 times better than Apache (version 6).

Principle:

1. When the Web server starts, load the FastCGI process manager;

2. The FastCGI process manager initializes, starts a plurality of CGI interpreter processes (PHP-CGI) and waits for a connection from the Web server;

3. When the client request arrives at the Web server, the FastCGI process manager selects and connects to an CGI interpreter, and the Web server sends the CGI environment variables and standard input to the FastCGI child processes PHP-CGI.

4. The FastCGI child process returns the standard output and error message from the same 1 connection to the Web server after processing. When the FastCGI child process closes the connection, the request is processed. The FastCGI child process then waits for and processes the next 1 connection from the FastCGI process manager (running in the Web server). In CGI mode, PHP-CGI exits here.

In the above case, you can imagine how slow CGI is usually. Every Web requests PHP, you must reparse php. ini, reload all extensions, and re-initialize all data structures. With FastCGI, all this happens only once when the process starts. In addition, database persistent connections can work.

NOTE: The main advantage of FastCGI is that it separates dynamic language from HTTP Server, so Nginx and PHP/PHP-FPM are often deployed on different servers to share the pressure of front-end Nginx servers, so that Nginx dedicated 1 handles static requests and forwards dynamic requests, while PHP/PHP-FPM dedicated 1 parses PHP dynamic requests.

3), PHP-CGI

PHP-CGI is the FastCGI manager that comes with PHP.

Deficiencies of PHP-CGI:

1. After php-cgi changes the configuration of php.ini, it is necessary to restart php-cgi to make the new php-ini take effect, and it cannot be restarted smoothly.

2. Kill the php-cgi process directly, and php will not run (PHP-FPM and Spawn-FCGI do not have this problem, and the daemon will smoothly regenerate new child processes).

4), Spawn-FCGI

Spawn-FCGI is a general FastCGI management server, which is a part of lighttpd. Many people use Spawn-FCGI of Lighttpd to manage FastCGI mode, but there are many shortcomings. The appearance of PHP-FPM somewhat alleviates some problems, but PHP-FPM has a disadvantage that it needs to be recompiled, which may have no small risk for some already running environments (refer). PHP-FPM can be used directly in php 5.3. 3.

At present, Spawn-FCGI has become a single project, which is more stable and brings convenience to the configuration of many Web sites. Many sites have paired it with nginx to solve dynamic web pages. This block is not included in the latest lighttpd (http://www.lighttpd.NET/search? q=Spawn-FCGI), but it can be found in previous versions. In lighttpd-1. 4.15

http://www.lighttpd. net/download/lighttpd-1. 4.15. tar.gz. At present, the download address of Spawn-FCGI is http://redmine.lighttpd.Net/projects/spawn-fcgi, and the latest version is http://www.lighttpd.net/download/spawn-fcgi-1. 6.3. tar.gz.

NOTE: The latest Spawn-FCGI can be found by searching "Spawn-FCGI" on the website of lighttpd. net.

5) Compare Spawn-FCGI

PHP-FPM is very convenient to use, and the configuration is in the file of PHP-FPM. ini, while the startup and restart can be carried out from php/sbin/PHP-FPM. More conveniently, after modifying php. ini, you can directly use PHP-FPM reload to load, and you can complete the modification and loading of php. ini without killing the process

The results show that using PHP-FPM can improve the performance of php. The PHP-FPM-controlled process cpu recycles slowly and distributes memory evenly.

The Spawn-FCGI-controlled process CPU drops rapidly, but the memory allocation is uneven. Many processes seem to be unallocated, while the other one is very occupied. It may be caused by uneven distribution of process tasks. This also leads to a decline in the overall response speed. The reasonable allocation of PHP-FPM leads to the mention of overall response and the average of tasks.

Management and Configuration of PHP-FPM

PHP-FPM Management

The master main process understands the following signals:

INT, TERM terminated immediately QUIT Smooth Termination USR1 Reopen Log File USR2 Smooth overloads all worker processes and reloads configuration and binary modules

#  View  php-fpm  Users 
ps aux | grep php-fpm

#  View  php-fpm  Process  pid
ps -ef | grep php-fpm

#  Start  php-fpm
php-fpm -D

#  Shut down  php-fpm
kill -INT [pid]

#  Restart  php-fpm
kill -USR2 [pid]

ps is a snapshot showing the current process.

PHP-FPM Configuration

The configuration of all process pools is contained in the folder/etc/php-fpm. d, with the following line in the main configuration file/etc/php-fpm. conf:


include=/etc/php-fpm.d/*.conf

The main configuration file for php-fpm. conf contains the global configuration, and the child configurations are configurations for different process pools, such as www.

Global configuration


# s  Seconds, m  Points, h  Hours, d  Days 

#  Child process in  emergency_restart_interval  The number of times the parameter is set within the set time  SIGSEGV  Or  SIGBUS Exit the information number, then FPM Will restart 
emergency_restart_threshold 10
#  Smooth restart interval: 
emergency_restart_interval 1m
#  Time-out for sub-process to receive multiplexed signal from main process 
process_control_timeout 10s

Process pool configuration


#  Receive  FastCGI  Requested address 
listen :  'ip:port', '/path/to/unix/socket'

#  Setting up how the Process Manager manages child processes 
pm: static  Fixed quantity , ondemand  Processes are generated when there is a demand , dynamic  Dynamic setting 

static and dynamic Configuration

Memory is relatively small, concurrent amount is not very large applications, can consider the use of dynamic, which can control the php-fpm consumed total memory.

In the case of high concurrency or large traffic fluctuation, static can get faster response speed than dynamic under high concurrency.

Number of configurable processes = php-fpm configurable memory/(php-fpm child footprint * 1.2)


Related articles: