PHP鍐呮牳浜嗚В锛氱敓鍛藉懆鏈熷強杩愯妯″紡

PHP杩愯妯″紡

1锛塁GI锛堥氱敤缃戝叧鎺ュ彛 / Common Gateway Interface锛
2锛塅astCGI锛堝父椹诲瀷CGI / Long-Live CGI锛
3锛塁LI锛堝懡浠よ杩愯 / Command Line Interface锛
4锛塛eb妯″潡妯″紡锛圓pache绛塛eb鏈嶅姟鍣ㄨ繍琛岀殑妯″紡锛
5锛塈SAPI锛圛nternet Server Application Program Interface锛
澶囨敞锛氬湪PHP5.3浠ュ悗锛孭HP涓嶅啀鏈塈SAPI妯″紡

CGI鏄釜鍗忚锛岃窡杩涚▼浠涔堢殑娌″叧绯汇傞偅fastcgi鍙堟槸浠涔堝憿锛烣astcgi鏄敤鏉ユ彁楂楥GI绋嬪簭鎬ц兘鐨勩

PHP涓殑CGI瀹炵幇

PHP鐨凜GI瀹炵幇鏈川鏄槸浠ocket缂栫▼瀹炵幇涓涓猅CP鎴朥DP鍗忚鐨勬湇鍔″櫒锛屽綋鍚姩鏃讹紝鍒涘缓TCP/UDP鍗忚鐨勬湇鍔″櫒鐨剆ocket鐩戝惉锛 骞舵帴鏀剁浉鍏宠姹傝繘琛屽鐞嗐傝繖鍙槸璇锋眰鐨勫鐞嗭紝鍦ㄦ鍩虹涓婃坊鍔犳ā鍧楀垵濮嬪寲锛宻api鍒濆鍖栵紝妯″潡鍏抽棴锛宻api鍏抽棴绛夊氨鏋勬垚浜嗘暣涓狢GI鐨勭敓鍛藉懆鏈熴


CGI

CGI鍏ㄧО鏄滈氱敤缃戝叧鎺ュ彛鈥(Common Gateway Interface)锛 瀹冨彲浠ヨ涓涓鎴风锛屼粠缃戦〉娴忚鍣ㄥ悜鎵ц鍦╓eb鏈嶅姟鍣ㄤ笂鐨勭▼搴忚姹傛暟鎹

CGI鎻忚堪浜嗗鎴风鍜岃繖涓▼搴忎箣闂翠紶杈撴暟鎹殑涓绉嶆爣鍑嗐
CGI鐨勪竴涓洰鐨勬槸瑕佺嫭绔嬩簬浠讳綍璇█鐨勶紝鎵浠GI鍙互鐢ㄤ换浣曚竴绉嶈瑷缂栧啓锛屽彧瑕佽繖绉嶈瑷鍏锋湁鏍囧噯杈撳叆銆佽緭鍑哄拰鐜鍙橀噺銆傚php,perl,tcl绛

CGI宸茬粡鏄瘮杈冭佺殑妯″紡浜嗭紝杩欏嚑骞撮兘寰堝皯鐢ㄤ簡銆

  • 姣忔湁涓涓敤鎴疯姹傦紝閮戒細鍏堣鍒涘缓CGI鐨勫瓙杩涚▼锛岀劧鍚庡鐞嗚姹傦紝澶勭悊瀹屽悗缁撴潫杩欎釜瀛愯繘绋嬶紝杩欏氨鏄疐ork-And-Execute妯″紡銆
  • 褰撶敤鎴疯姹傛暟閲忛潪甯稿鏃讹紝浼氬ぇ閲忔尋鍗犵郴缁熺殑璧勬簮濡傚唴瀛橈紝CPU鏃堕棿绛夛紝閫犳垚鏁堣兘浣庝笅銆
  • 鎵浠ョ敤CGI鏂瑰紡鐨勬湇鍔″櫒鏈夊灏戣繛鎺ヨ姹傚氨浼氭湁澶氬皯CGI瀛愯繘绋嬶紝瀛愯繘绋嬪弽澶嶅姞杞芥槸CGI鎬ц兘浣庝笅鐨勪富瑕佸師鍥犮

