Briefly describe what CGI is and briefly describe CGI

Source: Internet
Author: User

Briefly describe what CGI is and briefly describe CGI

Directory:
1. What is CGI?
2. Various Glossary
3. Interaction Mode Between web server and CGI
3.1 CGI Mode
3.2 Module Mode
3.3 php-fpm Mode

1. What is CGI?

CGI is the abbreviation of common gateway interface, which is translated as common gateway interface. However, unfortunately, we cannot understand it.

We know that the content processed by the web server is static. To process dynamic content, we need to rely on web applications, such as php, jsp, python, and perl. But how does the web server send dynamic requests to these applications? It depends on the cgi protocol. That's right. It's a protocol, that is, the specification used by the web server to communicate with web applications. In other words, by Using cgi protocol and combined with the established web application, the web server can also "process" dynamic requests. You must know why double quotation marks are added to the two words.

The simple version of cgi works as follows:

For example, if you search for an http keyword in the Google search bar, the corresponding URL is:

https://www.google.com/search?q=http&oq=http&aqs=chrome..69i57j69i60l4j0.1136j0j8&sourceid=chrome&ie=UTF-8

When Google's web server receives the request, it first analyzes the url, knows that the search program is to be executed, and also knows a series of parameters to be passed to the search and their corresponding values. Web server will pass these program parameters and other environment variables to the started cgi program through TCP or socket based on cgi protocol (possibly cgi process, or the loaded cgi Module ). When the cgi process receives a request from the web server, it calls the search program and executes the request, and also passes the parameter to the search program. After the search execution is complete, the cgi process/thread returns the processing result to the web server, and the web server returns the result to the browser.

There are multiple ways to execute cgi programs, but for http request methods, only get and post methods allow the execution of cgi scripts (that is, the above search program ).

2. Interpretation of various terms

To be honest, for a person who has never been familiar with programming languages, there will certainly be a bunch of questions when he is new to cgi concepts. What the hell is this, isn't it a php-like app that processes dynamic content? It's a little cool with cgi. What is fastcgi? I think that non-class obsessive-compulsive patients (including me) will surely survive these concepts.

Taking php as an example, I will briefly explain the concepts related to a dynamic request.

Taking php-fpm as an example, the process of web server forwarding dynamic requests to the end is roughly as follows:

Each php-cgi process has the following functions: (some functions are classified incorrectly. Ignore them. It is enough to know the general functions)

Note: Although php-fpm is fully called PHP FastCGI Process Manager, strictly speaking, php-fpm is not the fastcgi Process Manager, but the php fastcgi, that is, the php-cgi Process Manager. Fastcgi is only a protocol, not a process. Just like the http protocol, apache implements httpd and nginx implements nginx.

Again, cgi and fastcgi are protocols. Various programming languages that support interaction with the WEB implement cgi/fastcgi Protocols respectively (of course, any language can write cgi scripts ), the php-cgi and php-fpm on php are the implementation of the fastcgi protocol by php.

3. Interaction Mode Between web server and CGI

For cgi processes/threads, web server initiates dynamic request processing, transmits some parameters and environment variables, and finally receives cgi return results. In other words, the web server uses cgi/fastcgi protocol to forward dynamic requests to applications that execute cgi scripts. The following httpd. conf forwarding configuration should be easy to understand (interaction between httpd and php-fpm ):

ProxyRequests offProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/usr/local/apache/htdocs/$1

Take the most typical apache httpd and php as examples. For httpd, there are three interactive modes for web server and php-cgi.

  • Cgi Mode: When httpd receives a dynamic request, it fork a cgi process. After the cgi process returns the result to the httpd process, it destroys itself.
  • Dynamic module Mode: Compile the php-cgi Module (for example, php5_module) into httpd. When httpd is started, the module is loaded, and the corresponding module is activated during loading, and php-cgi is started. (Note: to correct a small error, many people think that dynamic compiling modules can be loaded and called at any time when needed, and they will stop when not needed. In fact, this is not the case. Like a statically compiled module, a Dynamically Loaded module is added to an activation linked list when it is loaded. Whether or not it is used, it is already running inside apache httpd. Refer to the LoadModule instruction manual)
  • Php-fpm Mode: Use php-fpm to manage php-cgi. At this time, httpd no longer controls the startup of the php-cgi process. Php-fpm can be run independently on a non-web server to achieve static-dynamic separation.

