What is a PHP – FPM

  • Before we get to phP-FPM, let’s consider a question: a user makes a Web (niginx server) request in PHP code, so how do we get through it$_POST,$_GET,$_SERVERHow about getting some request information? What format should we refer to to assemble the data?

In fact, we know that each type of dynamic language, which is interpreted language, need through the corresponding parser can be server (here refers to the web server) recognition, but the interpreter and the server must follow some sort of agreement, the two sides can normal communication, then this agreement is the CGI, but the mechanism of CGI is every response web request, A new handler is created and initialized and killed at the end of the request. Each request requires three steps: create -> initialize -> end, which is not only a waste of resources, but also inefficient. So what? FastCGI, a modified version of CGI, starts a resident service process that does not need to manage the life cycle, thus avoiding the repeated creation and termination of the process. On the other hand, it does not need to repeatedly read environment variables. The resident server process starts the CGI interpreter process

  • Well, now that CGI is available, make an adaptation for an interpreted language such as PHP Phython. So PHP officially came up with php-CGI, the CGI version of PHP.
  • But with use, people found problems with PHP-CGI

1. After modifying php.ini, you must restart php-cgi for the modification to take effect. Simply kill php-CGI and PHP will not run, which is clearly unacceptable 3. It doesn’t manage the process itself, it just parses the request and returns the result

Now that FastCGI has arrived, can PHP FastCGI be far behind? Of course not, Then in 2004, a guy named Andrei Nigmatulin invented PHP-FPM. The full name of phP-FMP is called PHp-FastCGI Process Manager, which is basically the custom version of FASTCGI for PHP. Both php-CGI and php-fpm are intended to implement the CGI protocol, not a new protocol.) Now that you know the full name of php-fmp is php-FastCGI Process Manager, you can tell them no and spread the knowledge to them.

Php-fpm processes include master(resident server) and worker processes

The master process
  • The master is responsible for scheduling processes (e.g. forking a child process when the worker is running out of processes)
  • Responsible for listening port, generally 9000 this port, can be set in the configuration file, of course, there is another way, is through socket, can passThe process of netstat nap | grep master numberCheck port information (9000 port is actually TCP communication mode, and socket is said Unix socket, from efficiency, Unix socket is obviously the best, because it is the communication between processes, but Unix socket to ensure that is in a server, If the communication is between different machines, still use TCP communication)

  • Receive requests from the server
It’s the real working class, where the code is actually executed

  • How does PHP-FMP communicate with Nignx

Nginx conf file: socker conf file: socker conf

        location ~ [^/]\.php(/|$)
        {
            try_files $uri =404;
            fastcgi_pass  unix:/tmp/php-cgi.sock;
            fastcgi_index index.php;
            include fastcgi.conf;
        }

Copy the code

/ TMP /php-cgi.sock is the link between PHP and nginx. Include fastcgi.conf is the link between PHP and nginx

root@6d05153a8988:/usr/local/nginx/conf# cat fastcgi.conf fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param QUERY_STRING $query_string; fastcgi_param REQUEST_METHOD $request_method; fastcgi_param CONTENT_TYPE $content_type; fastcgi_param CONTENT_LENGTH $content_length; fastcgi_param SCRIPT_NAME $fastcgi_script_name; fastcgi_param REQUEST_URI $request_uri; fastcgi_param DOCUMENT_URI $document_uri; fastcgi_param DOCUMENT_ROOT $document_root; fastcgi_param SERVER_PROTOCOL $server_protocol; fastcgi_param REQUEST_SCHEME $scheme; fastcgi_param HTTPS $https if_not_empty; Fastcgi_param GATEWAY_INTERFACE CGI / 1.1; fastcgi_param SERVER_SOFTWARE nginx/$nginx_version; fastcgi_param REMOTE_ADDR $remote_addr; fastcgi_param REMOTE_PORT $remote_port; fastcgi_param SERVER_ADDR $server_addr; fastcgi_param SERVER_PORT $server_port; fastcgi_param SERVER_NAME $server_name; # PHP only, required if PHP was built with --enable-force-cgi-redirect fastcgi_param REDIRECT_STATUS 200; fastcgi_param PHP_ADMIN_VALUE "open_basedir=NULL";Copy the code

We see familiar ones like REMOTE_ADDR, REQUEST_URI, and now you should know that the information we get from $_SERVER is the one specified in this configuration file

Let’s take a look at the phP-fMP configuration file again (please note the comments, I won’t explain).

root@6d05153a8988:/usr/local/php/etc# cat php-fpm.conf [global] pid = /usr/local/php/var/run/php-fpm.pid error_log = /usr/local/php/var/log/php-fpm.log log_level = notice [www] listen = /tmp/php-cgi.sock listen.backlog = -1 Allowed_clients = 127.0.0.1 list.owner = WWW list.group = WWW list.mode = 0666 user = WWW group = WWW # If dm is set to static, only pm.max_children is valid. The system will start the php-fpm process to set the number. # if dm is set to dynamic, then the pm.max_children parameter is invalid and the last three parameters are in effect. The pm.start_servers process will be started at the beginning of the php-fpm run. # Then dynamically adjust the number of PHP-fPM processes between pm. min_spare_Servers and Pm. max_spare_Servers # based on system requirements. PM = dynamic # Number of php-FPM processes started in static mode pm.max_children = 20 # Number of php-FPM processes started in dynamic mode pm. start_Servers = 10 # Minimum number of php-FPM processes started in dynamic mode Pm.min_spare_servers = 10 # Maximum number of phP-FPM processes pm.max_servers = 20 # Maximum number of php-FPM child processes pm.max_requests = 1024 pm.process_idle_timeout = 10s request_terminate_timeout = 100 request_slowlog_timeout = 0 slowlog = var/log/slow.logCopy the code
  • Operations related to PHP-FPM

INT, TERM QUIT Smooth terminate USR1 reopen log files USR2 smooth reloads all worker processes and reloads configuration and binary modules

Activation: / usr/local/PHP/sbin/PHP – FPM number of processes: to view the ps aux | grep -c PHP – FPM view mater process: ps aux | grep ‘PHP – FPM: Master ‘| grep -v grep | awk’ {print $2} ‘or cat/usr/local/PHP/var/run/PHP – FPM. Pid

# forced closed pkill PHP - FPM kill INT ` cat/usr/local/PHP/var/run/PHP - FPM. Pid ` kill INT [pid] # smooth restart Is by creating a new process to make PHP. Ini kill -USR2 `cat /usr/local/php/var/run/php-fpm.pid` kill -USR2 [pid]Copy the code
summary

In addition to the high performance of Nginx, php-FPM is also one of the reasons why LNMP suffers from higher concurrency than LAMP.