Home > Backend Development > PHP Problem > There are several startup modes in php

There are several startup modes in php

王林
Release: 2023-02-24 07:08:01
Original
3579 people have browsed it

There are several startup modes in php

Several common startup modes of PHP:

1. CGI mode

CGI is the universal gateway interface (Common Gateway Interface), it is a program. In layman's terms, CGI is like a bridge that connects the web page and the execution program in the Web server. It passes the instructions received by HTML to the execution of the server. program, and then returns the result of the server's execution of the program to the HTML page. CGI is extremely cross-platform and can be implemented on almost any operating system. CGI is already an older model and has rarely been used in recent years.

Every time there is a user request, a CGI sub-process will be created first, then the request will be processed, and the sub-process will be terminated after processing. This is the Fork-And-Execute mode. When the number of user requests is very large, a large amount of system resources such as memory, CPU time, etc. will be occupied, resulting in low performance. Therefore, a server using CGI will have as many CGI sub-processes as there are connection requests. Repeated loading of sub-processes is the main reason for low CGI performance.

If you do not want to embed PHP into server-side software (such as Apache) and install it as a module, you can choose to install it in CGI mode. Or use PHP with different CGI wrappers to create secure chroot and setuid environments for your code. In this way, each client requests a PHP file, and the Web server calls php.exe (php.exe under win, php under Linux) to interpret the file, and then returns the result of the interpretation to the client in the form of a web page. This installation method usually installs the PHP executable file into the cgi-bin directory of the web server. CERT Recommendation CA-96.11 recommends not placing any interpreters in the cgi-bin directory.

The advantage of this method is that it separates the Web Server from specific program processing, has a clear structure and strong controllability. At the same time, the disadvantage is that if there is high access demand, the CGI process Fork will It becomes a huge burden on the server. Just imagine that hundreds of concurrent requests cause the server to fork hundreds of processes. This is why CGI has always been notorious for low performance and high resource consumption.

2. FastCGI mode

FastCGI is an upgraded version of CGI. FastCGI is like a long-live CGI, which can be executed all the time. As long as it is activated, it will not take time to Fork every time (this is the most criticized fork-and-execute mode of CGI).

FastCGI is a scalable, high-speed interface for communication between HTTP servers and dynamic scripting languages. Most popular HTTP servers support FastCGI, including Apache, Nginx and lighttpd. At the same time, FastCGI is also supported by many scripting languages, including PHP.

FastCGI interface mode adopts C/S structure, which can separate the HTTP server and 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 be delivered directly to the FastCGI process for execution, and then the result is returned to the browser. This method allows the HTTP server to exclusively process static requests or return the results of the dynamic script server to the client, which greatly improves the performance of the entire application system.

3. CLI mode

PHP-CLI is the abbreviation of PHP Command Line Interface, as its name means, it is PHP The interface running on the command line is different from the PHP environment (PHP-CGI, ISAPI, etc.) running on the web server. In other words, PHP can not only write front-end web pages, it can also be used to write back-end programs. PHP CLI Shell Scripting applies to all PHP advantages, enabling the creation of either scripts or server-side systems or even with GUI applications. PHP-CLI mode is supported under both Windows and Linux.

[Advantages]

1) Using 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 will still have the opportunity to rebuild the process;

3) A resident main process is only responsible for task distribution, and the logic is clearer.

We often use "php -m" under Linux to find out which extensions PHP has installed, which is the PHP command line running mode; interested students can enter "php -h" to study the running mode in depth.

4. Module mode

Module mode is integrated in the form of mod_php5 module. At this time, the function of mod_php5 module is to receive PHP file requests passed by Apache. , and process these requests, and then return the processed results to Apache. If we configure the PHP module (mod_php5) in its configuration file before Apache starts, the PHP module registers the ap_hook_post_config hook of apache2 and starts this module when Apache starts to accept requests for PHP files.

In addition to this loading method at startup, Apache modules can be dynamically loaded at runtime, which means that the server can be expanded without the need to recompile the source code, or even without stopping the server at all. All we need to do is to send the signal HUP or AP_SIG_GRACEFUL to the server to notify the server to reload the module. But before dynamic loading, we need to compile the module into a dynamic link library. Dynamic loading at this time is to load the dynamic link library. The processing of dynamic link libraries in Apache is completed through the module mod_so, so the mod_so module cannot be dynamically loaded, it can only be statically compiled into the core of Apache. This means it is started along with Apache.

How does Apache load modules? Let’s take the mod_php5 module mentioned earlier as an example. First we need to add a line to Apache's configuration file httpd.conf:

LoadModule php5_module modules/mod_php5.so
Copy after login

Here we use the LoadModule command. The first parameter of the command is the name of the module. The name 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 a module while the server is running, you can send the signal HUP or AP_SIG_GRACEFUL to the server. Once the signal is received, Apache will reload the module without restarting the server.

This operating mode is what we often used when using the apache server in the windows environment. In modularization (DLL), PHP is started and run together with the web server. (It is an extension of apache based on CGI to speed up the operating efficiency of PHP).

5. ISAPI mode

ISAPI (Internet Server Application Program Interface) is a A set of API interfaces for Internet services. An ISAPI DLL can reside in memory after being activated by a user request and wait for another user request. Multiple user request processing functions can also be set in one DLL. In addition, an ISAPI DLL The application and the WWW server are in the same process, and the efficiency is significantly higher than CGI. (Due to Microsoft's exclusivity, it can only run in the windows environment)

As an Apache module, the Apache server pre-generates multiple process copies and resides in the memory after the system starts. Once a request occurs, it will be immediately Use these spare sub-processes for processing, so there is no delay caused by spawning sub-processes. These server copies do not exit immediately after processing an HTTP request, but stay in the computer waiting for the next request. The response to client browser requests is faster and the performance is higher.

Recommended tutorial: PHP video tutorial

The above is the detailed content of There are several startup modes in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template