In fact, the fastcgi mode can be implemented by using the module mod_fastcgi. Like cgi, the inherent defect of the management model determines that this is not a good method.

3.1 CGI Mode

When the CGI mode is used, when a dynamic request arrives, httpd temporarily starts a cgi interpreter and forwards the content to be run through the cgi protocol. After running the cgi script, return the result to httpd and the cgi interpreter process destroys itself. When multiple dynamic requests arrive, a cgi interpreter is started successively. Therefore, this method is extremely inefficient.

After commenting out the LoadModule line of php5_module, use the action Command to specify the type of cgi to run. Note that the action command is provided by mod_action, so this module must be loaded.

For example, requests that specify the MIME type as image/gif run with images. cgi. Obviously, you must write the images. cgi script first.

Action image/gif /cgi-bin/images.cgi

You can also add handler to compose the file type, and then use a cgi script to run any type in the handler.

AddHandler my-file-type .xyzAction my-file-type "/cgi-bin/program.cgi"

For php, you can use the php-cgi program provided in the bin directory when installing php as the cgi program.

[Root @ xuexi php] # ls/usr/local/php/bin/pear peardev pecl phar. phar php-cgi php-config phpize # copy to the default cgi-bin directory of apache, convenient Management of [root @ xuexi php] # cp/usr/local/php/bin/php-cgi/usr/local/apache/cgi-bin/# in httpd. add the following Action application/x-httpd-php/usr/local/php/bin/cgi-bin/php-cgi in the conf file.

3.2 Module Mode

When compiling php, compile the php5_module module into apache. For example, when compiling php. add "-- with-apxs2 =/usr/local/apache/bin/apxs" to the/configure configuration ".

In this interactive mode, httpd loads and activates the php_module at startup. That is to say, php-cgi is resident within the httpd process. When a dynamic request arrives, httpd directly forwards the dynamic request to its internal php-cgi instead of generating a cgi interpreter.

This interactive mode is easy to configure. You only need to use LoadModule to load the php_module and then add the corresponding MIME processor.

LoadModule php5_module modules/libphp5.so # Add the corresponding type to the mime module <IfModule mime_module> AddType application/x-httpd-php. phpAddType applicaiton/x-httpd-php-source. phps </IfModule>

3.3 php-fpm

As mentioned above, php-fpm is the Process Manager of php-cgi. This interaction method actually allows php-cgi to exist independently of httpd. Currently, php-fpm is basically used to manage php-cgi processes. That is to say, in this mode, php-cgi and httpd have been separated, and their separation means that the request's dynamic and static separation becomes possible: httpd and php-fpm run on different servers respectively. After the static and dynamic separation, the pressure is also distributed to the respective servers.

To run php-fpm in this way, you need to add the "-- enable-fpm" option to the./configure configuration options for compilation. Of course, you have to start the php-fpm service. For example:

service php-fpm start

In this way, the php-cgi process opens the port (9000 by default) and waits for httpd to forward dynamic requests. To enable httpd to forward requests to php-cgi, disable the forward proxy in httpd. conf and set the fastcgi protocol proxy parameters. For example, forward to php-fpm on host 192.168.100.54.

# Load the Agent module LoadModule proxy_module modules/mod_proxy.soLoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so # Add the MIME type AddType application/x-httpd-php. phpAddType application/x-httpd-php-source. phps # configure the forwarding proxy ProxyRequests offProxyPassMatch ^ /(. *\. php) $ fcgi: // 192.168.100.54: 9000/usr/local/apache/htdocs/$1

 

Back to Linux series article outline: http://www.cnblogs.com/f-ck-need-u/p/7048359.html
Back to website architecture series article outline: http://www.cnblogs.com/f-ck-need-u/p/7576137.html
Back to database series article outline: http://www.cnblogs.com/f-ck-need-u/p/7586194.html
Reprinted please indicate the source: Success!

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.