php的4种常用运行方式详解
php的4种常用运行方式:CGI、FastCGI、APACHE2HANDLER、CLI。
1、CGI
CGI即通用网关接口(commongatewaginterface),它是一段程序,通俗的讲CGI就象是一座桥,把网页和WEB服务器中的执行程序连接起来,它把HTML接收的指令传递给服务器的执行程序,再把服务器执行程序的结果返还给HTML页。CGI的跨平台性能极佳,几乎可以在任何操作系统上实现。
CGI方式在遇到连接请求(用户请求)先要创建cgi的子进程,激活一个CGI进程,然后处理请求,处理完后结束这个子进程。这就是fork-and-execute模式。所以用cgi方式的服务器有多少连接请求就会有多少cgi子进程,子进程反复加载是cgi性能低下的主要原因。都会当用户请求数量非常多时,会大量挤占系统的资源如内存,CPU时间等,造成效能低下。
2、FastCGI
fast-cgi是cgi的升级版本,FastCGI像是一个常驻(long-live)型的CGI,它可以一直执行着,只要激活后,不会每次都要花费时间去fork一次。PHP使用PHP-FPM(FastCGIProcessManager),全称PHPFastCGI进程管理器进行管理。
WebServer启动时载入FastCGI进程管理器(IISISAPI或ApacheModule)。FastCGI进程管理器自身初始化,启动多个CGI解释器进程(可见多个php-cgi)并等待来自WebServer的连接。
当客户端请求到达WebServer时,FastCGI进程管理器选择并连接到一个CGI解释器。Webserver将CGI环境变量和标准输入发送到FastCGI子进程php-cgi。
FastCGI子进程完成处理后将标准输出和错误信息从同一连接返回WebServer。当FastCGI子进程关闭连接时,请求便告处理完成。FastCGI子进程接着等待并处理来自FastCGI进程管理器(运行在WebServer中)的下一个连接。在CGI模式中,php-cgi在此便退出了。
在上述情况中,你可以想象CGI通常有多慢。每一个Web请求PHP都必须重新解析php.ini、重新载入全部扩展并重初始化全部数据结构。使用FastCGI,所有这些都只在进程启动时发生一次。一个额外的好处是,持续数据库连接(Persistentdatabaseconnection)可以工作。
3、APACHE2HANDLER
PHP作为Apache模块,Apache服务器在系统启动后,预先生成多个进程副本驻留在内存中,一旦有请求出现,就立即使用这些空余的子进程进行处理,这样就不存在生成子进程造成的延迟了。这些服务器副本在处理完一次HTTP请求之后并不立即退出,而是停留在计算机中等待下次请求。对于客户浏览器的请求反应更快,性能较高。
4、CLI
cli是php的命令行运行模式,cli端的运行命令有时会很有用,以下总结几个:
查看php版本信息
eric:~youngeric$php-v PHP5.5.38(cli)(built:Oct1201623:03:00) Copyright(c)1997-2015ThePHPGroup ZendEnginev2.5.0,Copyright(c)1998-2015ZendTechnologies
查看当前php的扩展
eric:~youngeric$php-m [PHPModules] bcmath bz2 calendar Core ctype curl date ......
查看php.ini配置信息(相当于使用phpinfo()函数)
eric:~youngeric$php-ini phpinfo() PHPVersion=>5.5.38 System=>Darwineric.local16.1.0DarwinKernelVersion16.1.0:WedOct1920:31:56PDT2016;root:xnu-3789.21.4~4/RELEASE_X86_64x86_64 BuildDate=>Oct1201623:01:51 ConfigureCommand=>'./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' ServerAPI=>CommandLineInterface VirtualDirectorySupport=>disabled ConfigurationFile(php.ini)Path=>/usr/local/etc/php/5.5 LoadedConfigurationFile=>/usr/local/etc/php/5.5/php.ini Scanthisdirforadditional.inifiles=>/usr/local/etc/php/5.5/conf.d ......
查看函数信息
eric:~youngeric$php--rfdate Function[<internal:date>functiondate]{ -Parameters[2]{ Parameter#0[<required>$format] Parameter#1[<optional>$timestamp] } }
查看类信息
eric:~youngeric$php--rcpdo Class[<internal:PDO>classPDO]{ -Constants[89]{ Constant[integerPARAM_BOOL]{5} Constant[integerPARAM_NULL]{0} Constant[integerPARAM_INT]{1} Constant[integerPARAM_STR]{2} Constant[integerPARAM_LOB]{3} Constant[integerPARAM_STMT]{4} Constant[integerPARAM_INPUT_OUTPUT]{2147483648} ......
检测php代码
eric:~youngeric$php-ljiance.php PHPParseerror:syntaxerror,unexpectedendoffile,expecting','or';'injiance.phponline3 Errorsparsingjiance.php
作为世界上最好的语言,php甚至还内置了服务器的功能(有没有很震惊的样子)。
eric:Desktopyoungeric$php-S127.0.0.1:8080 PHP5.5.38DevelopmentServerstartedatThuDec2209:44:202016 Listeningonhttp://127.0.0.1:8080 Documentrootis/Users/youngeric/Desktop PressCtrl-Ctoquit. [ThuDec2209:44:292016]127.0.0.1:52988[404]:/-Nosuchfileordirectory [ThuDec2209:44:292016]127.0.0.1:52989[404]:/favicon.ico-Nosuchfileordirectory
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。