當前位置:才華齋>計算機>php語言>

php的4種常用執行方式詳解

php語言 閱讀(2.12W)

php的4種常用執行方式:CGI、FastCGI、APACHE2HANDLER、CLI。下面是具體的使用方法,希望對大家有用,更多訊息請關注應屆畢業生網。

php的4種常用執行方式詳解

  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都必須重新解析、重新載入全部擴充套件並重初始化全部資料結構。使用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 GroupZend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies

檢視當前php的擴充套件

?

eric:~ youngeric$ php -m

[PHP Modules]bcmathbz2calendarCorectypecurldate......

檢視配置資訊(相當於使用phpinfo()函式)

?

eric:~ youngeric$ php -ini

phpinfo()PHP Version => 5.5.38

System => Darwin l 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_64Build Date => Oct 1 2016 23:01:51Configure 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/' '--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 InterfaceVirtual Directory Support => disabledConfiguration File () Path => /usr/local/etc/php/5.5Loaded Configuration File => /usr/local/etc/php/5.5/can this dir for additional files => /usr/local/etc/php/5.5/conf.d......

檢視函式資訊

?

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

檢測php程式碼

?

eric:~ youngeric$ php -l

PHP Parse error: syntax error, unexpected end of file, expecting ',' or ';' in on line 3Errors parsing

作為世界上最好的語言,php甚至還內建了伺服器的功能(有沒有很震驚的樣子)。

?

eric:Desktop youngeric$ php -S

PHP 5.5.38 Development Server started at Thu Dec 22 09:44:20 2016Listening on Document root is /Users/youngeric/DesktopPress Ctrl-C to quit.[Thu Dec 22 09:44:29 2016] [404]: / - No such file or directory[Thu Dec 22 09:44:29 2016] [404]: / - No such file or directory