Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the concept of cgi in php

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Most people do not understand the knowledge points of this article "what is the concept of cgi in php", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "what is the concept of cgi in php" article.

In PHP, cgi is the abbreviation of "Common Gateway Interface", which means public gateway interface. Cgi allows a client to request data from a web browser to a program executed on a web server. It is a standard that describes the transfer of data between the server and the request handler.

Operating environment of this tutorial: windows10 system, PHP7.1 version, DELL G3 computer

What does cgi mean in php

To put it simply, CGI and FastCGI are just a protocol, php-cgi is a program that implements cgi, and php-fpm is a manager that manages php-cgi.

The full name of CGI is "Public Gateway Interface" (Common Gateway Interface), a tool for HTTP servers to "talk" with programs on your or other machines, which must run on a network server.

Is an important Internet technology that allows a client to request data from a web browser to a program executing on a web server.

CGI describes a standard for transferring data between a server and a request handler.

What does CGI do? The purpose of CGI is to ensure that the data transmitted by web server is in a standard format for the convenience of the writers of CGI programs.

Web server (such as nginx) is just a distributor of content. For example, if you request / index.html, web server will find the file in the file system and send it to the browser, where static data is distributed. Well, if the request is / index.php, and according to the configuration file, nginx knows that this is not a static file and needs to find a PHP parser to process it, then he will simply process the request and hand it over to the PHP parser. What data does Nginx pass to the PHP parser? There must be url, query string, POST data, and HTTP header. Well, CGI is a protocol that specifies which data to send and what format to pass to the rear to process the request. Think about where the user data you use in your PHP code comes from.

When web server receives the request / index.php, it starts the corresponding CGI program, which is the parser of PHP. Next, the PHP parser parses the php.ini file, initializes the execution environment, processes the request, returns the processed result in the format specified by the CGI, and exits the process. Web server then returns the results to the browser.

Okay, CGI is a protocol, it has nothing to do with the process or anything. And what is fastcgi? Fastcgi is used to improve the performance of CGI programs.

FastCGI is developed and improved from CGI. The main disadvantage of the traditional CGI interface is poor performance, because every time the HTTP server encounters a dynamic program, it needs to restart the script parser to perform parsing, and the result is returned to the HTTP server.

Improve performance, so what's the performance problem with CGI programs? "the PHP parser parses the php.ini file and initializes the execution environment." this is it. Standard CGI performs these steps for each request (don't get tired! It's tiring to start the process!) So it takes a long time to process each time This is obviously unreasonable! So how does Fastcgi do it? First, Fastcgi starts a master, parses the configuration file, initializes the execution environment, and then starts multiple worker. When the request comes, the master is passed to a worker, and the next request can be accepted immediately. In this way, repetitive work is avoided, and the efficiency is naturally high. And when the worker is not enough, master can start several worker in advance according to the configuration; of course, when there are too many idle workers, some will be stopped, which improves performance and saves resources. This is fastcgi's management of the process.

And what is PHP-FPM? Is a program that implements Fastcgi and has been officially accepted by PHP.

As we all know, the interpreter of PHP is php-cgi. Php-cgi is just a CGI program, he can only parse the request and return the result, but can't manage the process (your Majesty, my concubine really can't do it! So there are some programs that can schedule php-cgi processes, such as spawn-fcgi separated by lighthttpd. Well, PHP-FPM is also such a thing, after a long period of development, it has gradually been recognized by everyone, and it is becoming more and more popular.

PHP-CGI is just a program that interprets PHP scripts.

PHP-FPM is a FastCGI manager

Operation principle of CGI mode

When Nginx receives the browser / index.php request, it first creates a process that implements the CGI protocol, here is php-cgi (PHP parser). Next, php-cgi parses the php.ini file, initializes the execution environment, processes the request, returns the processed result in the format specified by CGI, and exits the process. Finally, Nginx returns the results to the browser. The whole process is a Fork-And-Execute pattern. When the number of user requests is very large, it will occupy a large number of system resources such as memory, CPU time and so on, resulting in low performance. Therefore, under the CGI server, there will be as many CGI child processes as there are connection requests, and the main reason for the poor performance of CGI is that the child processes are loaded repeatedly.

The above is the content of this article on "what is the concept of cgi in php". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report