PHP在实际应用中主要有四种常见的运行方式:CGI(通用网关接口)、FastCGI、APACHE2HANDLER(作为Apache模块加载)以及CLI(命令行界面)。
CGI运行模式原理与局限
1、CGI模式
CGI全称为Common Gateway Interface(通用网关接口),它本质上是一段执行程序。通俗来说,CGI充当了网页与Web服务器后端程序之间的桥梁:它接收HTML页面传来的指令,将其传递给服务器的执行引擎,并将处理结果返回给前端页面。CGI具有极佳的跨平台兼容性,几乎可以在任何操作系统上运行。
在CGI模式下,每当有用户请求到达时,服务器都会创建一个新的CGI子进程来激活处理流程,处理结束后立即终止该子进程。这种机制被称为“fork-and-execute”模式。因此,并发连接数直接决定了子进程的数量。由于子进程需要反复创建和加载,当用户请求量巨大时,会大量占用内存和CPU时间等资源,导致服务器性能显著下降。
FastCGI与PHP-FPM机制
2、FastCGI模式
FastCGI是CGI的升级版,采用常驻型(long-live)设计。一旦激活,进程将保持运行状态,无需像CGI那样每次请求都重新fork进程。PHP通常通过PHP-FPM(FastCGI Process Manager,即PHP FastCGI进程管理器)来进行管理。Web Server启动时会载入FastCGI进程管理器(如IIS ISAPI或Apache Module),该管理器自身初始化后,会启动多个CGI解释器进程(例如多个php-cgi实例),并持续等待来自Web Server的连接。
当客户端请求抵达Web Server时,FastCGI进程管理器会选择一个空闲的CGI解释器进行连接。随后,Web Server将该CGI的环境变量和标准输入数据发送给对应的FastCGI子进程(php-cgi)。
FastCGI子进程完成处理后,通过同一连接将标准输出和错误信息返回给Web Server。一旦连接关闭,当前请求即告完成。紧接着,该子进程会继续等待并处理由FastCGI进程管理器发来的下一个连接。相比之下,在CGI模式中,php-cgi在处理完一个请求后就会退出。
由此可见,CGI模式的效率较低。因为每个Web请求都需要PHP重新解析php.ini文件、重新加载所有扩展并重初始化数据结构。而在FastCGI模式下,这些操作仅在进程启动时执行一次。此外,FastCGI还支持持久数据库连接(Persistent database connection),进一步提升了性能。
Apache模块加载方式
3、APACHE2HANDLER PHP作为Apache模块,Apache服务器在系统启动后,预先生成多个进程副本驻留在内存中,一旦有请求出现,就立即使用这些空余的子进程进行处理,这样就不存在生成子进程造成的延迟了。这些服务器副本在处理完一次HTTP请求之后并不立即退出,而是停留在计算机中等待下次请求。对于客户浏览器的请求反应更快,性能较高。
CLI命令行常用命令汇总
在这种模式下,PHP作为Apache的一个模块存在。Apache服务器在系统启动后,会在内存中预先生成多个进程副本。当请求出现时,服务器直接利用这些空闲的子进程进行处理,从而消除了创建子进程带来的延迟。处理完一次HTTP请求后,这些副本不会立即退出,而是驻留在内存中以备下次请求使用。这使得服务器对客户浏览器的响应速度更快,整体性能较高。
4、CLI模式
CLI是PHP的命令行运行模式。在该模式下,许多命令对开发和维护非常有用,以下总结了几项常用操作:
eric:~ youngeric$ php -v
PHP 5.5.38 (cli) (built: Oct 1 2026 23:03:00)
Copyright (c) 1997-2026 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2026 Zend Technologies
查看PHP版本信息
eric:~ youngeric$ php -m
[PHP Modules]
bcmath
bz2
calendar
Core
ctype
curl
date
......
查看当前PHP已加载的扩展模块
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 2026; root:xnu-3789.21.4~4/RELEASE_X86_64 x86_64
Build Date => Oct 1 2026 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
......
查看php.ini配置信息(其效果等同于执行phpinfo()函数)
eric:~ youngeric$ php --rf date
Function [ function date ] {
- Parameters [2] {
Parameter #0 [ $format ]
Parameter #1 [ $timestamp ]
}
}
查看特定函数的详细信息
eric:~ youngeric$ php --rc pdo
Class [ 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 }
......
查看特定类的定义信息
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
检测PHP代码语法是否正确
eric:Desktop youngeric$ php -S 127.0.0.1:8080
PHP 5.5.38 Development Server started at Thu Dec 22 09:44:20 2026
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 2026] 127.0.0.1:52988 [404]: / - No such file or directory
[Thu Dec 22 09:44:29 2026] 127.0.0.1:52989 [404]: /favicon.ico - No such file or directory
作为被广泛讨论的语言,PHP甚至内置了简单的开发服务器功能,允许开发者在本地快速搭建测试环境。