How Web servers and dynamic languages interact--CGI&FASTCGI&FPM on the Web server interaction--cgi
How does a user's request interact with the Web server (apache,nginx,iis,light) and the backend's dynamic language (such as PHP) and return the results to the user?
This article discusses the personal point of view, may be wrong, welcome to shoot bricks, study together.
I. First clarify several concepts for follow-up instructions
CGI: (Common Gateway Interface) an intermediate layer in which HTTP servers interact with back-end programs such as PHP.
Working principle and processing mode (Fork-and-execute mode):
1. When Web server has request arrival
2.fork a CGI process or thread (configuration management, environment initialization)
3. Performing a background script
4. Return the results to the Web server.
The 5.WEB server returns the results to the user.
FastCGI: Resident (long-live) CGI form, after activation, will not take time to fork each time.
Working principle and treatment method:
Load the FASTCGI Process Manager (IIS ISAPI or Apache Module) at 1.Web server startup
The 2.FastCGI process Manager itself initializes, launches multiple CGI interpreter processes (visible multiple php-cgi processes), and waits for a connection from the Web server
3. When a client request arrives at Web server, the FASTCGI process manager selects and connects to a CGI interpreter, and the WEB server sends CGI environment variables and standard input to the FASTCGI child process php-cgi.
After the 4.FastCGI child process finishes processing, the standard output and error information is returned to the Web Server. When the fastcgi child process closes the connection, the request tells the processing to complete. The child process continues to respond to other requests that are assigned from the FASTCGI process manager.
PHP-FPM: PHP FastCGI process Manager for PHP only.
PHP5.3.3 later versions have been integrated with PHP-FPM.
PHP-FPM provides a better way to manage your PHP configuration by effectively controlling memory and processes, and by smoothing overloaded PHP configurations.
/configure PHP Source code, add-enable-fpm parameter can open PHP_FMP.
spawn-fcgi: An ordinary fastcgi process manager.
Two. CGI Implementations in PHP:
The CGI implementation of PHP is essentially a TCP or UDP protocol server implemented in socket programming. When started, the server that created the TCP/UDP protocol listens and accepts requests for processing.
The life cycle of CGI is: module initialization, SAPI initialization, request processing, module closing; sapi closing;
In the case of the TCP protocol, on the server side of TCP, the following actions are performed:
1. The server invokes the socket function to create a streaming socket for TCP;
2. The server calls the BIND function to bind the local addresses of the servers to the sockets created earlier;
3. The service calls the Listen function to listen for the newly created socket, waiting for the client to initiate the connection, which may need to be queued when the client has multiple connections to the socket;
4. The server calls the Accept function into a blocking state until a client process calls the Connect function to establish a connection;
5. When the connection is created with the client, the server calls the Read_stream function to read the client's request;
6. After processing the data, the server calls the Write function to send an answer to the client.
Three. How PHP works (take Apache server as an example, because Apache and PHP are good brothers)
1.Apache handler mode (PHP as module for Apache server)
An improved CGI method, the PHP interpretation module is compiled so extension, added to the Apache modules.
How to configure:
1. When compiling PHP, add the following parameters:
CD Php-source
./configure--prefix=/home/weiyanyan/local/php--with-apxs2=/home/weiyanyan/local/apache/bin/apxs--with-mysql
Description:-WITH-APXS2 is the APXS directory in Apache and will be generated under modules under the Apache root directory libphp5.so
2. Added in Apache config file http.conf
LoadModule Php5_module modules/libphp5.so
Then
Add the following MIME configuration
under the node
AddType application/x-httpd-php. php
2.CGI mode
The premise is that you cannot run in module mode. (commented out: LoadModule php5_module modules/libphp5.so)
Add action to httpd.conf:
Action application/x-httpd-php/cgi-bin/php-cgi
If you cannot find php-cgi in the/cgi directory, you can get a CP from the bin in PHP.
"You can write a PHP script to sleep (20), see the machine process without php-cgi process, when requested, there will be a corresponding process generated." After testing a php-cgi process can host multiple requests, specifically not to drill down, because this approach has basically no one to use. 】
3.FastCGI mode
Depending on the process manager, the FASTCGI mode can be divided into: Apache built-in Process Manager, PHP-FPM process Manager
Apache built-In Process Manager:
Installation of mod_fastcgi
#wget http://www.fastcgi.com/dist/mod_fastcgi-2.4.6.tar.gz
# TAR-ZXVF Mod_fastcgi-2.4.6.tar.gz
# CD mod_fastcgi-2.4.6
# CP Makefile.ap2 Makefile
# vim Makefile To change the path in Makefile to the installation path of your Apache
# Make install successful
After successful installation, the mod_fastcgi.so is automatically copied to the/usr/local/apache/modules directory
First, add the FastCGI module to the httpd.conf configuration file:
LoadModule Fastcgi_module modules/mod_fastcgi.so
This pattern annotation does not comment loadmodule php5_module modules/libphp5.so This line seems to be of little relevance, as long as the following modules are configured
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, this time with PS Aux|grep PHP will find a lot of php-cgi process is running. The description configuration takes effect.
FPM mode
First, add the FastCGI module to the httpd.conf configuration file:
LoadModule Fastcgi_module modules/mod_fastcgi.so
This pattern annotation does not comment loadmodule php5_module modules/libphp5.so This line seems to be of little relevance, 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
Where the PHP-FPM service is enabled on the native 9000 port
The installation of FPM is briefly described 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 in the root directory of PHP Sbin will have PHP-FPM run program, its configuration file under the PHP root directory/etc/php-fpm.conf
After modifying the configuration, start php-fpm on the corresponding port of Apache configuration.
[Just finished, not checked, 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/
http://www.bkjia.com/PHPjc/959695.html www.bkjia.com true http://www.bkjia.com/PHPjc/959695.html techarticle how Web servers and dynamic languages interact--cgifastcgifpm the Web server interaction--cgi How a user's request is passed through the Web server (apache,nginx,iis,light) with the backend ...