search
HomeBackend DevelopmentPHP TutorialAnalysis of cgi, fastcgi, php-cgi, php-fpm

Analysis of cgi, fastcgi, php-cgi, php-fpm

Apr 20, 2018 am 10:52 AM
fastcgiphp-cgiphp-fpm

The content of this article is about the analysis of cgi, fastcgi, php-cgi, and php-fpm. It has a certain reference value. Now I share it with you. Friends in need can refer to it

Definition


First of all, what is CGI for? CGI is to ensure that the data passed by the web server is in a standard format, which is convenient for writers of CGI programs.

The web server (such as nginx) is just a distributor of content. For example, if you request /index.html, the web server will find this file in the file system and send it to the browser. What is distributed here is static data. Okay, if the request now is /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 it, then it will simply process the request and hand it over to PHP parser. 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 header must be present. Well, CGI is the protocol that stipulates what data is to be transmitted and in what format to be passed to the backend for processing the request. Think carefully about where the users you use in your PHP code come from.

When the web server receives the request /index.php, it will start the corresponding CGI program, which is the PHP parser. Next, the PHP parser 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. The web server then returns the results to the browser.

Okay, CGI is a protocol and has nothing to do with processes or anything like that. So what is fastcgi? Fastcgi is used to improve the performance of CGI programs.

Improve performance, so what are the performance problems of CGI programs? "The PHP parser will parse the php.ini file and initialize the execution environment", that's it. Standard CGI will perform these steps for each request (don't be tired! Starting the process is very tiring!), so the time to process each time will be relatively long. This is obviously unreasonable! So how does Fastcgi do it? First, Fastcgi will start a master, parse the configuration file, initialize the execution environment, and then start multiple workers. When a request comes in, the master passes it to a worker and can immediately accept the next request. This avoids duplication of work and is naturally highly efficient. And when there are not enough workers, the master can pre-start several workers according to the configuration and wait; of course, when there are too many idle workers, some will be stopped, which improves performance and saves resources. This is fastcgi's process management.


cgi

  • Universal gateway interface, the interface standard between external programs and web servers, is the process of transferring information between cgi programs and web servers

  • Every request will generate a cgi process. After the cgi program is executed, the process exits

  • Independent of the server and independent programming language

FastCgi

  • FastCgi is like a resident Cgi, it can always execute this, as long as it is activated, no need It forks once every time and also supports distributed computing, that is, the FastCgi program can be executed on a host other than the website server and accept requests from other website servers

  • can handle multiple requests at the same time

  • Long-term memory usage

php-cgi

  • php official After the built-in FastCGI process manager

  • php.ini is modified, you must kill php-cgi and then start php.ini for it to take effect. Cannot restart smoothly

  • Memory cannot be allocated dynamically

php-fpm

  • The unofficial fastCgi process manager was officially included starting from php5.4. When compiling php, you only need –enable-fpm to enable php-fpm

  • Can smoothly restart php

  • Dynamic scheduling process




Nginx is only responsible for reverse proxy/request forwarding and is not responsible for managing the php-cgi process. Therefore, Nginx is generally used with php-fpm, which can manage the working process (child process) by itself. .
It should be noted that php-fpm is an independent SAPI, which does not manage php-cgi. In other words, php-fpm has nothing to do with php-cgi. php-fpm has a built-in php interpreter. php-fpm The child process is forked by itself and will not call php-cgi. If you delete php-cgi in the system, it will not affect the normal operation of the php-fpm service.

php-fpm is in pm = static configuration, the working process resides in the background, that is, if you configure 5 working processes pm.max_children = 5, then when the php-fpm service starts, it will automatically fork out 5 child processes and reside in the background, and will not be requested. Exit after the processing is completed, and will not exit when idle. If you use a database persistent connection in the php script, these 5 worker processes will also establish and maintain 5 persistent connections to the database to achieve processing of multiple Reuse database connection resources when making requests to avoid establishing/releasing a database connection for each request. Persistent connections can also automatically reconnect after timeout, which is completely transparent to the script in php-fpm. The script only needs to be started when Just specify to use persistent connection.

php-fpm under the pm = dynamic configuration, the working process [part] is resident in the background, that is, a certain number of resident processes are maintained. When the service is busy, more processes are forked, and some processes are automatically shut down when the service is idle. Return memory resources to the operating system. Virtual host providers should prefer this method.

In short, the PHP-FPM operating mode is similar to Apache's prefork MPM, a static and dynamic multi-process network Service.




##php-cgi is an early fastcgi manager produced by PHP official and is not supported For smooth restart, if you change php.ini, you must kill the original php-cgi and restart it to take effect; dynamic worker scheduling is not supported, and you can only specify how many workers to start at the beginning.

php-fpm is a fastcgi process manager added since 5.3.3. It has added a dynamic scheduling function, which can dynamically increase or decrease the number of worker processes according to changes in request pressure; it supports the reload command to allow the worker process to complete Restarts after the current request and applies the new php.ini configuration.





#php54 was a relationship before, Another relationship after php54.


Before php54, php-fpm (third-party compilation) is the manager, php-cgi is the interpreter

After php54, php-fpm (official Comes with), master and pool modes. php-fpm and php-cgi have nothing to do with each other. php-fpm is both an interpreter and a manager

Reference website https://www.zhihu.com/question/55835080



https://segmentfault .com/q/1010000000256516


Related recommendations:

Illustration of the relationship between CGI, FastCGI and PHP-FPM

linux view Directory where nginx, apache, php, php-fpm, mysql and configuration items are located

The above is the detailed content of Analysis of cgi, fastcgi, php-cgi, php-fpm. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

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

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.