褰搘eb server鏀跺埌/index.php杩欎釜璇锋眰鍚庯紝浼氬惎鍔ㄥ搴旂殑CGI绋嬪簭锛岃繖閲屽氨鏄疨HP鐨勮В鏋愬櫒銆傛帴涓嬫潵PHP瑙f瀽鍣ㄤ細瑙f瀽php.ini鏂囦欢锛屽垵濮嬪寲鎵ц鐜锛岀劧鍚庡鐞嗚姹傦紝鍐嶄互瑙勫畾CGI瑙勫畾鐨勬牸寮忚繑鍥炲鐞嗗悗鐨勭粨鏋滐紝閫鍑鸿繘绋嬨倃eb server鍐嶆妸缁撴灉杩斿洖缁欐祻瑙堝櫒銆

FastCGI

fast-cgi 鏄痗gi鐨勫崌绾х増鏈紝FastCGI 鍍忔槸涓涓父椹 (long-live) 鍨嬬殑 CGI锛屽畠鍙互涓鐩存墽琛岀潃锛屽彧瑕佹縺娲诲悗锛屼笉浼氭瘡娆¢兘瑕佽姳璐规椂闂村幓 fork 涓娆 (杩欐槸 CGI 鏈涓轰汉璇熺梾鐨 fork-and-execute 妯″紡)銆

FastCGI宸ヤ綔鍘熺悊

  1. Web Server鍚姩鏃惰浇鍏astCGI杩涚▼绠$悊鍣ㄣ怭HP鐨凢astCGI杩涚▼绠$悊鍣ㄦ槸PHP-FPM(php-FastCGI Process Manager)銆戯紙IIS ISAPI鎴朅pache Module)
  2. FastCGI杩涚▼绠$悊鍣ㄨ嚜韬垵濮嬪寲锛屽惎鍔ㄥ涓狢GI瑙i噴鍣ㄨ繘绋 (鍙澶氫釜php-cgi.exe鎴杙hp-cig)骞剁瓑寰呮潵鑷猈eb Server鐨勮繛鎺ワ紱
  3. 褰撳鎴风璇锋眰鍒拌揪Web Server鏃讹紝FastCGI杩涚▼绠$悊鍣ㄩ夋嫨骞惰繛鎺ュ埌涓涓狢GI瑙i噴鍣ㄣ俉eb server灏咰GI鐜鍙橀噺鍜屾爣鍑嗚緭鍏ュ彂閫佸埌FastCGI瀛愯繘绋媝hp-cgi
  4. FastCGI瀛愯繘绋嬪畬鎴愬鐞嗗悗灏嗘爣鍑嗚緭鍑哄拰閿欒淇℃伅浠庡悓涓杩炴帴杩斿洖Web Server銆傚綋FastCGI瀛愯繘绋嬪叧闂繛鎺ユ椂锛岃姹備究鍛婂鐞嗗畬鎴愩侳astCGI瀛愯繘绋嬫帴鐫绛夊緟骞跺鐞嗘潵鑷狥astCGI杩涚▼绠$悊鍣紙杩愯鍦 WebServer涓級鐨勪笅涓涓繛鎺ャ
  5. 鍦ㄦ甯哥殑CGI妯″紡涓紝php-cgi.exe鍦ㄦ渚块鍑轰簡銆傚湪CGI妯″紡涓紝浣犲彲浠ユ兂璞 CGI閫氬父鏈夊鎱€傛瘡涓涓猈eb璇锋眰PHP閮藉繀椤婚噸鏂拌В鏋恜hp.ini銆侀噸鏂拌浇鍏ュ叏閮╠ll鎵╁睍骞堕噸鍒濆鍖栧叏閮ㄦ暟鎹粨鏋勩備娇鐢‵astCGI锛屾墍鏈夎繖浜涢兘鍙湪杩涚▼鍚姩鏃跺彂鐢熶竴娆°備竴涓澶栫殑濂藉鏄紝鎸佺画鏁版嵁搴撹繛鎺ワ紙Persistent database connection锛夊彲浠ュ伐浣溿

