Understanding LNMP Architecture

Original blogger: https://blog.csdn.net/u013592371/article/details/73729892

Nginx is a high-performance web server, which cannot handle PHP itself. When receiving a request, if it is judged that it is a PHP request, it will hand over the request to the PHP interpreter for processing, and then return the result to the Client. Nginx generally forwards the request to the fast-cgi management process for processing, and the fast-cgi management process selects the cgi sub-process to process the request, and then returns the result to Nginx.

Preliminary understanding of Fast-CGI and Nginx+Fast-CGI
1. The difference between Apache+PHP and Nginx+PHP
Apache generally starts PHP as its own module; while Ngnix forwards http request variables to the PHP process, that is, PHP Independent process, communicate with Ngnix, this way is called Fast-CGI operation mode. So PHP compiled by Apache cannot be used with Nginx.
The basic structure of Nginx+PHP is as follows:
write picture description here

2. What is Fast-CGI
  Fast-CGI is a scalable, high-speed communication interface between HTTP server and dynamic scripting language. Most popular HTTP servers support Fast-CGI, including Apache, Nginx, and lighttpd. At the same time, Fast-CGI is also supported by many scripting languages, including PHP.
  Fast-CGI is developed and improved from CGI. The main disadvantage of the traditional CGI interface method is poor performance, because every time the HTTP server encounters a dynamic program, the script parser needs to be restarted to perform the parsing, and then return the result to the HTTP server. This is almost unusable when dealing with high concurrent access. In addition, the security of the traditional CGI interface method is also very poor, and it is rarely used now.
  The FastCGI interface adopts the C/S structure, which can separate the HTTP server and the script parsing server, and start one or more script parsing daemons on the script parsing server at the same time. Every time the HTTP server encounters a dynamic program, it can be directly delivered to the Fast-CGI process for execution, and then the result is returned to the browser. In this way, the HTTP server can exclusively handle static requests or return the results of the dynamic script server to the client, which greatly improves the performance of the entire application system.

There will be an explanation of the relationship between php-fpm and fast-cgi in the future.

3. Nginx+Fast-CGI operating principle
  Nginx does not support direct invocation or parsing of external programs, all external programs (including PHP) must be invoked through the Fast-CGI interface. The Fast-CGI interface is a socket under Linux (this socket can be a file socket or an ip socket).
  wrapper: In order to call a CGI program, a Fast-CGI wrapper (wrapper can be understood as a program used to start another program) is also required. This wrapper is bound to a fixed socket, such as a port or a file socket. When Nginx sends a CGI request to this socket, the wrapper receives the request through the Fast-CGI interface, and then forks (derived) a new thread, which calls the interpreter or external program to process the script and reads the returned data; Then, the wrapper passes the returned data to Nginx through the Fast-CGI interface along the fixed socket; finally, Nginx sends the returned data (html page or image) to the client. This is the entire operation of Nginx+Fast-CGI.
write picture description here
So, we first need a wrapper, the work that this wrapper needs to do:

1> Communicate with Nginx through socket by calling the function of fast-cgi (library) (reading and writing socket is a function implemented inside fast-cgi, which is non-transparent to wrapper)
2> schedule thread, fork and kill
3> and application ( php) to communicate

4. Brief description of php-fpm
  PHP-FPM is a PHP FastCGI manager, which is only used for PHP. It is actually a patch of PHP source code, which aims to integrate Fast-CGI process management into PHP packages. It must be patched into your PHP source code before it can be used after compiling and installing PHP. The new version of PHP has integrated php-fpm. You can enable PHP-FPM with the --enable-fpm parameter in ./configure.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325741487&siteId=291194637