Table of Contents
PHP running mode
SAPI
CLI mode
CGI mode
FastCGI mode
This uses the
ISAPI mode
Home Backend Development PHP Tutorial Detailed summary of PHP operating modes

Detailed summary of PHP operating modes

Dec 16, 2021 pm 03:36 PM
php

PHP running mode

SAPI

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

Detailed summary of PHP operating modes

php_sapi_name() — Returns the type of interface between the web server and PHP. Possible returned values ​​include aolserver, apache, apache2filter, apache2handler, caudium, 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 the SAPI implementations built into PHP are no longer maintained or have become somewhat non-mainstream. The PHP community is currently considering moving some SAPIs out of the code base. The community considers many features to be in the PECL library unless they are really necessary, or some features are almost universal.

Next, five of the more common operating modes will be explained.

CLI mode

CLI (Command Line Interface), which is the command line interface, PHP will be installed by default. Through this interface, you can interact with PHP in a shell environment. Enter php -v in the terminal, you will get a result similar to the picture below (assuming PHP is installed):

Detailed summary of PHP operating modes

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

CGI mode

CGI (Common Gateway Interface, Common Gateway Interface) is an important Internet technology that allows a client to go from a web browser to a program executing on a network server Request data. CGI describes a standard for transferring data between servers and request handlers.

WEB server is just a distributor of content. For example, Nginx, if the client requests /index.html, then Nginx will find this file in the file system and send it to the browser. What is distributed here is static data; if the client now requests /index.php, according to the configuration file, Nginx knows that this is not a static file and needs to be processed by a PHP parser, so it will hand the request to the PHP parser after simple processing. What data will Nginx pass to the PHP parser? The URL must be present, the query string must be present, the POST data must be present, and the HTTP request header must not be missing. Well, CGI is a protocol that stipulates what data is to be transmitted and in what format it is passed to the backend for processing the request. .

CGI mode operating principle: When Nginx receives the request from the browser /index.php, it will first create a process corresponding to the CGI protocol, here is php-cgi (PHP parser). Next, php-cgi will parse the php.ini file, initialize the execution environment, process the request, return the processed result in the format specified by CGI, and exit the process. Finally, Nginx returns the results to the browser. The entire process is a Fork-And-Execute pattern. When the number of user requests is very large, a large amount of system resources such as memory and CPU time will be occupied, resulting in low performance. Therefore, under a CGI server, there will be as many CGI sub-processes as there are connection requests. Repeated loading of sub-processes is the main reason for low CGI performance.

Detailed summary of PHP operating modes

#The advantage of CGI mode is that it is completely independent of any server and only acts as an intermediary: providing interfaces to WEB servers and scripting languages ​​or completely independent programming languages. They are wired through the CGI protocol to complete data transfer. The advantage of this is to minimize the correlation between them, making each more independent and independent of each other.

CGI mode is already a relatively old mode and has rarely been used in recent years.

FastCGI mode

FastCGI (Fast Common Gateway Interface, 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 is committed to reducing the interaction overhead between the web server and CGI programs, so that the server can handle more web page requests at the same time.

According to the definition, FastCGI is also a protocol. A program that implements the FastCGI protocol is more like a resident (long-live) CGI protocol program. As long as it is activated, it can always be executed. , it won’t take time to fork every time.

FastCGI mode operating principle: After the FastCGI process manager is started, it will first parse the php.ini file, initialize the execution environment, and then start multiple CGI protocol interpreter daemons (multiple CGI protocol interpreter daemons can be seen in the process management php-cig or php-cgi.exe) and wait for the connection from the WEB server; when the client request reaches the WEB server, the FastCGI process manager will select and connect to a CGI interpreter, and the WEB server will CGI environment variables and standard The input is sent to the sub-process php-cgi of FastCGI; after the php-cgi sub-process completes the processing, it returns the standard output and error information to the WEB server; at this time, the php-cgi sub-process will close the connection and the request will be processed. Then continue to wait and process the next request connection from the FastCGI process manager.

Detailed summary of PHP operating modes

FastCGI mode adopts a C/S structure, which can separate the WEB server and the script parsing server, and start one or more script parsing daemons on the script parsing server at the same time. Every time the WEB 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 WEB 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.

In addition, in CGI mode, after php-cgi changes the php.ini configuration, the php-cgi process needs to be restarted for the new php-ini configuration to take effect, and smooth restart is not possible. In FastCGI mode, PHP-FPM can achieve smooth restart after php.ini modification by generating a new child process.

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

Advantages of FastCGI mode:

  • From a stability point of view, FastCGI mode uses an independent process pool to run CGI protocol programs. If a single process dies, the system It can be easily discarded and then reassigned to a new process to run the logic;

  • From a security perspective, FastCGI mode supports distributed computing. The FastCGI program is completely independent from the host server. Even if the FastCGI program hangs up, it will not affect the server; The IO processing is still left to the host server, so that the host server can handle the IO wholeheartedly. For an ordinary dynamic web page, there may only be a small part of the logical processing, and a large number of static images and other images.

  • FastCGI mode is currently the mainstream WEB service operating mode of PHP. It has efficient and reliable performance and is recommended for everyone to use.

Module modePHP is often paired with the Apache server to form a LAMP supporting operating environment. Integrating PHP as a submodule into Apache is the Module mode. The common configuration in Apache is as follows:

LoadModule php5_module modules/mod_php5.so
Copy after login

This uses the

LoadModule

command. The first parameter of the command is 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 the 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. By registering to the ap_hook_post_config hook of apache2, this module is started when Apache starts to accept requests for PHP files. For example, when the client accesses a PHP file, Apache will call php5_module to parse the PHP script. Every time Apache receives a request, it will create a process to connect to PHP to complete the request. In Module mode, sometimes PHP is compiled into Apache as a module, making it difficult to determine whether a problem occurs with PHP or Apache.

In the past, with its rich modules and functions, enterprises often used Apache as a WEB server, so the combination of PHP and Apache running in Module mode was very common. In recent years, with the rise of asynchronous event-driven, high-performance Nginx servers, the market share has grown rapidly. The PHP Nginx combination running in FastCGI mode has better performance and has 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 reside in memory after being activated by a user request. , waiting for another request from the user, and multiple user request processing functions can be set in one DLL. In addition, the ISAPI DLL application and the WEB server are in the same process, and the efficiency is significantly higher than that of CGI. Due to Microsoft's exclusivity, it can only run in the Windows environment.

is rarely used and will not be introduced in detail here.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of Detailed summary of PHP operating modes. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles