php的4种常用运行方式详解


Posted in PHP onDecember 22, 2016

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 相关文章推荐
用Socket发送电子邮件(利用需要验证的SMTP服务器)
Oct 09 PHP
php 什么是PEAR?(第三篇)
Mar 19 PHP
php Undefined index的问题
Jun 01 PHP
解析PayPal支付接口的PHP开发方式
Nov 28 PHP
解析array splice的移除数组中指定键的值,返回一个新的数组
Jul 02 PHP
php文件上传的例子及参数详解
Dec 12 PHP
PHP网页游戏学习之Xnova(ogame)源码解读(九)
Jun 24 PHP
PHP以mysqli方式连接类完整代码实例
Jul 15 PHP
解析WordPress中函数钩子hook的作用及基本用法
Dec 22 PHP
php中的单引号、双引号和转义字符详解
Feb 16 PHP
解决form中action属性后面?传递参数 获取不到的问题
Jul 21 PHP
php使用ftp实现文件上传与下载功能
Jul 21 PHP
php curl 模拟登录并获取数据实例详解
Dec 22 #PHP
使用PHP连接多种数据库的实现代码(mysql,access,sqlserver,Oracle)
Dec 21 #PHP
Docker配置PHP开发环境教程
Dec 21 #PHP
PHP符合PSR编程规范的实例分享
Dec 21 #PHP
利用PHP生成CSV文件简单示例
Dec 21 #PHP
PHP实现支付宝即时到账功能
Dec 21 #PHP
简单实现PHP留言板功能
Dec 21 #PHP
You might like
Linux下 php7安装redis的方法
2018/11/01 PHP
Yii2框架视图(View)操作及Layout的使用方法分析
2019/05/27 PHP
借用Google的Javascript API Loader来加速你的网站
2009/01/28 Javascript
理解Javascript_14_函数形式参数与arguments
2010/10/20 Javascript
jquery 鼠标滑动显示详情应用示例
2014/01/24 Javascript
Nodejs sublime text 3安装与配置
2014/06/19 NodeJs
JQuery中Ajax()的data参数类型实例分析
2015/12/15 Javascript
自己动手制作基于jQuery的Web页面加载进度条插件
2016/06/03 Javascript
Javascript 数组去重的方法(四种)详解及实例代码
2016/11/24 Javascript
node.js中使用Export和Import的方法
2017/09/18 Javascript
js 将canvas生成图片保存,或直接保存一张图片的实现方法
2018/01/02 Javascript
Angular angular-file-upload文件上传的示例代码
2018/08/23 Javascript
使用rollup打包JS的方法步骤
2018/12/05 Javascript
Vue入门之数量加减运算操作示例
2018/12/11 Javascript
利用es6 new.target来对模拟抽象类的方法
2019/05/10 Javascript
react 中父组件与子组件双向绑定问题
2019/05/20 Javascript
vue+导航锚点联动-滚动监听和点击平滑滚动跳转实例
2019/11/13 Javascript
Vuejs中的watch实例详解(监听者)
2020/01/05 Javascript
node.js 微信开发之定时获取access_token
2020/02/07 Javascript
vue props default Array或是Object的正确写法说明
2020/07/30 Javascript
python和shell实现的校验IP地址合法性脚本分享
2014/10/23 Python
python使用socket进行简单网络连接的方法
2015/04/29 Python
python判断一个集合是否包含了另外一个集合中所有项的方法
2015/06/30 Python
Python基于回溯法子集树模板解决选排问题示例
2017/09/07 Python
Python中常见的异常总结
2018/02/20 Python
TensorFlow实现简单卷积神经网络
2018/05/24 Python
python实现根据指定字符截取对应的行的内容方法
2018/10/23 Python
Python 连接 MySQL 的几种方法
2020/09/09 Python
日本无添加化妆品:HABA
2016/08/18 全球购物
Lentiamo荷兰:在线订购隐形眼镜、隐形眼镜液和太阳镜
2019/10/25 全球购物
特色蛋糕店创业计划书
2014/01/28 职场文书
合作经营协议书范本
2014/04/17 职场文书
村委会换届选举方案
2014/05/03 职场文书
节水口号标语
2014/06/19 职场文书
值班管理制度范本
2015/08/06 职场文书
Springboot-cli 开发脚手架,权限认证,附demo演示
2022/04/28 Java/Android