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 operation mode of PHP?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "PHP operation mode is how", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let Xiaobian take you to learn "PHP operation mode is what" bar!

PHP Run Mode SAPI

The PHP runtime mode mentioned here actually refers to SAPI (Server Application Programming Interface). SAPI provides PHP with an interface to communicate with the outside world, and PHP uses this interface to interact with other applications. PHP also provides a variety of SAPI for different application scenarios, common ones are: apache, apache2filter, apache2handler, cli, cgi, embed, fast-cgi, isapi, etc.

php_sapi_name() -Returns the type of interface between the web server and PHP. Possible values returned include aolserver, apache, apache2filter, apache2 handler, audium, cgi (until PHP 5.3), cgi-fcgi, cli, cli-server, continuity, embed, fpm-fcgi, isapi, litespeed, milter, nsapi, phttpd, pi3web, roxen, thttpd, tux, and webjames.

Many of PHP's built-in SAPI implementations are no longer maintained or have become somewhat non-mainstream, and the PHP community is currently considering moving some SAPI out of the code base. The community's concern for many features is that unless they are really necessary, or some features are nearly universal, they are in PECL libraries.

Five of the more common modes of operation are described below.

CLI mode

CLI (Command Line Interface), which is the command line interface, PHP is installed by default. This interface allows you to interact with PHP in a shell environment. Typing php -v into the terminal will get something like the following (PHP installed):

Because of CLI, we can run PHP scripts directly from the terminal command line, just like using shell and Python, without relying on WEB servers. For example, the Artisan command-line tool in Laravel framework is actually a PHP script to help us quickly build Laravel applications.

CGI (Common Gateway Interface) 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.

Web servers are simply distributors of content. For example, Nginx, if the client requests/index.html, then Nginx will go to the file system to find this file and send it to the browser, where static data is distributed; if the client is now requesting/index.php, according to the configuration file, Nginx knows that this is not a static file and needs to find a PHP parser to process, then it will pass this request to the PHP parser after simple processing. What data does Nginx pass to PHP Parsers? URL must have it, query string must have it, POST data must also have it, HTTP request header can not be less, OK, CGI is to specify what data to pass, in what format to pass to the post-processing of this request protocol.

CGI mode operation principle: When Nginx receives the browser/index.php request, it will first create a process corresponding to the CGI protocol, here is php-cgi (PHP parser). php-cgi then parses the php.ini file, initializes the execution environment, processes the request, returns the processed result in CGI format, 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, etc., resulting in low performance. Therefore, in CGI mode server, there will be as many CGI child processes as there are connection requests, and repeated loading of child processes is the main reason for poor CGI performance.

The CGI model has the advantage of being completely independent of any server and acting only as an intermediary: providing interfaces to WEB servers and scripting languages or being completely independent programming languages. They complete the data transfer through CGI protocol wiring. The advantage of doing so is to minimize the correlation between them, making them more independent and independent.

CGI is an old pattern and has been rarely used in recent years.

FastCGI (Fast Common Gateway Interface) is a protocol that allows interactive programs to communicate with Web servers. FastCGI is an enhanced version of the earlier Common Gateway Interface (CGI). FastCGI aims to reduce the overhead of interaction between web servers and CGI programs so that servers can handle more web requests simultaneously.

According to the definition, FastCGI is also a protocol. A program that implements FastCGI protocol is more like a long-live CGI protocol program. As long as it is activated, it can be executed all the time without spending time to fork every time.

FastCGI mode works: After FastCGI process manager starts, it first parses php.ini file, initializes execution environment, and then starts several CGI protocol interpreter daemons (multiple php-cig or php-cgi.exe can be seen in the process management) and wait for a connection from the WEB server; when the client request arrives at the WEB server, the FastCGI process manager will select and connect to a CGI interpreter, and the WEB server will send CGI environment variables and standard inputs to the child process php-cgi of FastCGI; When the php-cgi child completes processing, it returns standard output and error messages to the WEB server; at this point the php-cgi child closes the connection, the request ends, and then continues to wait and process the next request connection from the FastCGI process manager.

FastCGI mode adopts C/S structure, which can separate WEB server and script parsing server, and start one or more script parsing daemons on script parsing server at the same time. Each time the WEB 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 WEB server to exclusively handle static requests or return the results of the dynamic script server to the client, which greatly improves the performance of the entire application system.

In addition, in CGI mode, php-cgi needs to restart the php-cgi process to make the new php-ini configuration take effect after php.ini configuration changes, and it cannot be restarted smoothly. In FastCGI mode, PHP-FPM can generate new child processes to achieve a smooth restart of php.ini modifications.

PHP-FPM (PHP-FastCGI Process Manager) is a process manager that implements FastCGI protocol in PHP language, written and implemented by Andrei Nigmatulin, which has been officially included in PHP and integrated into the kernel.

Advantages of FastCGI mode:

From the stability point of view, FastCGI mode is to run CGI protocol programs with independent process pools. If a single process dies, the system can easily discard it and then reassign new processes to run logic.

From a security perspective, FastCGI patterns support distributed computing. FastCGI program and host Server completely independent, FastCGI program hanging also does not affect the Server;

From the performance point of view, FastCGI mode separates the dynamic logic processing from the Server, and the heavy load IO processing is still left to the host Server, so that the host Server can concentrate on processing IO. For an ordinary dynamic web page, the logic processing may only be a small part, and a large number of static images.

FastCGI mode is currently the mainstream PHP WEB service operation mode, with efficient and reliable performance, recommended for everyone to use.

Module mode

PHP is often paired with Apache servers to form a LAMP companion runtime environment. Integrating PHP as a submodule into Apache is Module mode. Common configurations in Apache are as follows:

LoadModule php5_module modules/mod_php5.so

This uses the LoadModule command, whose first argument is 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 do so by sending a signal HUP or AP_SIG_GRACEFUL to the server. Upon receiving this signal, Apache will reload the module without restarting the server. Start this module at Apache startup to accept requests for PHP files by registering the ap_hook_post_config hook in apache2.

For example, when a client accesses a PHP file, Apache calls php5_module to parse the PHP script. Each time Apache receives a request, it spawns a process to connect to PHP to complete the request. In Module mode, PHP is sometimes coded into Apache as a module, making it difficult to locate whether a problem is PHP or Apache.

In the past, with its wealth of modules and functionality, companies tended to use Apache as a WEB server, so PHP + Apache combinations running in Module mode were common. In recent years, the rise of asynchronous event-driven, high-performance Nginx servers has led to rapid market share growth, and PHP + Nginx combinations running in FastCGI mode have better performance and have a tendency to catch up with Apache. ISAPI mode

ISAPI (Internet Server Application Program Interface) is a set of API interfaces for Internet services provided by Microsoft. An ISAPI DLL can be activated by a user's request for a long time, waiting for another user request. It can also set multiple user request processing functions in a DLL. In addition, ISAPI DLL applications and WEB servers are in the same process, and the efficiency is significantly higher than CGI. Due to Microsoft exclusivity, it can only run in Windows environment.

At this point, I believe that everyone has a deeper understanding of "what PHP mode is", so let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

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