Table of Contents
CGI
FastCGI
FastCGI Features
How FastCGI works
Disadvantages of FastCGI
PHP-CGI
PHP-FPM
Spawn-FCGI
Comparison between PHP-FPM and spawn-CGI
Home Backend Development PHP Tutorial Concept understanding: CGI, FastCGI, PHP-CGI and PHP-FPM_PHP tutorial

Concept understanding: CGI, FastCGI, PHP-CGI and PHP-FPM_PHP tutorial

Jul 13, 2016 am 10:33 AM
cgi php kernel

CGI

The full name of CGI is "Common Gateway Interface". It is a tool for the HTTP server to "talk" with programs on your or other machines. The program must run on the network server.

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

FastCGI is like a long-live CGI. It 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 of CGI). -execute mode). 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. Its main behavior is to keep the CGI interpreter process in memory and thus obtain higher performance. As we all know, repeated loading of the CGI interpreter is the main reason for low CGI performance. If the CGI interpreter remains in memory and accepts FastCGI process manager scheduling, it can provide good performance, scalability, Fail-Over features, etc.

FastCGI Features

  1. FastCGI is language independent.
  2. FastCGI in-process applications run independently of the core web server, providing a more secure environment than APIs. APIs link an application's code with the core web server, meaning an application with the wrong API could break other applications or the core server. Malicious API application code can even steal the keys of another application or core server.
  3. FastCGI technology currently supports languages: C/C++, Java, Perl, Tcl, Python, SmallTalk, Ruby, etc. Related modules are also available on popular servers such as Apache, ISS, Lighttpd, etc.
  4. FastCGI does not depend on the internal architecture of any web server, so even if server technology changes, FastCGI remains stable.

How FastCGI works

  1. Load the FastCGI process manager (IIS ISAPI or Apache Module) when the Web Server starts
  2. FastCGI process manager initializes itself, starts multiple CGI interpreter processes (visible multiple php-cgi) and waits for connections from the Web Server.
  3. When a client request reaches 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.
  4. 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.

In the above case, you can imagine how slow CGI usually is. Every web request to PHP must reparse php.ini, reload all extensions and reinitialize all data structures. With FastCGI, all of this happens only once, when the process starts. An added bonus is that persistent database connections work.

Disadvantages of FastCGI

Because it is multi-process, it consumes more server memory than CGI multi-threading. The PHP-CGI interpreter consumes 7 to 25 megabytes of memory per process. Multiply this number by 50 or 100 to get a large amount of memory.

Nginx 0.8.46+PHP 5.2.14 (FastCGI) server has 30,000 concurrent connections. The 10 Nginx processes started consume 150M of memory (15M*10=150M), and the 64 php-cgi processes started consume 1280M. Memory (20M*64=1280M), plus the memory consumed by the system itself, consumes less than 2GB of memory in total. If the server memory is small, you can only open 25 php-cgi processes, so that the total memory consumed by php-cgi is only 500M.
The above data is excerpted from Nginx 0.8.x + PHP 5.2.13 (FastCGI) to build a web server that is ten times better than Apache (version 6)

PHP-CGI

PHP-CGI is PHP’s own FastCGI manager.

Disadvantages of PHP-CGI:

  1. After php-cgi changes the php.ini configuration, you need to restart php-cgi to make the new php-ini take effect, and smooth restart is not possible.
  2. Kill the php-cgi process directly, and php will not be able to run. (PHP-FPM and Spawn-FCGI do not have this problem, the daemon process will smoothly regenerate new child processes.)

PHP-FPM

PHP-FPM is a PHP FastCGI manager, which is only used for PHP and can be downloaded at http://php-fpm.org/download.

PHP-FPM is actually a patch of the PHP source code, designed to integrate FastCGI process management into the PHP package. It must be patched into your PHP source code and can be used after compiling and installing PHP.

Now we can download the branch that directly integrates PHP-FPM in the latest PHP 5.3.2 source tree. It is said that the next version will be integrated into the main branch of PHP. Compared with Spawn-FCGI, PHP-FPM has better CPU and memory control, and the former is easy to crash and must be monitored with crontab, while PHP-FPM does not have such troubles.

PHP5.3.3 has integrated php-fpm and is no longer a third-party package. PHP-FPM provides a better PHP process management method, which can effectively control memory and processes, and can smoothly reload PHP configuration. It has more advantages than spawn-fcgi, so it is officially included in PHP. You can enable PHP-FPM by passing the –enable-fpm parameter in ./configure.

Spawn-FCGI

Spawn-FCGI is a universal FastCGI management server. It is part of lighttpd. Many people use Lighttpd's Spawn-FCGI to perform management work in FastCGI mode, but it has many shortcomings. The emergence of PHP-FPM has somewhat alleviated some problems, but PHP-FPM has the disadvantage of having to recompile, which may pose a considerable risk (refer) to some already running environments. It can be used directly in PHP 5.3.3 PHP-FPM.

Spawn-FCGI has now become a separate project, which is more stable and brings convenience to the configuration of many Web sites. Many sites have paired it with nginx to solve dynamic web pages.

