Four common running modes in php

Keywords: PHP Web Server Apache PDO

This article introduces four common ways of running php: CGI, FastCGI, Apache 2 handler and CLI. There is a certain reference value, friends in need can refer to it, hope to help you.

There are four common running modes of PHP: CGI, FastCGI, Apache 2 handler and CLI.


CGI is the common gateway interface, which is a program. Generally speaking, CGI is like a bridge, connecting the WEB page with the execution program in the WEB server, passing the instructions received by HTML to the execution program of the server, and then returning the results of the execution program of the server to the HTML page. CGI has excellent cross platform performance and can be implemented on almost any operating system.

In cgi mode, when a connection request (user request) is encountered, a child process of cgi should be created first, a cgi process should be activated, then the request should be processed, and then the child process should be ended. This is the fork and execute mode. Therefore, the main reason for the low performance of cgi is that the number of cgi subprocesses will be as many as the number of connection requests of cgi servers. When the number of user requests is very large, it will occupy a lot of system resources such as memory, CPU time and so on, resulting in low efficiency.


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

Load FastCGI Process Manager (IIS ISAPI or Apache Module) when Web Server starts. The FastCGI process manager initializes itself, starts multiple CGI interpreter processes (see multiple PHP CGI) 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 the CGI environment variables and standard input to the FastCGI subprocess PHP CGI.

The FastCGI subprocess returns standard output and error information from the same connection to the Web Server after processing. When the FastCGI subprocess closes the connection, the request is processed. The FastCGI subprocess then waits and processes the next connection from the FastCGI Process Manager (running in the Web Server). In CGI mode, PHP CGI exits here.

In this case, you can imagine how slow CGI is usually. Every Web request PHP must re parse php.ini, reload all extensions, and re initialize all data structures. With FastCGI, all this happens only once when the process starts. An additional benefit is that persistent database connection works.


As an Apache module, after the system is started, the Apache server generates multiple process copies in advance and resides in memory. Once a request appears, it immediately uses these spare subprocesses for processing, so there is no delay caused by the generation of subprocesses. These server copies do not exit immediately after processing an HTTP request, but stay in the computer waiting for the next request. The response to the request of the client browser is faster and the performance is higher.


cli is the command-line running mode of php. The running commands on the cli side are sometimes very useful. Here are a few summary:

View php version information

eric:~ youngeric$ php -v
PHP 5.5.38 (cli) (built: Oct  1 2016 23:03:00) 
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies 


View the current php extension

eric:~ youngeric$ php -m


[PHP Modules]











View php.ini configuration information (equivalent to using the phpinfo() function)


eric:~ youngeric$ php -ini



PHP Version => 5.5.38


System => Darwin eric.local 16.1.0 Darwin Kernel Version 16.1.0: Wed Oct 19 20:31:56 PDT 2016; root:xnu-3789.21.4~4/RELEASE_X86_64 x86_64

Build Date => Oct 1 2016 23:01:51

Configure Command => './configure' '--prefix=/usr/local/Cellar/php55/5.5.38_11' '--localstatedir=/usr/local/var' '--sysconfdir=/usr/local/etc/php/5.5' '--with-config-file-path=/usr/local/etc/php/5.5' '--with-config-file-scan-dir=/usr/local/etc/php/5.5/conf.d' '--mandir=/usr/local/Cellar/php55/5.5.38_11/share/man' '--enable-bcmath' '--enable-calendar' '--enable-dba' '--enable-exif' '--enable-ftp' '--enable-gd-native-ttf' '--enable-mbregex' '--enable-mbstring' '--enable-shmop' '--enable-soap' '--enable-sockets' '--enable-sysvmsg' '--enable-sysvsem' '--enable-sysvshm' '--enable-wddx' '--enable-zip' '--with-freetype-dir=/usr/local/opt/freetype' '--with-gd' '--with-gettext=/usr/local/opt/gettext' '--with-iconv-dir=/usr' '--with-icu-dir=/usr/local/opt/icu4c' '--with-jpeg-dir=/usr/local/opt/jpeg' '--with-kerberos=/usr' '--with-libedit' '--with-mhash' '--with-ndbm=/usr' '--with-png-dir=/usr/local/opt/libpng' '--with-xmlrpc' '--with-zlib=/usr' '--with-readline=/usr/local/opt/readline' '--without-gmp' '--without-snmp' '--with-libxml-dir=/usr/local/opt/libxml2' '--with-pdo-odbc=unixODBC,/usr/local/opt/unixodbc' '--with-unixODBC=/usr/local/opt/unixodbc' '--with-bz2=/usr' '--with-openssl=/usr/local/opt/openssl' '--enable-fpm' '--with-fpm-user=_www' '--with-fpm-group=_www' '--with-curl' '--with-xsl=/usr' '--with-ldap' '--with-ldap-sasl=/usr' '--with-mysql-sock=/tmp/mysql.sock' '--with-mysqli=mysqlnd' '--with-mysql=mysqlnd' '--with-pdo-mysql=mysqlnd' '--disable-opcache' '--enable-pcntl' '--without-pear' '--enable-dtrace' '--disable-phpdbg' '--enable-zend-signals'

Server API => Command Line Interface

Virtual Directory Support => disabled

Configuration File (php.ini) Path => /usr/local/etc/php/5.5

Loaded Configuration File => /usr/local/etc/php/5.5/php.ini

Scan this dir for additional .ini files => /usr/local/etc/php/5.5/conf.d



View function information

eric:~ youngeric$ php --rf date
Function [ <internal:date> function date ] {
 - Parameters [2] {
 Parameter #0 [ <required> $format ]
 Parameter #1 [ <optional> $timestamp ]


View class information

eric:~ youngeric$ php --rc pdo
Class [ <internal:PDO> class PDO ] {
 - Constants [89] {
 Constant [ integer PARAM_BOOL ] { 5 }
 Constant [ integer PARAM_NULL ] { 0 }
 Constant [ integer PARAM_INT ] { 1 }
 Constant [ integer PARAM_STR ] { 2 }
 Constant [ integer PARAM_LOB ] { 3 }
 Constant [ integer PARAM_STMT ] { 4 }
 Constant [ integer PARAM_INPUT_OUTPUT ] { 2147483648 }


Check php code

eric:~ youngeric$ php -l jiance.php
PHP Parse error: syntax error, unexpected end of file, expecting ',' or ';' in jiance.php on line 3
Errors parsing jiance.php


As the best language in the world, php even has the function of server built in (is it shocked or not).

eric:Desktop youngeric$ php -S
PHP 5.5.38 Development Server started at Thu Dec 22 09:44:20 2016
Listening on
Document root is /Users/youngeric/Desktop
Press Ctrl-C to quit.
[Thu Dec 22 09:44:29 2016] [404]: / - No such file or directory
[Thu Dec 22 09:44:29 2016] [404]: /favicon.ico - No such file or directory 


These are the details of the four commonly used running modes in php

For more information, please visit:

Tencent T3-T4 standard boutique PHP architect tutorial directory, as long as you read it to ensure a higher salary (continuous update)

Posted by ElectricRain on Mon, 20 Apr 2020 23:57:19 -0700