PHP – FPM is introduced

CGI protocol and FastCGI protocol

The code files for each dynamic language (PHP,Python, etc.) need to pass through the corresponding parser to be recognized by the server, and the CGI protocol is used to enable the interpreter and the server to communicate with each other. Parsing PHP files on the server requires the use of the PHP interpreter, coupled with the corresponding CGI protocol, so that the server can parse PHP files.

Because the mechanism of CGI is that each processing a request to fork a CGI process, the request ends and then kill the process, in the actual application is more waste of resources, so there is a modified version of CGI FastCGI, FastCGI after the request is finished, will not kill the process, Instead, you continue to process multiple requests, which is much more efficient.

What is PHP – FPM

Php-fpm stands for php-FastCGI Process Manager, which is an implementation of FastCGI and provides Process management functionality. Processes include master process and worker process. There is only one master process, which listens to the port and receives requests from the server, while worker processes generally have multiple processes (the specific number can be configured according to actual needs). Each process has a PHP interpreter embedded inside, which is where the code is actually executed.

Nginx communicates with PHP-FPM

When we visit a website (such as www.test.com), the process goes like this:

Routing to www.test.com/index.php www.test.com | | Nginx | | | | loading Nginx fast - cgi module | | fast - cgi to monitor 127.0.0.1:9000 address | | www.test.com/index.php request arrives at 127.0.0.1:9000 | | waiting to be processed...Copy the code

Nginx combined with PHP-FPM

On Linux, nginx communicates with PHP-FPM through TCP sockets and Unix sockets.

The advantage of TCP sockets is that they can cross servers and can only be used in this way when nginx and PHP-FPM are not on the same machine.

Unix sockets, also called IPC sockets, are used for inter-process communication on the same host. In this way, you need to fill in the socket file location of PHP-FPM in the nginx configuration file.

The data transmission process of the two modes is shown in the figure below:

The difference between the two:

Because Unix sockets do not need to go through the network protocol stack, there is no need to pack and unpack, calculate checksums, maintain serial numbers and replies, and so on. They just copy application layer data from one process to another. Therefore, the efficiency of TCP socket is higher than that of TCP socket, reducing unnecessary TCP overhead. However, Unix sockets are unstable with high concurrency. When the number of connections bursts, a large number of long time caches will be generated. Without the support of connection-oriented protocols, large packets may directly fail without returning exceptions. TCP, a connection-oriented protocol, can better ensure the correctness and integrity of communication.

Nginx with phP-FPM only needs to be set in the respective configuration files:

1) Configuration in Nginx

The following uses TCP socket communication as an example

server {
    listen       80; Listen on port 80 to receive HTTP requests
    server_name  www.test.com; # is the website address
    root /usr/local/etc/nginx/www/huxintong_admin; Prepare the path to the code project
    # route to the root directory www.test.com
    location / {
        index index.php; # Jump to www.test.com/index.php
        autoindex on;
    }   

    # reverse proxy to phP-fpm when requesting PHP files from websites
    location ~ \.php$ {
        include /usr/local/etc/nginx/fastcgi.conf; Load the nginx fastCGI module
        fastcgi_intercept_errors on;
        fastcgi_pass   127.0.0.1:9000; # php-fpm listen on the IP address and port in TCP
       /usr/run/ PHP -fpm.sock}}Copy the code
  1. PHP – FPM configuration
Listen = 127.0.0.1:9000# or something like this
listen = /var/run/php-fpm.sock

Copy the code

Note that when using Unix socket connection, because the socket file is essentially a file, there is a problem of permission control, so you need to pay attention to the nginx process permission and phP-fpm permission problem, otherwise there will be a message indicating no permission to access. (Set users in their own profiles)

Through the above configuration, the communication between PHP-FPM and Nginx can be completed.

Selection in the application

If nginx and PHP-FPM are running on the same server and the concurrency is not more than 1000, use Unix sockets to improve the communication efficiency between nginx and PHP-FPM. For high-concurrency services, use more reliable TCP sockets to maintain efficiency through o&M methods such as load balancing and kernel optimization.

If the concurrency is high but you still want to use Unix sockets, you can use the following methods to improve the stability of Unix sockets.

1) Place sock files in the /dev/shm directory. In this directory, sock files are stored in the memory for faster reading and writing.

2) Improve the backlog

Backlog defaults to bit 128,1024. This value is best converted to your normal QPS configuration as follows.

Nginx. Conf file

server {
        listen 80 default backlog=1024;
       }
Copy the code

PHP – FPM. Conf file

listen.backlog = 1024
Copy the code

3) Add sock files and phP-FPM instances

Create a sock file in /dev/shm and load the request to two sock files using upstream in nginx and map the two sock files to two phP-FPM instances.