Detailed explanation of FastCGI in php (kernel exploration)
The full name of CGI is "Common Gateway Interface", which 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 the client and the program. One of the purposes of CGI is to be independent of any language, so CGI can be written in any language, as long as the language has standard input, output and environment variables. Such as php, perl, tcl, etc.
FastCGI is a protocol for communication between Web servers and handlers. It is an improved solution for CGI. FastCGI is like a long-live CGI. It can be executed all the time. When a request arrives, it does not take time to fork a process to handle it (this is the most criticized fork-and-execute mode of CGI). Precisely because it is just a communication protocol, it also supports distributed computing, that is, FastCGI programs can be executed on hosts other than the website server and accept requests from other website servers.
FastCGI is a language-independent, scalable architecture CGI open extension that keeps the CGI interpreter process in memory to obtain higher performance. Repeated loading of CGI programs is the main reason for low CGI performance. If CGI programs are kept in memory and accepted to be scheduled by the FastCGI process manager, it can provide good performance, scalability, Fail-Over features, etc.
Generally, the entire workflow of FastCGI is as follows:
The FastCGI process manager (IIS ISAPI or Apache Module) is loaded when the Web Server starts.
FastCGI process The manager initializes itself, starts multiple CGI interpreter processes (visible multiple php-cgi) and waits for connections from the Web Server.
When a client request arrives at the Web Server, the FastCGI process manager selects and connects to a CGI interpreter. The web server sends CGI environment variables and standard input to the FastCGI subprocess php-cgi.
After the FastCGI sub-process completes processing, it returns standard output and error information to the Web Server from the same connection. When the FastCGI child process closes the connection, the request is processed. The FastCGI child process then waits for and handles the next connection from the FastCGI process manager (running in the Web Server). In CGI mode, php-cgi exits at this point.
PHP's CGI implements the Fastcgi protocol. It is a TCP or UDP protocol server that accepts requests from the Web server. When it is started, it creates a socket listener for the TCP/UDP protocol server and receives related requests for processing. Then the life cycle of PHP is entered: module initialization, sapi initialization, processing of PHP requests, module closing, sapi closing, etc. constitute the entire CGI life cycle.
Taking TCP as an example, on the TCP server side, the following steps are generally performed:
Call the socket function to create a streaming socket for TCP;
Call the bind function to bind the local address of the server to the previously created socket;
Call the listen function to use the newly created socket as a listener, waiting for the connection initiated by the client. When the client has When multiple connections are connected to this socket, they may need to be queued;
The server process calls the accept function and enters the blocking state until a client process calls the connect function to establish a connection;
When a connection is established with the client, the server calls the read_stream function to read the client's request;
After processing the data, the server calls the write function to send a response to the client.
FastCGI for PHP enables all your PHP applications to run through mod_fastci instead of mod_phpsusexec. FastCGI applications are fast because they are durable and stable and do not have to be started and initialized for every request. This enables the development of applications that would otherwise be impractical in the CGI paradigm (such as a large script, or an application that requires connection to a single or multiple databases).
Advantages of FastCGI:
PHP scripts run faster (3 to 30 times). The PHP interpreter is loaded into memory instead of being read from memory each time it is needed, greatly improving the performance of sites that rely on scripts.
Need to use less system resources. Since the server does not have to load the PHP interpreter every time it is needed, you can increase the transmission speed of the site very much without increasing the load on the CPU.
No need to make any changes to the existing code. Everything out there works with PHP's FastCGI.
But there are also potential problems:
You only have one available php.ini file for all subdirectories (/home/USERNAME/public_html/php.ini). This is necessary to optimize your website code. If you need multiple php.ini files to accommodate different scripting needs, you can disable PHP's fast CGI in any subdirectory and leave it active elsewhere. Please contact support if you need to do this.
Any upgrades you make to your PHP environment (such as changes to the php.ini file) will have a delay of several minutes. This is because your php.ini file has been loaded into memory for faster speed, rather than being re-read from memory each time it is needed.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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

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

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

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

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,

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

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 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.