澶囨敞锛歅HP鐨凢astCGI杩涚▼绠$悊鍣ㄦ槸PHP-FPM锛圥HP-FastCGI Process Manager锛

浼樼偣

  • 浠庣ǔ瀹氭т笂鐪嬶紝FastCGI鏄互鐙珛鐨勮繘绋嬫睜鏉ヨ繍琛孋GI锛屽崟鐙竴涓繘绋嬫鎺夛紝绯荤粺鍙互寰堣交鏄撶殑涓㈠純锛岀劧鍚庨噸鏂板垎閰嶆柊鐨勮繘绋嬫潵杩愯閫昏緫锛
  • 浠庡畨鍏ㄦт笂鐪嬶紝FastCGI鏀寔鍒嗗竷寮忚繍绠椼侳astCGI鍜屽涓荤殑Server瀹屽叏鐙珛锛孎astCGI鎬庝箞down涔熶笉浼氭妸Server鎼炲灝锛
  • 浠庢ц兘涓婄湅锛孎astCGI鎶婂姩鎬侀昏緫鐨勫鐞嗕粠Server涓垎绂诲嚭鏉ワ紝澶ц礋鑽风殑IO澶勭悊杩樻槸鐣欑粰瀹夸富Server锛岃繖鏍峰涓籗erver鍙互涓蹇冧竴鎰忎綔IO锛屽浜庝竴涓櫘閫氱殑鍔ㄦ佺綉椤垫潵璇, 閫昏緫澶勭悊鍙兘鍙湁涓灏忛儴鍒嗭紝澶ч噺鐨勬槸鍥剧墖绛夐潤鎬併

涓嶈冻

鍥犱负鏄杩涚▼锛屾墍浠ユ瘮CGI澶氱嚎绋嬫秷鑰楁洿澶氱殑鏈嶅姟鍣ㄥ唴瀛橈紝PHP-CGI瑙i噴鍣ㄦ瘡杩涚▼娑堣7鑷25鍏嗗唴瀛橈紝灏嗚繖涓暟瀛椾箻浠50鎴100灏辨槸寰堝ぇ鐨勫唴瀛樻暟銆

Nginx 0.8.46+PHP 5.2.14(FastCGI)鏈嶅姟鍣ㄥ湪3涓囧苟鍙戣繛鎺ヤ笅锛屽紑鍚殑10涓狽ginx杩涚▼娑堣150M鍐呭瓨锛15M10=150M锛夛紝寮鍚殑64涓猵hp-cgi杩涚▼娑堣1280M鍐呭瓨锛20M64=1280M锛夛紝鍔犱笂绯荤粺鑷韩娑堣楃殑鍐呭瓨锛屾诲叡娑堣椾笉鍒2GB鍐呭瓨銆傚鏋滄湇鍔″櫒鍐呭瓨杈冨皬锛屽畬鍏ㄥ彲浠ュ彧寮鍚25涓猵hp-cgi杩涚▼锛岃繖鏍穚hp-cgi娑堣楃殑鎬诲唴瀛樻暟鎵500M銆

涓婇潰鐨勬暟鎹憳鑷狽ginx 0.8.x + PHP 5.2.13(FastCGI)鎼缓鑳滆繃Apache鍗佸嶇殑Web鏈嶅姟鍣(绗6鐗)

CLI

