Home >Backend Development >PHP Tutorial >How web servers and dynamic languages ​​interact - a brief discussion of CGI&FastCGI&FPM, web server interaction - cgi_PHP tutorial

How web servers and dynamic languages ​​interact - a brief discussion of CGI&FastCGI&FPM, web server interaction - cgi_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:06:21848browse

How web servers and dynamic languages ​​interact--A brief discussion of CGI&FastCGI&FPM, web server interaction--cgi

How a user's Request passes through the web server (Apache, Nginx, IIS, Light) interacts with the back-end dynamic language (such as PHP, etc.) and returns the results to the user?

This article briefly talks about my personal opinions and may be wrong. Welcome to comment and learn together.

1. First clarify a few concepts for subsequent explanation

CGI: (Common Gateway Interface) Http server and backend The middle layer through which programs (such as PHP) interact.

Working principle and processing method (fork-and-execute mode):

1. When a Request from the Web Server arrives

2. Fork a CGI process or Thread (configuration management, environment initialization)

3. Execute background script

4. Return the result to the web server.

5. The Web server returns the results to the user.

How web servers and dynamic languages ​​interact - a brief discussion of CGI&FastCGI&FPM, web server interaction - cgi_PHP tutorial

FastCGI: Resident (long-live) CGI form. After activation, it will not take time to fork every time.

Working principle and processing method:

1. Load the FastCGI process manager (IIS ISAPI or Apache Module) when the Web Server starts up

2.FastCGI process The manager initializes itself, starts multiple CGI interpreter processes (multiple php-cgi processes are visible), and waits for connections from the Web Server

3. When a client request reaches the Web Server, FastCGI process management The Web Server selects and connects to a CGI interpreter; the Web Server sends CGI environment variables and standard input to the FastCGI subprocess php-cgi.

4. After the FastCGI sub-process completes processing, it returns standard output and error information to the Web Server. When the FastCGI child process closes the connection, the request is notified that processing is complete. The child process continues to respond to other requests allocated from the FastCGI process manager.

How web servers and dynamic languages ​​interact - a brief discussion of CGI&FastCGI&FPM, web server interaction - cgi_PHP tutorial

PHP-FPM: PHP FastCGI Process Manager for PHP only.

PHP5.3.3 and later versions have integrated PHP-FPM.

php-fpm provides a better PHP configuration management method, which can effectively control memory and processes, and smoothly reload PHP configuration.

When ./configure php source code, add the -enable-fpm parameter to enable PHP_FMP.

Spawn-FCGI: a common FastCGI process manager.

2. CGI implementation in PHP:

PHP’s CGI implementation essentially uses Socket programming to implement a TCP or UDP Protocol server. When started, create a server listening for the TCP/UDP protocol and accept related requests for processing.

The life cycle of CGI is: module initialization; SAPI initialization; request processing; module shutdown; SAPI shutdown;

Taking the TCP protocol as an example, on the TCP server, the following operations will be performed :

1. The server calls the Socket function to create a streaming socket for TCP;

2. The server calls the bind function to compare the local address of the server with the previously created socket. Binding;

3. The service calls the listen function to use the newly created socket as a listener, waiting for the client to initiate a connection. When the client has multiple connections connected to this socket, it may need to be queued for processing ;

4. The server calls the accept function and enters the blocking state until a client process calls the connect function to establish a connection;

5. After the connection is established with the client, the server calls the read_stream function Read the client's request;

6. After processing the data, the server calls the write function to send a response to the client.

How web servers and dynamic languages ​​interact - a brief discussion of CGI&FastCGI&FPM, web server interaction - cgi_PHP tutorial

3. The current way PHP works (take the Apache server as an example, because Apache and Php are good brothers)

1.Apache Handler method (php as the Module of the Apache server)

An improved CGI method that compiles the PHP interpretation module into a so extension and adds it to Apache modules.

