PHP运行模式
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">PHP运行模式</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1、运行模式</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">SAPI(Server Application Programming Interface)服务器应用程序编程接口, 即PHP与其他应用交互的接口, PHP脚本要执行有<span style="color: black;">非常多</span>方式,</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">经过</span>Web服务器, <span style="color: black;">或</span>直接在命令行下, <span style="color: black;">亦</span><span style="color: black;">能够</span>嵌入在其他程序中。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">SAPI<span style="color: black;">供给</span>了一个和<span style="color: black;">外边</span>通信的接口,<span style="color: black;">平常</span>的SAPI有:cgi、fast-cgi、cli、apache模块的DLL、isapi</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">关于PHP<span style="color: black;">日前</span>比较<span style="color: black;">平常</span>的五大运行模式:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1)CGI(通用网关接口 / Common Gateway Interface)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2)FastCGI(常驻型CGI / Long-Live CGI)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">3)CLI(命令行运行 / Command Line Interface)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">4)Web模块模式(Apache等Web服务器运行的模式)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">5)ISAPI(Internet Server Application Program Interface)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">备注:在PHP5.3以后, PHP<span style="color: black;">再也不</span>有ISAPI模式, 安装后<span style="color: black;">亦</span><span style="color: black;">再也不</span>有php5isapi.dll这个文件。要在IIS6上<span style="color: black;">运用</span>高版本PHP, 必须安装FastCGI 扩展, <span style="color: black;">而后</span>使IIS6支持FastCGI。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">备注:在PHP5.3以后, PHP<span style="color: black;">再也不</span>有ISAPI模式, 安装后<span style="color: black;">亦</span><span style="color: black;">再也不</span>有php5isapi.dll这个文件。要在IIS6上<span style="color: black;">运用</span>高版本PHP, 必须安装FastCGI 扩展, <span style="color: black;">而后</span>使IIS6支持FastCGI。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1.1 CGI模式</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">CGI即通用网关接口(Common Gateway Interface), 它是一段程序, 通俗的讲CGI就象是一座桥, 把网页和Web服务器中的执行程序连接起来, 它把HTML接收的指令传递给服务器的执行程序, 再把服务器执行程序的结果返还给HTML页。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">CGI 的跨平台性能极佳, 几乎<span style="color: black;">能够</span>在任何操作系统上实现。 CGI<span style="color: black;">已然</span>是比较老的模式了, 这几年都很少用了。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">每有一个用户请求, 都会先要创建CGI的子进程, <span style="color: black;">而后</span>处理请求, 处理完后结束这个子进程, 这<span style="color: black;">便是</span>Fork-And-Execute模式。 当用户请求数量非常多时, 会<span style="color: black;">海量</span>挤占系统的资源如内存, CPU时间等, <span style="color: black;">导致</span>效能低下。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">因此</span>用CGI方式的服务器有多少连接请求就会有多少CGI子进程, 子进程反复加载是CGI性能低下的<span style="color: black;">重点</span><span style="color: black;">原由</span>。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">倘若</span>不想把 PHP 嵌入到服务器端软件(如 Apache)<span style="color: black;">做为</span>一个模块安装的话, <span style="color: black;">能够</span><span style="color: black;">选取</span>以 CGI 的模式安装。<span style="color: black;">或</span>把 PHP 用于<span style="color: black;">区别</span>的 CGI 封装以便为代码创建安全的 chroot 和 setuid 环境。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这般</span><span style="color: black;">每一个</span>客户机请求一个PHP文件, Web服务器就调用php.exe(win下是php.exe,linux是php)去解释这个文件, <span style="color: black;">而后</span>再把解释的结果以网页的形式返回给客户机。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">这种安装方式<span style="color: black;">一般</span>会把 PHP 的可执行文件安装到 web 服务器的 cgi-bin 目录。CERT <span style="color: black;">意见</span>书 CA-96.11 <span style="color: black;">意见</span>不要把任何的解释器放到 cgi-bin 目录。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 这种方式的好处是把Web Server和<span style="color: black;">详细</span>的程序处理独立开来, 结构清晰, 可控性强, <span style="color: black;">同期</span>缺点<span style="color: black;">便是</span><span style="color: black;">倘若</span>在高<span style="color: black;">拜访</span><span style="color: black;">需要</span>的<span style="color: black;">状况</span>下, CGI的进程Fork就会<span style="color: black;">作为</span>很大的服务器<span style="color: black;">包袱</span>,</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">想象一下数百个并发请求<span style="color: black;">引起</span>服务器Fork出数百个进程就明白了。这<span style="color: black;">亦</span>是<span style="color: black;">为何</span>CGI<span style="color: black;">始终</span>背负性能低下, 高资源消耗的恶名的<span style="color: black;">原由</span>。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1.2 FastCGI模式</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">FastCGI是CGI的升级版本, FastCGI像是一个常驻 (long-live)型的 CGI, 它<span style="color: black;">能够</span><span style="color: black;">始终</span>执行着, 只要激活后, 不会每次都要花费时间去 Fork 一次(这是 CGI 最为人诟病的 fork-and-execute 模式)。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">FastCGI是一个可伸缩地、高速地在HTTP server和动态脚本语言间通信的接口。<span style="color: black;">都数</span>流行的HTTP server都支持FastCGI, <span style="color: black;">包含</span>Apache、Nginx和lighttpd等, <span style="color: black;">同期</span>, FastCGI<span style="color: black;">亦</span>被许多脚本语言所支持, 其中就有PHP。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> FastCGI接口方式采用C/S结构, <span style="color: black;">能够</span>将HTTP服务器和脚本解析服务器<span style="color: black;">掰开</span>, <span style="color: black;">同期</span>在脚本解析服务器上<span style="color: black;">起步</span>一个<span style="color: black;">或</span>多个脚本解析<span style="color: black;">保护</span>进程。当HTTP服务器每次遇到动态程序时, <span style="color: black;">能够</span>将其直接交付给FastCGI进程来执行,</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">而后</span>将得到的结果返回给浏览器。这种方式<span style="color: black;">能够</span>让HTTP服务器专一地处理静态请求<span style="color: black;">或</span>将动态脚本服务器的结果返回给客户端, 这在很大程度上<span style="color: black;">加强</span>了<span style="color: black;">全部</span>应用系统的性能。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">【原理】</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1)Web Server<span style="color: black;">起步</span>时载入FastCGI进程管理器(IIS ISAPI或Apache Module);</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2)FastCGI进程管理器<span style="color: black;">自己</span>初始化, <span style="color: black;">起步</span>多个CGI解释器进程 (可见多个php-cgi.exe或php-cig)并等待来自Web Server的连接;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">3)当客户端请求到达Web Server时, FastCGI进程管理器<span style="color: black;">选取</span>并连接到一个CGI解释器。Web server将CGI环境变量和标准输入发送到FastCGI子进程php-cgi;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">4)FastCGI子进程完成处理后将标准输出和错误信息从同一连接返回Web Server。当FastCGI子进程关闭连接时, 请求便告处理完成。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">FastCGI子进程接着等待并处理来自FastCGI进程管理器(运行在 WebServer中)的下一个连接。在正常的CGI模式中, php-cgi.exe<span style="color: black;">这里</span>便退出了。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 在CGI模式中, 你<span style="color: black;">能够</span>想象 CGI<span style="color: black;">一般</span>有多慢。每一个Web请求PHP都必须重新解析php.ini、重新载入<span style="color: black;">所有</span>dll扩展并重初始化<span style="color: black;">所有</span>数据结构。<span style="color: black;">运用</span>FastCGI, 所有这些都<span style="color: black;">仅在</span>进程<span style="color: black;">起步</span>时<span style="color: black;">出现</span>一次。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">一个额外的好处是, <span style="color: black;">连续</span>数据库连接(Persistent database connection)<span style="color: black;">能够</span>工作。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">备注:PHP的FastCGI进程管理器是PHP-FPM(PHP-FastCGI Process Manager)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">【优点】</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1)从稳定性上看, FastCGI是以独立的进程池来运行CGI, 单独一个进程死掉, 系统<span style="color: black;">能够</span>很轻易的丢弃, <span style="color: black;">而后</span>重新分配新的进程来运行<span style="color: black;">规律</span>;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2)从安全性上看, FastCGI支持分布式运算。FastCGI和宿主的Server完全独立, FastCGI怎么down<span style="color: black;">亦</span>不会把Server搞垮;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">3)从性能上看, FastCGI把动态<span style="color: black;">规律</span>的处理从Server中分离出来, 大负荷的IO处理还是留给宿主Server, <span style="color: black;">这般</span>宿主Server<span style="color: black;">能够</span>一心一意作IO, <span style="color: black;">针对</span>一个普通的动态网页<span style="color: black;">来讲</span>, <span style="color: black;">规律</span>处理可能<span style="color: black;">仅有</span>一小部分, <span style="color: black;">海量</span>的是<span style="color: black;">照片</span>等静态。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">【缺点】</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 说完了好处, <span style="color: black;">亦</span><span style="color: black;">来讲</span>说缺点。从我的<span style="color: black;">实质</span><span style="color: black;">运用</span>来看, 用FastCGI模式更适合生产环境的服务器。但<span style="color: black;">针对</span><span style="color: black;">研发</span>用<span style="color: black;">设备</span><span style="color: black;">来讲</span>就不太合适。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">由于</span>当<span style="color: black;">运用</span> Zend Studio调试程序时, <span style="color: black;">因为</span> FastCGI会认为 PHP进程超时, 从而在页面返回 500错误。这一点让人非常恼火, <span style="color: black;">因此</span>我在<span style="color: black;">研发</span><span style="color: black;">设备</span>上还是换回了 ISAPI模式。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">对某些服务器的新版本支持<span style="color: black;">欠好</span>, 对分布式负载<span style="color: black;">平衡</span>没<span style="color: black;">需求</span>的模块化安装<span style="color: black;">是不是</span>是更好的<span style="color: black;">选取</span>。<span style="color: black;">日前</span>的FastCGI和Server沟通还<span style="color: black;">不足</span>智能, 一个FastCGI进程<span style="color: black;">倘若</span>执行时间过长会被当成是死进程杀掉重起,</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这般</span>在处理<span style="color: black;">长期</span>任务的时候很麻烦, <span style="color: black;">这般</span>做<span style="color: black;">亦</span>使得FastCGI<span style="color: black;">没法</span><span style="color: black;">准许</span>联机调试。<span style="color: black;">由于</span>是多进程, <span style="color: black;">因此</span>比CGI多线程消耗<span style="color: black;">更加多</span>的服务器内存, PHP-CGI解释器每进程消耗7至25兆内存, 将这个数字乘以50或100<span style="color: black;">便是</span>很大的内存数。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1.3 CLI模式</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">PHP-CLI是PHP Command Line Interface的简<span style="color: black;">叫作</span>, 如同它名字的意思, <span style="color: black;">便是</span>PHP在命令行运行的接口, 区别于在Web服务器上运行的PHP环境(PHP-CGI, ISAPI等)。 <span style="color: black;">亦</span><span style="color: black;">便是</span>说, PHP不单<span style="color: black;">能够</span>写前台网页, 它还<span style="color: black;">能够</span>用来写后台的程序。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">PHP的CLI Shell脚本适用于所有的PHP<span style="color: black;">优良</span>, 使创建要么支持脚本或系统<span style="color: black;">乃至</span>与GUI应用程序的服务端, 在Windows和Linux下都是支持PHP-CLI模式的。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">PHP <span style="color: black;">供给</span>了一种新类型的 CLI SAPI(Server Application Programming Interface, 服务端应用编程端口)支持, 名为 CLI, 意为 Command Line Interface, 即命令行接口。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">顾名思义, 该 CLI SAPI 模块<span style="color: black;">重点</span>用作 PHP 的<span style="color: black;">研发</span>外壳应用。CLI SAPI 和其它 CLI SAPI 模块相比有<span style="color: black;">非常多</span>的<span style="color: black;">区别</span>之处, <span style="color: black;">咱们</span>将在本章中<span style="color: black;">仔细</span>阐述。值得一提的是,CLI 和 CGI 是<span style="color: black;">区别</span>的 SAPI,尽管它们之间有<span style="color: black;">非常多</span><span style="color: black;">一起</span>的<span style="color: black;">行径</span>。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在命令行下, 运行 php -v 便能得知该 php 是 CGI 还是 CLI。请参考函数 php_sapi_name() 以及常量 PHP_SAPI。</p><span style="color: black;">if</span>(PHP_SAPI == <span style="color: black;">cli</span>){
<span style="color: black;">echo</span> <span style="color: black;">"该PHP脚本属于cli模式"</span>;
}<span style="color: black;">else</span>{
<span style="color: black;">echo</span> <span style="color: black;">"该PHP脚本不属于cli模式"</span>;
}<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">以下例子检测了子字符串 cgi, <span style="color: black;">由于</span>它<span style="color: black;">亦</span>有可能会是 cgi-fcgi。</p><span style="color: black;"><span style="color: black;"><?php</span>
$sapi_type = php_sapi_name ();
<span style="color: black;">if</span>(substr($sapi_type , <span style="color: black;">0</span> , <span style="color: black;">3</span> ) == <span style="color: black;">cgi</span> ) {
<span style="color: black;">echo</span> <span style="color: black;">"You are using CGI PHP\n"</span>;
}eleeif(substr($sapi_type ,<span style="color: black;">0</span> , <span style="color: black;">3</span> ) == <span style="color: black;">cli</span> ){
<span style="color: black;">echo</span> <span style="color: black;">"You are using CLI PHP\n"</span> ;
}<span style="color: black;">else</span>{
<span style="color: black;">echo</span> <span style="color: black;">"You are not using CGI PHP\n"</span> ;
}
<span style="color: black;">?></span></span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">提醒: PHP 常量 PHP_SAPI <span style="color: black;">拥有</span>和 php_sapi_name() 相同的值, 多用于执行计划任务的PHP脚本的CLI模式的判断</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">【优点】</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1)<span style="color: black;">运用</span>多进程, 子进程结束以后, 内核会负责回收资源;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2)<span style="color: black;">运用</span>多进程, 子进程<span style="color: black;">反常</span>退出不会<span style="color: black;">引起</span><span style="color: black;">全部</span>进程Thread退出, 父进程还有机会重建流程;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">3)一个常驻主进程, 只负责任务分发, <span style="color: black;">规律</span>更清楚。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">咱们</span>在Linux下经常<span style="color: black;">运用</span>"php –m"<span style="color: black;">查询</span>PHP安装了<span style="color: black;">哪些</span>扩展<span style="color: black;">便是</span>PHP命令行运行模式;有兴趣的<span style="color: black;">朋友</span><span style="color: black;">能够</span>输入"php –h"去深入<span style="color: black;">科研</span>该运行模式。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> php命令行用法简介</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">php -m 查看安装的扩展</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">php -v 查看PHP版本</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">php -i 输出无html格式的phpinfo</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">php -ini php加载ini配置文件的数量、路径信息</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">php -h <span style="color: black;">表示</span><span style="color: black;">帮忙</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">php -l 语法<span style="color: black;">检测</span>PHP文件 php -l somefile.php(PHP Parse error: unexpected T_STRING in /tmp/somefile.php on line 81)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">查看扩展目录安装的位置</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">php -i | grep extension_dir</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1.4 模块模式</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 模块模式是以mod_php5模块的形式集成, 此时mod_php5模块的<span style="color: black;">功效</span>是接收Apache传递过来的PHP文件请求, 并处理这些请求, <span style="color: black;">而后</span>将处理后的结果返回给Apache。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">倘若</span><span style="color: black;">咱们</span>在Apache<span style="color: black;">起步</span>前在其配置文件中配置好了PHP模块(mod_php5), PHP模块<span style="color: black;">经过</span>注册apache2的ap_hook_post_config挂钩, 在Apache<span style="color: black;">起步</span>的时候<span style="color: black;">起步</span>此模块以接受PHP文件的请求。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 除了这种<span style="color: black;">起步</span>时的加载方式, Apache的模块<span style="color: black;">能够</span>在运行的时候动态装载, 这<span style="color: black;">寓意</span>着对服务器<span style="color: black;">能够</span>进行功能扩展而不需要重新对源代码进行编译, <span style="color: black;">乃至</span><span style="color: black;">基本</span>不需要停止服务器。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">咱们</span>所需要做的仅仅是给服务器发送信号HUP<span style="color: black;">或</span>AP_SIG_GRACEFUL<span style="color: black;">通告</span>服务器重新载入模块。<span style="color: black;">然则</span>在动态加载之前, <span style="color: black;">咱们</span>需要将模块编译<span style="color: black;">作为</span>动态链接库。此时的动态加载<span style="color: black;">便是</span>加载动态链接库。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Apache中对动态链接库的处理是<span style="color: black;">经过</span>模块mod_so来完成的, <span style="color: black;">因此呢</span>mod_so模块<span style="color: black;">不可</span>被动态加载, 它只能被静态编译进Apache的核心。这<span style="color: black;">寓意</span>着它是随着Apache<span style="color: black;">一块</span><span style="color: black;">起步</span>的。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> Apache是<span style="color: black;">怎样</span>加载模块的呢?<span style="color: black;">咱们</span>以前面<span style="color: black;">说到</span>的mod_php5模块为例。<span style="color: black;">首要</span><span style="color: black;">咱们</span>需要在Apache的配置文件httpd.conf中添加一行:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">LoadModule php5_module modules/mod_php5.so</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这儿</span>我们<span style="color: black;">运用</span>了LoadModule命令, 该命令的<span style="color: black;">第1</span>个参数是模块的名<span style="color: black;">叫作</span>, 名<span style="color: black;">叫作</span><span style="color: black;">能够</span>在模块实现的源码中找到。第二个选项是该模块所处的路径。<span style="color: black;">倘若</span>需要在服务器运行时加载模块, <span style="color: black;">能够</span><span style="color: black;">经过</span>发送信号HUP<span style="color: black;">或</span>AP_SIG_GRACEFUL给服务器,</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">一旦接受到该信号, Apache将重新装载模块, 而不需要重新<span style="color: black;">起步</span>服务器。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 该运行模式是<span style="color: black;">咱们</span>以前在windows环境下<span style="color: black;">运用</span>apache服务器经常<span style="color: black;">运用</span>的, 而在模块化(DLL)中, PHP是与Web服务器<span style="color: black;">一块</span><span style="color: black;">起步</span>并运行的。(它是apache在CGI的<span style="color: black;">基本</span>上进行的一种扩展, 加快PHP的运行效率)。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1.5 ISAPI模式</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">ISAPI(Internet Server Application Program Interface)是微软<span style="color: black;">供给</span>的一套面向Internet服务的API接口, 一个ISAPI的DLL, <span style="color: black;">能够</span>在被用户请求激活后长驻内存, 等待用户的另一个请求,</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">还<span style="color: black;">能够</span>在一个DLL里设置多个用户请求处理函数, <span style="color: black;">另外</span>, ISAPI的DLL应用程序和WWW服务器<span style="color: black;">处在</span>同一个进程中, 效率要<span style="color: black;">明显</span>高于CGI。(<span style="color: black;">因为</span>微软的排他性, 只能运行于windows环境)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> PHP<span style="color: black;">做为</span>Apache模块, Apache服务器在系统<span style="color: black;">起步</span>后, 预先生成多个进程副本驻留在内存中, 一旦有请求<span style="color: black;">显现</span>, 就立即<span style="color: black;">运用</span>这些空余的子进程进行处理, <span style="color: black;">这般</span>就不存在生成子进程<span style="color: black;">导致</span>的延迟了。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">这些服务器副本在处理完一次HTTP请求之后并不立即退出, 而是停留在计算机中等待下次请求。<span style="color: black;">针对</span>客户浏览器的请求反应更快, 性能较高。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">实例: 执行计划任务的脚本(CLI模式的判断)</p><span style="color: black;"><span style="color: black;"><?php</span>
header(<span style="color: black;">"Content-Type: text/html; charset=UTF-8"</span>);
<span style="color: black;"><span style="color: black;">function</span> <span style="color: black;">http_gets</span><span style="color: black;">($url)</span></span>{
$oCurl = curl_init();
<span style="color: black;">if</span>(stripos($url,<span style="color: black;">"https://"</span>)!==<span style="color: black;">FALSE</span>){
curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, <span style="color: black;">FALSE</span>);
curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST,<span style="color: black;">FALSE</span>);
curl_setopt($oCurl, CURLOPT_SSLVERSION, <span style="color: black;">1</span>); <span style="color: black;">//CURL_SSLVERSION_TLSv1</span>}
curl_setopt($oCurl, CURLOPT_URL, $url);
curl_setopt($oCurl, CURLOPT_RETURNTRANSFER,<span style="color: black;">1</span>);
$sContent = curl_exec($oCurl);
$aStatus = curl_getinfo($oCurl);
curl_close($oCurl);<span style="color: black;">if</span>(intval($aStatus[<span style="color: black;">"http_code"</span>])==<span style="color: black;">200</span>){
<span style="color: black;">return</span> <span style="color: black;">true</span>;
}<span style="color: black;">else</span>{
<span style="color: black;">return</span> <span style="color: black;">false</span>;
}
}
<span style="color: black;">if</span>(PHP_SAPI == <span style="color: black;">cli</span>){
$domain=<span style="color: black;">"47.74.157.205"</span>;
$queues = <span style="color: black;">array</span>(
<span style="color: black;">Admin/Login/queue</span>,
<span style="color: black;">Home/Queue/tendency</span>,
<span style="color: black;">Home/Queue/houprice</span>,
<span style="color: black;">Home/Queue/paicuo</span>,
<span style="color: black;">Home/Queue/qianbao</span>,
<span style="color: black;">Home/Queue/move</span>,
<span style="color: black;">Home/Queue/yichang</span>,
);
$fp = fopen(<span style="color: black;">"/data/thyjy/lockrun.txt"</span>, <span style="color: black;">"w+"</span>);
<span style="color: black;">if</span>(flock($fp,LOCK_EX | LOCK_NB))
{
<span style="color: black;">for</span>($i=<span style="color: black;">0</span>;$i<count($queues);$i++){
http_gets(<span style="color: black;">"http://"</span>.$domain.<span style="color: black;">"/"</span>.$queues[$i]);
}
flock($fp,LOCK_UN);
}
fclose($fp);
<span style="color: black;">echo</span> <span style="color: black;">"<span style="color: black;">这次</span>执行完毕"</span>;
}
<span style="color: black;">?></span></span>
真情实感,其含义为认真了、走心了的意思,是如今的饭圈常用语。
页:
[1]