PHP-CLI鏄疨HP Command Line Interface鐨勭畝绉帮紝灏辨槸PHP鍦ㄥ懡浠よ杩愯鐨勬帴鍙o紝鍖哄埆浜庡湪Web鏈嶅姟鍣ㄤ笂杩愯鐨凱HP鐜锛圥HP-CGI锛孖SAPI绛夛級銆
涔熷氨鏄锛孭HP涓嶅崟鍙互鍐欏墠鍙扮綉椤碉紝瀹冭繕鍙互鐢ㄦ潵鍐欏悗鍙扮殑绋嬪簭銆 PHP鐨凜LI Shell鑴氭湰閫傜敤浜庢墍鏈夌殑PHP浼樺娍锛屼娇鍒涘缓瑕佷箞鏀寔鑴氭湰鎴栫郴缁熺敋鑷充笌GUI搴旂敤绋嬪簭鐨勬湇鍔$锛屽湪Windows鍜孡inux涓嬮兘鏄敮鎸丳HP-CLI妯″紡鐨勩

鎴戜滑鍦↙inux涓嬬粡甯镐娇鐢ㄢ漰hp 鈥搈鈥濇煡鎵綪HP瀹夎浜嗛偅浜涙墿灞曞氨鏄疨HP鍛戒护琛岃繍琛屾ā寮忥紱

PHP寮濮嬪拰缁撴潫闃舵

PHP寮濮嬫墽琛屼互鍚庝細缁忚繃涓や釜涓昏鐨勯樁娈碉細澶勭悊璇锋眰涔嬪墠鐨勫紑濮嬮樁娈靛拰璇锋眰涔嬪悗鐨勭粨鏉熼樁娈点

寮濮嬮樁娈

妯″潡鍒濆鍖栭樁娈礛INIT

鍦ㄦ暣涓猄API鐢熷懡鍛ㄦ湡鍐(渚嬪Apache鍚姩浠ュ悗鐨勬暣涓敓鍛藉懆鏈熷唴鎴栬呭懡浠よ绋嬪簭鏁翠釜鎵ц杩囩▼涓)锛 璇ヨ繃绋嬪彧杩涜涓娆°

鍚姩Apache鍚庯紝PHP瑙i噴绋嬪簭涔熼殢涔嬪惎鍔紱
PHP璋冪敤鍚勪釜鎵╁睍锛堟ā鍧楋級鐨凪INIT鏂规硶锛屼粠鑰屼娇杩欎簺鎵╁睍鍒囨崲鍒板彲鐢ㄧ姸鎬併

