In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article is to share with you about the common operation modes of PHP, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
Operation mode
About the five common operation modes of PHP:
1) CGI (Universal Gateway Interface / Common Gateway Interface)
2) FastCGI (resident CGI / Long-Live CGI)
3) CLI (command line run / Command Line Interface)
4) Web module mode (running mode of Web servers such as Apache)
5) ISAPI (Internet Server Application Program Interface)
1.1.The CGI mode
CGI is the Universal Gateway Interface (Common Gateway Interface), which is a program. Generally speaking, CGI is like a bridge that connects the web page with the execution program in the Web server. It transmits the instructions received by HTML to the server execution program, and then returns the results of the server execution program to the HTML page. The cross-platform performance of CGI can be implemented on almost any operating system. CGI is an older model that has been rarely used in recent years.
Every time there is a user request, a child process of CGI is created first, then the request is processed, and then the child process is finished. This is the 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, the server in CGI mode has as many connection requests as there are CGI child processes, and the repeated loading of child processes is the main reason for the poor performance of CGI.
If you do not want to embed PHP into server-side software (such as Apache) as a module installation, you can choose to install it in CGI mode. Or use PHP in different CGI wrappers to create secure chroot and setuid environments for your code. So each client requests a PHP file, and the Web server calls php.exe (php.exe,linux is php in win) to interpret the file, and then returns the interpreted result to the client as a web page. This installation usually installs the executable file of PHP to the cgi-bin directory of the web server.
The CERT recommendation CA-96.11 recommends that you do not put any interpreters in the cgi-bin directory. The advantage of this approach is to separate the Web Server from the specific procedures, with a clear structure and strong controllability, while the disadvantage is that if the process Fork of CGI becomes a great burden on the server in the case of high access requirements, imagine hundreds of concurrent requests leading to hundreds of processes in the server Fork. This is why CGI has always had a reputation for low performance and high resource consumption.
1.2.The FastCGI mode
FastCGI is an upgraded version of CGI, and FastCGI is like a resident (long-live) CGI that can be executed all the time, as long as it is activated and does not take the time to Fork every time (this is CGI's most criticized fork-and-execute mode).
FastCGI is a scalable, high-speed communication interface between HTTP server and dynamic scripting languages. Most popular HTTP server supports FastCGI, including Apache, Nginx, and lighttpd, while FastCGI is also supported by many scripting languages, including PHP.
The FastCGI interface adopts Cramp S structure, which can separate the HTTP server from the script parsing server, and start one or more script parsing daemons on the script parsing server. Every time the HTTP server encounters a dynamic program, it can deliver it directly to the FastCGI process for execution, and then return the results to the browser. This approach allows the HTTP server to handle static requests exclusively or return the results of the dynamic script server to the client, which greatly improves the performance of the entire application system.
[principle]
1) load the FastCGI process Manager (IIS ISAPI or Apache Module) when Web Server starts
2) the FastCGI process manager initializes itself, starts multiple CGI interpreter processes (see multiple php-cgi.exe or php-cig) and waits for a connection from Web Server
3) when the client request arrives at Web Server, the FastCGI process manager selects and connects to a CGI interpreter. Web server sends CGI environment variables and standard input to FastCGI child process php-cgi
4) the FastCGI child process returns the standard output and error information from the same connection to Web Server after processing. When the FastCGI child process closes the connection, the request is processed. The FastCGI child process then waits and processes the next connection from the FastCGI process manager (running in WebServer). In normal CGI mode, php-cgi.exe exits here.
In CGI mode, you can imagine how slow CGI usually is. Each Web request PHP must reparse the php.ini, reload all dll extensions, and reinitialize all data structures. With FastCGI, all of this happens only once when the process starts. An added benefit is that persistent database connections (Persistent database connection) work.
Note: PHP's FastCGI process manager is PHP-FPM (PHP-FastCGI Process Manager)
[advantages]
1) in terms of stability, FastCGI runs CGI as a separate process pool. If a single process dies, the system can easily discard it, and then reassign a new process to run the logic.
2) from the security point of view, FastCGI supports distributed computing. FastCGI is completely independent of the host Server. FastCGI down will not bring down Server.
3) from the performance point of view, FastCGI separates the dynamic logic processing from the Server, and the heavy load of IO processing is left to the host Server, so that the host Server can wholeheartedly do IO, for an ordinary dynamic web page, logic processing may be only a small part, a large number of static such as pictures.
[shortcomings]
After talking about the advantages, let's also talk about the disadvantages. From my actual use, FastCGI mode is more suitable for servers in production environment. But it is not suitable for developing machines. Because when using Zend Studio to debug a program, because FastCGI thinks the PHP process timed out, it returns a 500error on the page. This is very annoying, so I switched back to ISAPI mode on the development machine. New versions of some servers are not well supported, and whether a modular installation that is not required for distributed load balancing is a better choice. At present, the communication between FastCGI and Server is not smart enough. If a FastCGI process is executed for too long, it will be killed and restarted as a dead process, which is troublesome to deal with long-time tasks, and this also makes FastCGI unable to allow online debugging. Because it is multi-process, it consumes more server memory than CGI multithreading. The PHP-CGI interpreter consumes 7 to 25 megabytes of memory per process, and multiplying this number by 50 or 100 is a large amount of memory.
1.3 CLI mode
PHP-CLI is the abbreviation of PHP Command Line Interface, which, as its name implies, is the interface on which PHP runs on the command line, which is different from the PHP environment (PHP-CGI,ISAPI, etc.) running on the Web server. In other words, PHP can not only write foreground web pages, it can also be used to write background programs. PHP's CLI Shell script is suitable for all PHP advantages, so that the creation of either scripts or systems or even servers with GUI applications supports PHP-CLI mode under both Windows and Linux.
[advantages]
1) use multiple processes. After the child process ends, the kernel will be responsible for recycling resources.
2) using multiple processes, the abnormal exit of the child process will not cause the entire process Thread to exit, and the parent process still has a chance to rebuild the process.
3) A resident main process, only responsible for task distribution, the logic is clearer.
We often use "php-m" to find out which extensions are installed in PHP under Linux, which is the PHP command line running mode; interested students can type "php-h" to study the running mode in depth.
1.4 Module Mod
Module mode is integrated in the form of mod_php5 module, and the function of mod_php5 module is to receive PHP file requests from Apache, process these requests, and then return the processed results to Apache. If we configure the PHP module in its configuration file before Apache starts
(mod_php5), the PHP module starts this module when Apache starts by registering apache2's ap_hook_post_config hook to accept requests for PHP files.
In addition to this startup loading method, Apache's modules can be loaded dynamically at run time, which means that the server can be extended without recompiling the source code or even stopping the server at all. All we need to do is send a signal to the server HUP or AP_SIG_GRACEFUL to inform the server to reload the module. But before loading dynamically, we need to compile the module into a dynamic link library. The dynamic loading at this time is to load the dynamic link library. The processing of dynamic link libraries in Apache is done through the module mod_so, so the mod_so module can not be dynamically loaded, it can only be statically compiled into the core of Apache. This means that it was started with Apache.
How does Apache load the module? Let's take the mod_php5 module mentioned earlier as an example. First, we need to add a line to the configuration file httpd.conf of Apache:
LoadModule php5_module modules/mod_php5.so
Here we use the LoadModule command, whose * * arguments are the name of the module, which can be found in the source code of the module implementation. The second option is the path where the module is located. If you need to load the module while the server is running, you can send a signal HUP or AP_SIG_GRACEFUL to the server, and once the signal is received, Apache will reload the module without restarting the server.
This mode of operation is often used by us to use apache server in windows environment, while in DLL, PHP is started and run together with Web server. (it is an extension of apache based on CGI to speed up the efficiency of PHP.)
1.5 ISAPI mode
ISAPI (Internet Server Application Program Interface) is a set of API interface for Internet service provided by Microsoft. An ISAPI DLL can stay in memory long after being activated by a user request, waiting for another user request, and can also set up multiple user request handling functions in a DLL. In addition, ISAPI's DLL application and WWW server are in the same process, which is significantly more efficient than CGI. (due to Microsoft's exclusivity, it can only run in windows environment.)
PHP as an Apache module, the Apache server resides in memory as multiple process copies after the system starts. Once a request appears, it immediately uses these spare child processes for processing, so that there is no delay caused by generating child processes. These server replicas do not exit immediately after processing an HTTP request, but stay on the computer waiting for the next request. Faster response and higher performance to requests from customer browsers.
The above are the common operation modes in PHP, and the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.