The latest lighttpd does not include this piece (http://www.lighttpd.net/search?q=Spawn-FCGI), but it can be found in previous versions. It is included in the lighttpd-1.4.15 version (http://www.lighttpd.net/download/lighttpd-1.4.15.tar.gz). The current download address of Spawn-FCGI is http://redmine.lighttpd .net/projects/spawn-fcgi, the latest version is http://www.lighttpd.net/download/spawn-fcgi-1.6.3.tar.gz.

Note: For the latest Spawn-FCGI, you can search for "Spawn-FCGI" on the lighttpd.net website to find its latest version release address.

Comparison between PHP-FPM and spawn-CGI

PHP-FPM is very convenient to use. The configuration is in the PHP-FPM.ini file, and startup and restart can be done from php/sbin/PHP-FPM. What is more convenient is that after modifying php.ini, you can directly use PHP-FPM reload to load it. You can complete the modification and loading of php.ini without killing the process
The results show that using PHP-FPM can significantly improve the performance of PHP. . The CPU recycling speed of the process controlled by PHP-FPM is relatively slow, and the memory is allocated evenly.
The CPU of the process controlled by Spawn-FCGI drops quickly, and the memory allocation is uneven. There are many processes that appear to be unallocated, while others are highly occupied. It may be caused by uneven distribution of process tasks. This also leads to a decrease in overall response speed. The reasonable distribution of PHP-FPM leads to the mention of overall response and the average of tasks.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/752545.htmlTechArticleCGI The full name of CGI is "Common Gateway Interface", the HTTP server is connected to your or other machines A tool for "talking" with a program. The program must be run on the network server...
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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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 and CGI file upload and download technology: how to implement file management functions PHP and CGI file upload and download technology: how to implement file management functions Jul 21, 2023 am 11:19 AM

File upload and download technology with PHP and CGI: How to implement file management functions Introduction: File upload and download are one of the common functions in modern web applications. This article will introduce how to implement file upload and download functions using PHP and CGI programming languages, and show some code examples to demonstrate how to manage uploaded and downloaded files. Here’s what we’re going to cover: Basic concepts of file upload using PHP to implement file upload CGI to implement file upload Basic concepts of file download using PHP to implement file download CGI implementation under the file

How to use PHP and CGI to implement user registration and login functions How to use PHP and CGI to implement user registration and login functions Jul 21, 2023 pm 02:31 PM

How to use PHP and CGI to implement user registration and login functions User registration and login are one of the necessary functions for many websites. In this article, we will introduce how to use PHP and CGI to achieve these two functions. We'll demonstrate the entire process with a code example. 1. Implementation of the user registration function The user registration function allows new users to create an account and save their information to the database. The following is a code example to implement the user registration function: Create a database table First, we need to create a database table to store user information. Can

Explore the working principles, similarities and differences between PHP CLI and CGI Explore the working principles, similarities and differences between PHP CLI and CGI Mar 11, 2024 pm 12:39 PM

Working principles and similarities and differences In web development, PHP is a commonly used programming language that can interact with web servers in different ways, the most common of which are through PHPCLI (CommandLineInterface) and PHPCGI (CommonGatewayInterface). This article will explore the working principles, similarities and differences between PHPCLI and CGI, and provide specific code examples to illustrate the differences between them. 1. PHP

How to use PHP and CGI to implement the video playback function of the website How to use PHP and CGI to implement the video playback function of the website Jul 22, 2023 pm 07:45 PM

How to use PHP and CGI to implement the video playback function of the website In today's multimedia era, video has become an indispensable part of the website content. In order to provide a good user experience, the website needs to implement video playback function. This article will introduce how to use PHP and CGI to implement the video playback function of the website, and provide code samples for reference. 1. Preparation Before starting, you need to ensure that the server has PHP and CGI modules installed. You can do this by running the phpinfo() function or typing "php

PHP and CGI technologies compared: How to choose the right one for your website PHP and CGI technologies compared: How to choose the right one for your website Jul 22, 2023 am 09:45 AM

Comparison of PHP and CGI technologies: How to choose the right website for you With the development of the Internet, CGI (Common Gateway Interface) and PHP (Hypertext Preprocessor) have become one of the most commonly used website development technologies. This article will compare these two technologies to help you choose the right development technology for your website. 1. Overview PHP is a very popular server-side scripting language that is widely used for dynamic website development. It is open source, supports multiple operating systems, and has powerful database connection and processing capabilities. Developers can use simple syntax

Using machine learning to reconstruct faces in videos Using machine learning to reconstruct faces in videos Apr 08, 2023 pm 07:21 PM

Translator | Reviewed by Cui Hao | Sun Shujuan begins with a collaborative study between China and the UK that has devised a new way to recreate faces in video. This technology can enlarge and reduce facial structure with high consistency and no trace of artificial trimming. Typically, this transformation of facial structure is achieved through traditional CGI methods, which rely on detailed and expensive motion capping, rigging and texturing procedures to completely reconstruct the face. Unlike traditional methods, CGI in the new technology is integrated into the neural pipeline as a parameter for 3D facial information and serves as the basis for the machine learning workflow. The author points out: “Our goal is to refine facial contours based on natural faces in the real world.

The relationship between cgi, fast-cgi and php-fpm (with flow chart) The relationship between cgi, fast-cgi and php-fpm (with flow chart) Oct 08, 2022 pm 02:07 PM

CGI is a protocol and has nothing to do with the process. For example, when the web server (nginx) receives a PHP network request, nginx needs to find the PHP parser according to the configuration file. After simple processing, some of the requested information is handed over to The PHP parser stipulates which protocols are to be transmitted and the format in which they are transmitted. This standard is called the cgi protocol.

How to use PHP and CGI to implement website paging and sorting functions How to use PHP and CGI to implement website paging and sorting functions Jul 21, 2023 pm 01:39 PM

How to use PHP and CGI to implement the paging and sorting functions of a website Preface Paging and sorting of a website are very common and important functions, especially for the display and processing of large amounts of data. In this article, we will use PHP and CGI (Common Gateway Interface) technology to implement the paging and sorting functions of the website, with corresponding code examples. 1. Implementation of paging function The paging function mainly obtains a specific range of data by querying the database, and displays the data according to the specified page number and display quantity per page. The following are the steps to implement paging functionality

See all articles