Interaction between Web servers and dynamic languages -- CGI & amp; FastCGI & amp; FPM; web server interaction -- cgi_PHP tutorial

Source: Internet
Author: User
Interaction between Web servers and dynamic languages-CGI & amp; FastCGI & amp; FPM; web server interaction-cgi. Interaction between Web servers and dynamic languages -- CGIFastCGIFPM; interaction between web servers -- how a user's Request passes through a Web server (Apache, Nginx, IIS, Light) how to interact with backend Web servers and dynamic languages -- CGI & FastCGI & FPM; web server interaction -- cgi

How does a user's Request interact with dynamic backend languages (such as PHP) on the Web server (Apache, Nginx, IIS, Light) and return the results to the user?

This article introduces my point of view, which may be incorrect. You are welcome to join us in learning.

I. first, I will clarify several concepts for further explanation.

CGI :( Common Gateway Interface) The middle layer for interaction between the Http server and backend programs (such as PHP.

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

1. when a Request arrives on the Web Server

2. fork a CGI process or thread (configuration management, environment initialization)

3. execute the background script

4. return the result to the Web server.

5. the Web server returns the result to the user.

FastCGI: long-live CGI format. after activation, it does not take time to fork every time.

Working Principle and handling method:

1. load FastCGI Process Manager (iis isapi or Apache Module) when the Web Server starts)

2. FastCGI Process Manager initializes itself, starts multiple CGI interpreter processes (multiple php-cgi processes are visible), and waits for a connection from the Web Server

3. when a client request arrives at the Web Server, the FastCGI process manager selects and connects to a CGI interpreter. the Web Server sends CGI environment variables and standard input to the FastCGI sub-process php-cgi.

4. after The FastCGI sub-process completes processing, the standard output and error messages are returned to the Web Server. When the FastCGI sub-process closes the connection, the request is notified that the processing is complete. The child process continues to respond to other requests from The FastCGI Process Manager.

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

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

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

./When configure php source code is added, you can enable PHP_FMP by adding the-enable-fpm parameter.

Spawn-FCGI: a common FastCGI Process Manager.

II. CGI implementation in PHP:

In essence, the CGI implementation of PHP implements a TCP or UDP server through Socket programming. When starting, create a TCP/UDP server listener and accept the relevant requests for processing.

CGI lifecycle: module initialization, SAPI initialization, request processing, module shutdown, and SAPI shutdown;

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

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

2. the server calls the bind function to bind the local address of the server to the socket created earlier;

3. the service calls the listen function to use the newly created socket as the listener. wait for the client to initiate a connection. when the client has multiple connections connected to the socket, it may need to queue for processing;

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

5. after creating a connection with the client, the server calls the read_stream function to read the client's request;

6. after the data is processed, the server calls the write function to send a response to the client.

III. Current PHP Working methods (take Apache server as an example, because Apache and Php are good brothers)

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

An improved CGI method is to compile the PHP interpretation module into the so extension and add it to the modules of Apache.

Configuration method:

 

1. add the following parameters when compiling PHP:

Cd php-source

./Configure -- prefix =/home/weiyanyan/local/php -- with-apxs2 =/home/weiyanyan/local/apache/bin/apxs -- with-mysql

(-With-apxs2 for apache apxs Directory, will generate libphp5.so under the modules apache root directory)

2. add

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 it cannot 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 you cannot find php-cgi in the/cgi directory, you can run cp in the php bin.

[You can write a PHP script to make it sleep (20). before running the script, you can see that there is no php-cgi process in the machine process. during the request, a corresponding process will be generated. After testing, a php-cgi process can carry multiple requests, which has not been further explored, because this method has basically been used by no one .]

3. FastCGI mode

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

Apache built-in process manager:

 

Mod_fastcgi installation

# 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: change the path in Makefile to your apache installation path.

# Make install

After the installation is successful, mod_fastcgi.so will be 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 mode annotation does not comment out LoadModule php5_module modules/libphp5.so this line seems to have nothing to do with, as long as the following module is 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

It will automatically go to fastcgi mode.

Restart apache and use ps aux | grep php to check that many php-cgi processes are 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 mode annotation does not comment out LoadModule php5_module modules/libphp5.so this line seems to have nothing to do with, as long as the following module is 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 port 9000 of the local machine.

A brief introduction to FPM installation is 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 Php-fpm running program under the php root directory sbin, 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 writing, not checked, go home for the Chinese New Year...]

Refer:

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/

How does one Request pass through the Web server (Apache, Nginx, IIS, Light) and backend...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.