This article will give you a detailed introduction to PHP-FMP. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.
$_POST, $_GET, $_SERVER
? What format should we refer to to assemble data? In fact, we know that each dynamic language, that is, interpreted language, needs to pass the corresponding parser to be recognized by the server (here refers to the web server), but the interpreter and server must follow certain Only through this protocol can the two parties communicate normally, then this protocol is the CGI protocol, but the mechanism of CGI is that every time it responds to a web request, a new processing process will be created and initialized, and this process will be killed when the request is completed. Then each request must execute these three steps: Create->Initialize->End. In fact, this process not only wastes resources, but is also very inefficient. What to do? FastCGI emerged in response to the times. As an improved version of CGI, FastCGI will start a resident service process. This process does not need to manage the life cycle, thus avoiding the repeated creation and termination of the process. On the other hand, there is no need for repeated reading. Get the environment variable. Whenever there is a web request, the FastCGI manager, that is, the resident service process, starts the CGI interpreter process
1. After modifying php.ini, you must restart PHP-CGI to take effect, and smooth restart cannot be achieved. 2. If you directly kill PHP-CGI, php will not be able to run. This is obviously unacceptable. 3. This thing will not manage the process by itself, it can only parse the request and return the result
So FastCGI has arrived, will PHP’s FastCGI be far behind? Of course not, by 2004 a man named Andrei Nigmatulin The loser invented PHP-FPM. The full name of PHP-FMP is PHP-FASTCGI Process Manager. To put it bluntly, it is a customized version of FastCGI for PHP (I would like to emphasize here that both PHP-CGI and PHP-FPM are designed to implement the CGI protocol, and It is not a new protocol). In fact, there is one thing I didn’t say just now. Many people on the Internet say that PHP-CGI is a program for PHP to manage FAST-CGI. Now you know that the full name of PHP-FMP is PHP-FASTCGI Process. After becoming Manager, you can confidently say no to them and spread this knowledge to them.
netstat -nap | grep master's process number
(Port 9000 is actually the communication method of tcp, and socket is the unix socket. In terms of efficiency, Unix socket is obviously the best, because it is communication between processes, but Unix socket must be on a server. If it is communication between different machines, tcp communication must still be used)Taking socker communication as an example, in the conf file of nginx, you can see the following information
location ~ [^/]\.php(/|$) { try_files $uri =404; fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_index index.php; include fastcgi.conf; }
I believe everyone can understand this information, /tmp/php-cgi.sock is the connection between php and nginx bridge, and we also saw include fastcgi.conf
, let’s take a look at
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";
We saw some familiar ones, such as REMOTE_ADDR, REQUEST_URI, now you should understand, we pass The information obtained by $_SERVER is the one specified in this configuration file
Let’s take a look at the php-fmp configuration file (please pay attention to the comments inside, I won’t explain it)
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 listen.allowed_clients = 127.0.0.1 listen.owner = www listen.group = www listen.mode = 0666 user = www group = www # 如何控制子进程,选项有static和dynamic #区别: #如果dm设置为 static,那么其实只有pm.max_children这个参数生效。系统会开#启设置数量的php-fpm进程。 #如果dm设置为 dynamic,那么pm.max_children参数失效,后面3个参数生效。 #系统会在php-fpm运行开始 的时候启动pm.start_servers个php-fpm进程, #然后根据系统的需求动态在pm.min_spare_servers和pm.max_spare_servers之#间调整php-fpm进程数。 pm = dynamic # 静态方式下开启的php-fpm进程数量 pm.max_children = 20 # 动态方式下的起始php-fpm进程数量 pm.start_servers = 10 # 动态方式下的最小php-fpm进程数 pm.min_spare_servers = 10 # 动态方式下的最大php-fpm进程数量 pm.max_spare_servers = 20 # php-fpm子进程能处理的最大请求数 pm.max_requests = 1024 pm.process_idle_timeout = 10s request_terminate_timeout = 100 request_slowlog_timeout = 0 slowlog = var/log/slow.log
INT, TERM QUIT smooth termination USR1 reopens the log file USR2 smoothly reloads all worker processes and reloads configuration and binary modules
启动: /usr/local/php/sbin/php-fpm
查看进程数: ps aux | grep -c php-fpm
查看mater进程号:ps aux|grep 'php-fpm: master'|grep -v grep|awk '{print $2}'
或者cat /usr/local/php/var/run/php-fpm.pid
# 强制关闭 pkill php-fpm kill -INT `cat /usr/local/php/var/run/php-fpm.pid` kill -INT [pid] # 平滑重启 其实就是通过创建新的进程使 php.ini 生效 kill -USR2 `cat /usr/local/php/var/run/php-fpm.pid` kill -USR2 [pid]
至此,php-fpm 算是说完了,其实通过上面的解说,大家也会明白一个问题,为什么lnmp 承受的并发比lamp高,除了nginx的高性能之外,php-fpm 是不是也是其中的一个原因呢?
推荐学习:《PHP视频教程》
The above is the detailed content of This article will give you an in-depth analysis of PHP-FMP. For more information, please follow other related articles on the PHP Chinese website!