PHP_MINIT_FUNCTION(myphpextension){// 娉ㄥ唽甯搁噺鎴栬呯被绛夊垵濮嬪寲鎿嶄綔returnSUCCESS;}

妯″潡婵娲婚樁娈礡INIT

璇ヨ繃绋嬪彂鐢熷湪璇锋眰闃舵锛 渚嬪閫氳繃url璇锋眰鏌愪釜椤甸潰锛屽垯鍦ㄦ瘡娆¤姹備箣鍓嶉兘浼氳繘琛屾ā鍧楁縺娲伙紙RINIT璇锋眰寮濮嬶級銆
璇锋眰鍒拌揪涔嬪悗锛孲API灞傚皢鎺у埗鏉冧氦缁橮HP灞傦紝PHP鍒濆鍖栨湰娆¤姹傛墽琛岃剼鏈墍闇鐨勭幆澧冨彉閲

渚嬪鏄疭ession妯″潡鐨凴INIT锛屽鏋滃湪php.ini涓惎鐢ㄤ簡Session 妯″潡锛岄偅鍦ㄨ皟鐢ㄨ妯″潡鐨凴INIT鏃跺氨浼氬垵濮嬪寲$_SESSION鍙橀噺锛屽苟灏嗙浉鍏冲唴瀹硅鍏ワ紱 鐒跺悗PHP浼氳皟鐢ㄦ墍鏈夋ā鍧桼INIT鍑芥暟,鍗斥滆姹傚垵濮嬪寲鈥濄
鍦ㄨ繖涓樁娈靛悇涓ā鍧椾篃鍙互鎵ц涓浜涚浉鍏崇殑鎿嶄綔, 妯″潡鐨凴INIT鍑芥暟鍜孧INIT鍑芥暟绫讳技 锛孯INIT鏂规硶鍙互鐪嬩綔鏄竴涓噯澶囪繃绋嬶紝鍦ㄧ▼搴忔墽琛屼箣鍓嶅氨浼氳嚜鍔ㄥ惎鍔ㄣ

缁撴潫闃舵

璇锋眰澶勭悊瀹屽悗灏辫繘鍏ヤ簡缁撴潫闃舵, 涓鑸剼鏈墽琛屽埌鏈熬鎴栬呴氳繃璋冪敤exit()鎴栬卍ie()鍑芥暟,PHP閮藉皢杩涘叆缁撴潫闃舵. 鍜屽紑濮嬮樁娈靛搴,缁撴潫闃舵涔熷垎涓轰袱涓幆鑺,涓涓湪璇锋眰缁撴潫鍚(RSHUWDOWN),涓涓湪SAPI鐢熷懡鍛ㄦ湡缁撴潫鏃(MSHUTDOWN).

璇锋眰缁撴潫鍚(RSHUWDOWN)

璇锋眰澶勭悊瀹屽悗灏辫繘鍏ヤ簡缁撴潫闃舵锛孭HP灏变細鍚姩娓呯悊绋嬪簭銆
瀹冧細鎸夐『搴忚皟鐢ㄥ悇涓ā鍧楃殑RSHUTDOWN鏂规硶銆
RSHUTDOWN鐢ㄤ互娓呴櫎绋嬪簭杩愯鏃朵骇鐢熺殑绗﹀彿琛紝涔熷氨鏄姣忎釜鍙橀噺璋冪敤unset鍑芥暟銆

SAPI鐢熷懡鍛ㄦ湡缁撴潫鏃(MSHUTDOWN)

鏈鍚庯紝鎵鏈夌殑璇锋眰閮藉凡澶勭悊瀹屾瘯
SAPI涔熷噯澶囧叧闂簡
PHP璋冪敤姣忎釜鎵╁睍鐨凪SHUTDOWN鏂规硶
杩欐椂鍚勪釜妯″潡鏈鍚庝竴娆¢噴鏀惧唴瀛樼殑鏈轰細銆
锛堣繖涓槸瀵逛簬CGI鍜孋LI绛塖API锛屾病鏈夆滀笅涓涓姹傗濓紝鎵浠API绔嬪埢寮濮嬪叧闂傦級

鏁翠釜PHP鐢熷懡鍛ㄦ湡灏辩粨鏉熶簡銆傝娉ㄦ剰鐨勬槸锛屽彧鏈夊湪鏈嶅姟鍣ㄦ病鏈夎姹傜殑鎯呭喌涓嬫墠浼氭墽琛屸滃惎鍔ㄧ涓姝モ濆拰鈥滃叧闂浜屾鈥濄

SAPI杩愯PHP閮界粡杩囩殑鍑犱釜闃舵

妯″潡鍒濆鍖栭樁娈(Module init)

鍗宠皟鐢ㄦ瘡涓嫇灞曟簮鐮佷腑鐨勭殑PHP_MINIT_FUNCTION涓殑鏂规硶鍒濆鍖栨ā鍧,杩涜涓浜涙ā鍧楁墍闇鍙橀噺鐨勭敵璇,鍐呭瓨鍒嗛厤绛夈

璇锋眰鍒濆鍖栭樁娈(Request init)

鍗虫帴鍙楀埌瀹㈡埛绔殑璇锋眰鍚庤皟鐢ㄦ瘡涓嫇灞曠殑PHP_RINIT_FUNCTION涓殑鏂规硶,鍒濆鍖朠HP鑴氭湰鐨勬墽琛岀幆澧冦
  1. 鎵цPHP鑴氭湰

璇锋眰缁撴潫(Request Shutdown)

杩欐椂鍊欒皟鐢ㄦ瘡涓嫇灞曠殑PHP_RSHUTDOWN_FUNCTION鏂规硶娓呯悊璇锋眰鐜板満,骞朵笖ZE寮濮嬪洖鏀跺彉閲忓拰鍐呭瓨

鍏抽棴妯″潡(Module shutdown)

Web鏈嶅姟鍣ㄩ鍑烘垨鑰呭懡浠よ鑴氭湰鎵ц瀹屾瘯閫鍑轰細璋冪敤鎷撳睍婧愮爜涓殑PHP_MSHUTDOWN_FUNCTION 鏂规硶

鍗曡繘绋婼API鐢熷懡鍛ㄦ湡

CLI/CGI妯″紡鐨凱HP灞炰簬鍗曡繘绋嬬殑SAPI妯″紡銆傝繖绫荤殑璇锋眰鍦ㄥ鐞嗕竴娆¤姹傚悗灏卞叧闂 涔熷氨鏄彧浼氱粡杩囧涓嬪嚑涓幆鑺傦細 寮濮-璇锋眰寮濮-璇锋眰鍏抽棴-缁撴潫 SAPI鎺ュ彛瀹炵幇灏卞畬鎴愪簡鍏剁敓鍛藉懆鏈熴


澶氳繘绋婼API鐢熷懡鍛ㄦ湡

  • 閫氬父PHP鏄紪璇戜负apache鐨勪竴涓ā鍧楁潵澶勭悊PHP璇锋眰銆
  • Apache涓鑸細閲囩敤澶氳繘绋嬫ā寮忥紝 Apache鍚姩鍚庝細fork鍑哄涓瓙杩涚▼锛屾瘡涓繘绋嬬殑鍐呭瓨绌洪棿鐙珛锛屾瘡涓瓙杩涚▼閮戒細缁忚繃寮濮嬪拰缁撴潫鐜妭
  • 姣忎釜杩涚▼鐨勫紑濮嬮樁娈靛彧鍦ㄨ繘绋媐ork鍑烘潵浠ユ潵鍚庤繘琛岋紝鍦ㄦ暣涓繘绋嬬殑鐢熷懡鍛ㄦ湡鍐呭彲鑳戒細澶勭悊澶氫釜璇锋眰銆
  • 鍙湁鍦ˋpache鍏抽棴鎴栬呰繘绋嬭缁撴潫涔嬪悗鎵嶄細杩涜鍏抽棴闃舵锛屽湪杩欎袱涓樁娈典箣闂翠細闅忕潃姣忎釜璇锋眰閲嶅璇锋眰寮濮-璇锋眰鍏抽棴鐨勭幆鑺傘

澶氱嚎绋嬬殑SAPI鐢熷懡鍛ㄦ湡

澶氱嚎绋嬫ā寮忓拰澶氳繘绋嬩腑鐨勬煇涓繘绋嬬被浼硷紝涓嶅悓鐨勬槸鍦ㄦ暣涓繘绋嬬殑鐢熷懡鍛ㄦ湡鍐呬細骞惰鐨勯噸澶嶇潃 璇锋眰寮濮-璇锋眰鍏抽棴鐨勭幆鑺.

鍦ㄨ繖绉嶆ā寮忎笅锛屽彧鏈変竴涓湇鍔″櫒杩涚▼鍦ㄨ繍琛岀潃锛屼絾浼氬悓鏃惰繍琛屽緢澶氱嚎绋嬶紝杩欐牱鍙互鍑忓皯涓浜涜祫婧愬紑閿锛屽悜Module init鍜孧odule shutdown灏卞彧闇瑕佽繍琛屼竴閬嶅氨琛屼簡锛屼竴浜涘叏灞鍙橀噺涔熷彧闇瑕佸垵濮嬪寲涓娆★紝鍥犱负绾跨▼鐙叿鐨勭壒璐紝浣垮緱鍚勪釜璇锋眰涔嬮棿鏂逛究鐨勫叡浜竴浜涙暟鎹垚涓哄彲鑳姐


浠ヤ笂灏辨槸PHP鍐呮牳浜嗚В锛氱敓鍛藉懆鏈熷強杩愯妯″紡鐨勮缁嗗唴瀹癸紝鏇村淇℃伅璇峰叧娉∣D浜戝叾瀹冪浉鍏虫枃绔狅紒



鏈枃URL锛http://www.odweb.cn/news_show.html?id=144