This article will introduce to you the four commonly used running methods of PHP: CGI, FastCGI, APACHE2HANDLER and CLI. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.
There are 4 common operating modes of PHP: CGI, FastCGI, APACHE2HANDLER, and CLI.
1. CGI
CGI is the common gateway interface. It is a program. In layman’s terms, CGI is like a bridge that connects web pages and The execution program in the WEB server is connected, it passes the instructions received by the HTML to the server's execution program, and then returns the results of the server's execution program to the HTML page. CGI is extremely cross-platform and can be implemented on almost any operating system.
When the CGI method encounters a connection request (user request), it must first create a cgi sub-process, activate a CGI process, then process the request, and end the sub-process after processing. This is the fork-and-execute pattern. Therefore, a server using CGI will have as many CGI sub-processes as there are connection requests. Repeated loading of sub-processes is the main reason for low CGI performance. When the number of user requests is very large, a large amount of system resources such as memory, CPU time, etc. will be occupied, resulting in low performance.
2. FastCGI
fast-cgi is an upgraded version of cgi. FastCGI is like a long-live CGI that can be executed all the time. , as long as it is activated, it will not take time to fork every time. PHP uses PHP-FPM (FastCGI Process Manager), the full name of PHP FastCGI Process Manager, for management.
Load the FastCGI process manager (IIS ISAPI or Apache Module) when the Web Server starts. The FastCGI process manager initializes itself, starts multiple CGI interpreter processes (visible multiple php-cgi) and waits for connections from the Web Server.
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.
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 situation, 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 benefit is that persistent database connections work.
3. APACHE2HANDLER
PHP is an Apache module. After the Apache server starts the system, it pre-generates multiple process copies and resides in the memory. Once a request occurs, These idle sub-processes are used immediately for processing, so that there is no delay caused by generating sub-processes. 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 client browser requests is faster and the performance is higher.
4. CLI
cli is the command line running mode of php. The running commands on the cli side are sometimes very useful. Here are a few summaries:
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] bcmath bz2 calendar Core ctype curl date ......
View php.ini configuration information (equivalent to using phpinfo( ) function)
eric:~ youngeric$ php -ini phpinfo() 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 } ......
Detect 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 built-in server functions (are you shocked?).
eric:Desktop youngeric$ php -S 127.0.0.1:8080 PHP 5.5.38 Development Server started at Thu Dec 22 09:44:20 2016 Listening on http://127.0.0.1:8080 Document root is /Users/youngeric/Desktop Press Ctrl-C to quit. [Thu Dec 22 09:44:29 2016] 127.0.0.1:52988 [404]: / - No such file or directory [Thu Dec 22 09:44:29 2016] 127.0.0.1:52989 [404]: /favicon.ico - No such file or directory
Recommended learning: PHP video tutorial
The above is the detailed content of 4 commonly used running methods in php. For more information, please follow other related articles on the PHP Chinese website!