CGI, server built-in module, Fastcgi, PHP-FPM Differences and Details

The earliest Web server simply responded to the HTTP static file request sent by the browser, and stored the static file on the server (for example: jpg, htm, html) return to the browser. The picture shows the processing flow

The emergence of CGI

< p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; color:r gb(69,69,69); font-family:"PingFang SC","Microsoft YaHei",SimHei,Arial,SimSun; font-size:16px; text-align:justify"> things are always constant With the development, the website is becoming more and more complex, so dynamic technology emerges. But the server can’t run files like php and asp directly. You can’t do it yourself, so you can outsource it to others, but you have to make an agreement with the third, what I give you, and what you give me, is that I send you the request parameters , And then I receive your processing results to the client. Then this convention is CGI (common gateway interface)This protocol can be implemented with vb, c, perl, php, python.

In 2000 or earlier, CGI was more popular. At that time, Perl was the mainstream language for writing CGI, so that ordinary CGI programs were Perl programs.

CGI (Common Gateway Interface), the common gateway interface, it is the interface standard for transferring information between the Web server and external applications (CGI programs). Through the CGI interface, the Web server can obtain the information submitted by the client, and transfer it to the CGI program on the server for processing, and finally return the result to the client. In other words, CGI is actually an interface standard. What we usually call CGI refers to a CGI program, that is, a program that implements the CGI interface standard. As long as a certain language has standard input, output and environment variables, such as perl, PHP, C, etc., it can be used to write CGI programs. CGI is just an interface protocol, not a language at all.

1. How the CGI program works: span>

Web servers generally only process static file requests (such as jpg, htm) , Html), if it encounters a dynamic script request (such as php), the main process of the web server forks a new process to start the CGI program, which means that the dynamic script request is handed over to the CGI program for processing. Starting a CGI program requires a process, such as reading configuration files, loading extensions, and so on. After the CGI program is started, it will parse the dynamic script, and then return the result to the Web server. Finally, the Web server returns the result to the client, and the fork process will also be closed. In this way, every time a user requests a dynamic script, the Web server has to fork a new process to start the CGI program. The program handles the dynamic script, and the process is closed after processing. The efficiency of this working method is very low. The operation diagram is as follows:


CGI program and web server transfer data:

< /p>

The CGI program performs input and output through standard input (STDIN) and standard output (STDOUT). In addition, CGI programs also get input through environment variables. The operating system provides many environment variables, which define the execution environment of the program, and applications can access them. The Web server and CGI interface set some additional environment variables to pass some important parameters to the CGI program. The CGI GET method also passes the data in the Form to the CGI program through the environment variable QUERY-STRING. Here are some commonly used CGI environment variables:

< td style="font-family:Verdana,Helvetica,Arial; word-break:break-all; line-height:18px; border:1px solid silver; border-collapse:collapse; padding:3px"> SERVER_NAME
Variable name Description
CONTENT_TYPE The value of this environment variable indicates the value of the information passed MIME type. At present, the environment variable CONTENT_TYPE is generally: application/x-www-form-urlencoded, which means that the data comes from an HTML form.
CONTENT_LENGTH If the server and CGI program information transmission method is POST, this environment variable is the number of bytes of valid data that can be read even from the standard input STDIN. This environment variable must be used when reading the input data.
HTTP_COOKIE COOKIE content in the client.
HTTP_USER_AGENT Provide client browser information including version number or other proprietary data.
PATH_INFO The value of this environment variable represents other path information immediately after the CGI program name. It often appears as a parameter of CGI programs.
QUERY_STRING If the server and CGI program information transfer method is GET, the value of this environment variable is the information passed. This information is followed by the CGI program name, separated by a question mark’?’ between the two.
REMOTE_ADDR The value of this environment variable is the IP address of the client that sent the request, such as 192.168.1.67 above. This value always exists. Moreover, it is the unique identifier that the Web client needs to provide to the Web server, and it can be used in the CGI program to distinguish different Web clients.
REMOTE_HOST The value of this environment variable contains the host name of the client that sent the CGI request. If the query you want is not supported, there is no need to define this environment variable.
REQUEST_METHOD Provide a method for the script to be called. For scripts using HTTP/1.0 protocol, only GET and POST are meaningful.
SCRIPT_FILENAME the full path of the CGI script
SCRIPT_NAME the name of the CGI script
This is the host name, alias or IP address of your WEB server.
SERVER_SOFTWARE The value of this environment variable contains the name and version number of the HTTP server calling the CGI program. For example, the above value is Apache/2.2.14(Unix)

2.Web server built-in modules:

Later, a more efficient way appeared: Web server built-in modules. For example, Apache’s mod_php module. Make the php interpreter into a module, and then load it into the apache server.

In this way, when the apache server starts, it will also start the php module . When the client requests the php file, the apache server does not need to fork a new process to start the php interpreter, but directly hand the php file to the running php module for processing. Obviously, in this way, the efficiency will be higher.

Because when the apache server starts, the php configuration file will be read , Load the php module, during the running of apache, the php configuration file will not be read again. Therefore, every time we modify the php configuration file, we must restart apache so that the new php configuration file will take effect. The running diagram is as follows:


3.FastCGI

FastCGI Just like a long-live CGI program, it can run all the time. The FastCGI program can also be deployed on different hosts from the Web server, and it can also accept requests from other Web servers.

FastCGI is also language independent. Its main behavior is to keep the CGI interpreter process in memory and thus obtain efficient performance. As we all know, repeated loading of the CGI interpreter is the main reason for the poor performance of CGI.

FastCGI is a process management tool that can manage CGI in memory process.

FastCGI process manager needs to be started separately. After FastCGI is started, a FastCGI main process and multiple child processes will be generated (the child process is actually the CGI interpreter process).

When the client requests the dynamic script on the Web server, the Web server will The dynamic script is handed over to the FastCGI main process through the TCP protocol. The FastCGI main process arranges an idle child process to parse the dynamic script according to the situation. After the processing is completed, the result is returned to the Web server, and the Web server returns the result to the client. After the client request is processed, the FastCGI child process will not be closed, but will continue to wait for the main process to schedule work tasks. From this, we can see that FastCGI’s work efficiency is very high. The running diagram is as follows:



4.php-fpm

fpm is FastCGI Process The abbreviation of Manager, then, fpm is the abbreviation of FastCGI process manager. php-fpm is the FastCGI process manager in php. For versions before php5.3, php-fpm is a third-party patch package designed to integrate the FastCGI process Management is integrated into the PHP package. In versions after php5.3, php-fpm is no longer a third-party package, it has been integrated into php In the source code. php-fpm provides a better PHP process management method, which can effectively control memory and processes, and can smoothly reload PHP configuration. It has more advantages than spawn-fcgi, so php-fpm was officially acquired by PHP.

Reference blog:

< p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; color:rgb(69,69,69); font-family:"PingFang SC","Microsoft YaHei",SimHei,Arial,SimSun; font-size:16px"> http://blog.csdn.net/lamp_yang_3533/article/details/53002731

http://www.cnblogs.com/iiiiher/p/5911419.html

< span style="margin:0px; padding:0px; font-size:13px">http://www.cnblogs.com/wanghetao/p/3934350.html

Leave a Comment

Your email address will not be published.