A brief discussion on Cgi,fast-cgi,cli,isapi,apache mode of PHP five operation mode

Source: Internet
Author: User

Original address: http://www.phpernote.com/news/723.html

PHP development should be aware of the concept of PHP running mode, this article will share with you about PHP is now more common five operating modes: including CGI, fast-cgi, CLI, ISAPI, Apache module DLL, The following author of the five operating modes do some of their own analysis, there is a wrong place, please leave a message.

  CGI run mode

CGI is the Universal Gateway Interface (Common Gateway Interface), it is a program, the popular CGI is like a bridge, the Web page and the Web server to connect the execution program, it takes HTML received instructions to the server's execution program, The results of the server execution program are then returned to the HTML page. CGI has excellent cross-platform performance and can be implemented on almost any operating system.

The CGI method encounters a connection request (user request) to create a CGI subprocess, activate a CGI process, then process the request, and end the subprocess after processing. This is the Fork-and-execute mode. So how many CGI sub-processes are there for the CGI server, and the sub-process loading is the main reason for the poor CGI performance. will be when the user requests a very long time, will be a lot of crowding out the system resources such as memory, CPU times, resulting in low performance.

this pattern needs to be commented out LoadModule php5_module modules/libphp5. So this line. If you do not annotate this line go straight to handler mode. This is the module mode. And then in httpd. conf Add action:action application/x-httpd-php/cgi-bin/php-CGI if /cgi-bin/is not found in the php-cgi directory. can be self-contained in the PHP bin inside the CP one. Then restart Apache, and then open the test page to discover that the server API becomes: cgi/fastcgi. Description successfully switched to CGI mode.

  FAST-CGI operating mode

FAST-CGI is an upgraded version of CGI, fastcgi is like a resident (long-live) CGI, which can be executed all the time, so long as it is activated, it does not have to take a moment to fork it every time. PHP uses PHP-FPM (FastCGI process Manager), the full name of the PHP FastCGI process Manager to manage.

How the FastCGI works

1. Load the FASTCGI Process Manager (IIS ISAPI or Apache Module) when Web server starts

2, the FASTCGI process manager itself initializes, starts multiple CGI interpreter processes (visible multiple php-cgi) and waits for a connection from the Web server.

3. When a client request arrives at Web server, the FASTCGI process manager selects and connects to a CGI interpreter. WEB server sends CGI environment variables and standard input to the FASTCGI child process php-cgi.

4. After the fastcgi process finishes processing, the standard output and error information are returned from the same connection to the Web Server. When the fastcgi child process closes the connection, the request is processed to completion. The fastcgi child process then waits and processes the next connection from the FASTCGI process Manager (running in Web server). In CGI mode, php-cgi exits here.

In the above scenario, you can imagine how slow CGI is usually. Every Web request PHP must re-parse php.ini, reload all extensions, and initialize all data structures. With fastcgi, all of this occurs only once when the process is started. An additional benefit is that the persistent database connection (persistent connection) can work.

First, add the FastCGI module to httpd. conf configuration file: LoadModule fastcgi_module modules/mod_fastcgi. So this pattern annotation does not annotate loadmodule php5_module modules/libphp5. so this does not seem to matter, as long as the following modules are configured <ifmodule fastcgi_module>   /usr/local/apache/cgi-bin/php-cgi- Processes   AddType application/x-httpd-php.  PHP   AddHandler php-fastcgi. PHP   Action php-fastcgi/cgi-bin/php-CGI</IfModule> will automatically go to fastcgi mode. Then restart Apache, this time with PS Aux|grep PHP will find a lot of php-cgi process is running. Description Configuration takes effect

  CLI Run mode

The CLI is the command-line run mode of PHP, you will often use it, but it may not be noticed (for example: we often use "php-m" in Linux to find PHP installed those extensions is the PHP command line running mode, interested students can input php-h to delve into the operation mode)

  ISAPI Run mode

ISAPI, the Internet Server application Program Interface, is a set of Internet service-oriented API interfaces provided by Microsoft, an ISAPI DLL that can be long-standing in memory after being activated by a user request. Waiting for another user request, you can also set up a DLL in a number of user request handler functions, in addition, the ISAPI DLL application and the WWW server in the same process, the efficiency is significantly higher than the CGI. (due to the exclusivity of Microsoft, only running in the Windows environment)

  Apache2handler

PHP as an Apache module, the Apache server after the system startup, pre-generated multiple copies of the process resides in memory, once a request appears, immediately use these spare child processes to process, so there is no generation of child process caused by the delay. These server replicas do not exit immediately after processing an HTTP request, but instead stay on the computer waiting for the next request. Requests for customer browsers respond faster and have higher performance.

The DLL run mode of Apache module

This mode of operation is often used by Apache servers in a Windows environment, and in a modular (DLL), PHP is started and run with a Web server. (It is an extension of Apache based on CGI, which accelerates the efficiency of PHP operation)

For each of the above operating mode has its own advantages and disadvantages, there is no absolute good and bad, mainly to see what the environment under what kind of needs.

This mode is the simplest in HTTP. add loadmodule php5_module modules/libphp5 in conf. So you can. Then in <ifmodule mime_module>addtype application/x-httpd-php. phpaddtype applicaiton/x-httpd-php-source. Phps

A brief discussion on Cgi,fast-cgi,cli,isapi,apache mode of PHP five operation mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.