Detailed explanation of 4 common running methods of PHP

墨辰丷
Release: 2023-03-28 10:32:01
Original
4592 people have browsed it

This article mainly introduces the four common operating methods of php, CGI, FastCGI, APACHE2HANDLER and CLI, which has certain reference value. Interested friends can refer to the 4 types of

php Commonly used operating methods: CGI, FastCGI, APACHE2HANDLER, CLI.

1. CGI

CGI is the common gateway interface. It is a program. In layman’s terms, CGI is like a bridge. It connects the web page to the execution program in the WEB server. It passes the instructions received by 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. It can It is 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 subprocess 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 case, 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 to reside in the memory. Once a request appears, it will be used immediately. These idle sub-processes are processed so that there is no delay caused by spawning 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
Copy after login

View the current php extension

eric:~ youngeric$ php -m

[PHP Modules]
bcmath
bz2
calendar
Core
ctype
curl
date
......
Copy after login

View php.ini configuration information (equivalent to using the 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
......
Copy after login

View function information

eric:~ youngeric$ php --rf date

Function [ <internal:date> function date ] {
 - Parameters [2] {
  Parameter #0 [ <required> $format ]
  Parameter #1 [ <optional> $timestamp ]
 }
}
Copy after login

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 }
 ......
Copy after login

Detect php code

eric:~ youngeric$ php -l jiance.php

PHP Parse error: syntax error, unexpected end of file, expecting &#39;,&#39; or &#39;;&#39; in jiance.php on line 3
Errors parsing jiance.php
Copy after login

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
Copy after login

The above is the entire content of this article, I hope it will be helpful to everyone's study.


Related recommendations:

PHPFolder copying, deletion, size viewing, etc. based on iteration

PHPWhat are the ways to implement multi-dimensional array sorting algorithm

PHPRecursive implementation Copy, delete, view size, etc. of folders

The above is the detailed content of Detailed explanation of 4 common running methods of PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!