本篇文章给大家介绍一下php的4种常用运行方式:cgi、fastcgi、apache2handler和cli。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。

php的4种常用运行方式:cgi、fastcgi、apache2handler、cli。

1、cgi

cgi即通用网关接口(common gatewag interface),它是一段程序,通俗的讲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(fastcgi process manager),全称php fastcgi进程管理器进行管理。

web server启动时载入fastcgi进程管理器(iis isapi或apache module)。fastcgi进程管理器自身初始化,启动多个cgi解释器进程(可见多个php-cgi)并等待来自web server的连接。

当客户端请求到达web server时,fastcgi进程管理器选择并连接到一个cgi解释器。web server将cgi环境变量和标准输入发送到fastcgi子进程php-cgi。

fastcgi子进程完成处理后将标准输出和错误信息从同一连接返回web server。当fastcgi子进程关闭连接时,请求便告处理完成。fastcgi子进程接着等待并处理来自fastcgi进程管理器(运行在web server中)的下一个连接。 在cgi模式中,php-cgi在此便退出了。

在上述情况中,你可以想象cgi通常有多慢。每一个web 请求php都必须重新解析php.ini、重新载入全部扩展并重初始化全部数据结构。使用fastcgi,所有这些都只在进程启动时发生一次。一个额外的 好处是,持续数据库连接(persistent database connection)可以工作。

3、apache2handler

php作为apache模块,apache服务器在系统启动后,预先生成多个进程副本驻留在内存中,一旦有请求出 现,就立即使用这些空余的子进程进行处理,这样就不存在生成子进程造成的延迟了。这些服务器副本在处理完一次http请求之后并不立即退出,而是停留在计算机中等待下次请求。对于客户浏览器的请求反应更快,性能较高。

4、cli

cli是php的命令行运行模式,cli端的运行命令有时会很有用,以下总结几个:

查看php版本信息

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 

  

查看当前php的扩展

eric:~ youngeric$ php -m

  

[php modules]

bcmath

bz2

calendar

core

ctype

curl

date

......

  

 

查看php.ini配置信息(相当于使用phpinfo()函数)

 

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

......

  

查看函数信息

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

  

查看类信息

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 }
 ...... 

  

检测php代码

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 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 

  

以上就是php中常用的4种运行方式的详细内容

更多学习内容请访问:

腾讯t3-t4标准精品php架构师教程目录大全,只要你看完保证薪资上升一个台阶(持续更新)