Configuration method:

 <p>1.编译PHP时,加上如下参数:</p> <p>cd php-source</p> <p>./configure --prefix=/home/weiyanyan/local/php --with-apxs2=/home/weiyanyan/local/apache/bin/apxs --with-mysql</p> <p>说明:—with-apxs2为apache中apxs相应目录,将在apache根目录下的modules下生成libphp5.so</p> <p>2.在apache的配置文件http.conf中增加  <p>LoadModule php5_module modules/libphp5.so  <p>然后在<IfModule mime_module>节点下增加如下mime配置  <p>AddType application/x-httpd-php .php<br><img  alt="How web servers and dynamic languages ​​interact - a brief discussion of CGI&FastCGI&FPM, web server interaction - cgi_PHP tutorial" > </p>

2. CGI mode

The premise is that it cannot be run in module mode. (Comment out: LoadModule php5_module modules/libphp5.so)

Add action in httpd.conf:

Action application/x-httpd-php /cgi-bin/php-cgi

If found in the /cgi directory If you don’t have php-cgi, you can cp one from the php bin.

How web servers and dynamic languages ​​interact - a brief discussion of CGI&FastCGI&FPM, web server interaction - cgi_PHP tutorial

[You can write a PHP script and let it sleep(20); before running, check that there is no php-cgi process in the machine process. When requested, the corresponding process will be generated. It has been tested that one php-cgi process can carry multiple requests, but the details have not been studied in detail because this method is basically no longer used. ]

3. FastCGI mode

FastCGI mode can be divided into: Apache built-in process manager, php-fpm process manager

Apache built-in process manager:

 <p>mod_fastcgi的安装  <p>#wget http://www.fastcgi.com/dist/mod_fastcgi-2.4.6.tar.gz  <p># tar -zxvf mod_fastcgi-2.4.6.tar.gz  <p># cd mod_fastcgi-2.4.6  <p># cp Makefile.AP2 Makefile  <p># vim Makefile 将Makefile中的路径改成你的apache的安装路径  <p># make install 安装成功  <p>安装成功后,会自动把mod_fastcgi.so复制到/usr/local/apache/modules目录  </p>

First, add the fastcgi module to the httpd.conf configuration file:

LoadModule fastcgi_module modules/mod_fastcgi.so

This mode does not comment the LoadModule php5_module modules/libphp5.so line. It seems to have nothing to do with it, as long as it is configured The following modules


FastCgiServer /home/weiyanyan/local/apache/cgi-bin/php-cgi -processes 20
AddType application/x-httpd-php .php

AddHandler php-fastcgi .php

Action php-fastcgi /cgi-bin/php-cgi

will automatically go to fastcgi mode.

Then restart apache. At this time, use ps aux|grep php and you will find that there are many php-cgi processes running. Indicates that the configuration takes effect.

FPM mode

First, add the fastcgi module to the httpd.conf configuration file:

LoadModule fastcgi_module modules/mod_fastcgi.so

This It seems that this line of LoadModule php5_module modules/libphp5.so does not matter if the mode comment is not commented, as long as the following modules are configured


FastCgiExternalServer /home/weiyanyan/local/apache/ cgi-bin/php-cgi -host 127.0.0.1:9000

AddType application/x-httpd-php .php

AddHandler php-fastcgi .php

Action php -fastcgi /cgi-bin/php-cgi

The PHP-Fpm service is enabled on the local port 9000

A brief introduction to the installation of FPM As follows:

cd php-source

./configure --prefix=/home/weiyanyan/local/php --with-apxs2=/home/weiyanyan/local/apache /bin/apxs --with-mysql --enable-fpm

At this time, there will be a php-fpm running program under the sbin root directory of Php, and its configuration file is in /etc/php under the php root directory. -fpm.conf

After modifying the configuration, start php-fpm on the port corresponding to the apache configuration.

[Just finished writing, not checking, going home for the New Year...]

Reference:

http://www.phppan.com/2011/05/php-cgi/

http://www.cnblogs.com/fangbo/archive/2011/12/02/2272400.html

http://blog.zyan.cc/nginx_php_v6/

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/959695.htmlTechArticleHow web servers and dynamic languages ​​interact - A brief talk on CGIFastCGIFPM, web server interaction - cgi A user's Request is How to go through the web server (Apache, Nginx, IIS, Light) and the backend...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn