Four common php running methods _ PHP Tutorial

Source: Internet
Author: User
Tags sapi
Four common php running methods. Four common php running methods this article mainly introduces four common php running methods. This article describes CGI, FastCGI, APACHE2HANDLER, CLI, and other four running methods, for more information, see four common php running methods.

This article mainly introduces four common php running methods. This article describes CGI, FastCGI, APACHE2HANDLER, CLI, and other four running methods. For more information, see

SAPI: Server Application Programming Interface Server Application Programming port. It is the interface for php to interact with other applications. There are many ways to execute php scripts, either through the web server or directly under the command line, or embedded in other programs. SAPI provides an interface for external communication. common SAPIs include cgi, fast-cgi, cli, and Apache module dll.

  1. CGI

CGI is the common gatewag interface. it is a program. Generally speaking, CGI is like a bridge, which connects the webpage to the execution program on the WEB server, it passes the commands received by HTML to the execution program on the server, and then returns the results of the execution program on the server to the HTML page. CGI provides excellent cross-platform performance and can be implemented on almost any operating system.

In CGI mode, a connection request (User request) must first create a cgi sub-process, activate a CGI process, process the request, and end the sub-process after processing. This is the fork-and-execute mode. As a result, the number of connection requests to servers in the cgi mode is limited to the number of cgi sub-processes. repeated loading of sub-processes is the main cause of low cgi performance. When the number of user requests is very large, a large amount of system resources such as internal storage and CPU time will be squeezed out, resulting in low efficiency.

  2. FastCGI

Fast-cgi is an upgraded version of cgi. FastCGI is like a long-live CGI. it can be executed all the time, it does not take time to fork once every time. PHP uses PHP-FPM (FastCGI Process Manager), the full name of PHP FastCGI Process Manager for management.

When the Web Server starts, load the FastCGI Process Manager (iis isapi or Apache Module ). FastCGI Process Manager initializes itself, starts multiple CGI interpreter processes (multiple php-cgi are visible), and waits for a connection 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 sub-process php-cgi.

After the FastCGI sub-process completes processing, the standard output and error messages are returned from the same connection to the Web Server. When the FastCGI sub-process closes the connection, the request processing is complete. The FastCGI sub-process then waits for and processes the next connection from the FastCGI Process Manager (running on the Web Server. In CGI mode, php-cgi exits here.

In the above cases, you can imagine how slow CGI is. For each Web request, PHP must re-parse php. ini, re-load all extensions, and initialize all data structures. With FastCGI, all of these occur only once when the process starts. An additional benefit is that Persistent database connection can work.

  3. APACHE2HANDLER
PHP is used as the Apache Module. after the Apache server is started, multiple process replicas are generated in advance and reside in the memory. once you find them, use these idle sub-processes for processing, in this way, there is no latency caused by the generation of sub-processes. These server copies do not exit immediately after processing an HTTP request, but are stuck in the computer waiting for the next request. The response to client browser requests is faster and the performance is high.

  4. CLI

Cli is the command line running mode of php. it is often used, but it may not be noticed (for example: in linux, we often use "php-m" to find out which extensions are installed in PHP command line running mode;

Ghost This article mainly introduces four common php running modes. This article describes CGI, FastCGI, APACHE2HANDLER, CLI, and other four running modes. if you need them, refer...

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.