ç¶ãPHPã¨H2OãfastCGIã§ã¤ãªã話ãæé»é
追è¨
æ¬ã¨ã³ããªã®ãã¹ãï¼ãã§ããªã件ã¯ã解決ããã¾ããï¼
ååã®ãããã
- H2Oã¨ããéãhttpdãfastCGIããµãã¼ã
- PHPã¯fastCGIããµãã¼ã
- PHPerã¯ã¤ãªããã
- å®éã¤ãªãã£ãï¼ãã£ãã¼ï¼
- ããããè¨å®ããããããã
- ã¼ããããä½è ã®kazuhoããã«ã¢ã³ãµã¼ã¨ã³ããªãé ãã
- ãã£ãï¼è©¦ããï¼
Kazuho's Weblog: H2OとPHPを組み合わせるの、超簡単です(もしくはmod_rewriteが不要な理由)
ä»åã®è©±
ã¡ããã¨ã«ã¼ã¿ã¼ãã¤ãããããªPHPã¢ããªãç¹ããã®ããPATH_INFOå¨è¾ºã¯ã©ããªãã®ãããããªã·ã³ãã«ãªconfigã§æ¬å½ã«åãã®ãã
ããã¦ãkazuhoããã®èª¬æãè¦ãæããµã¨æ°ã«ãªã£ãã®ããPHPã®ã¢ããªã®ç¸ä¹ãã¯ã©ããªãã®ãï¼ãã§ããã
ï¼å¾ãå®éã®è¨å®ãã¡ã¤ã«ãããã£ã¬ã¯ããªåãªã©ãã¡ãã£ã¨ã°ããä¿®æ£ãã¦ãã®ã§ããããããããã¤ãã¤ã¾ãåã£ã¦ãªãã¨ããããã£ãããã¿ã¾ããï¼
ä»åãã¾ããã®æ§ã«è¨å®ãã
listen: 8080 file.custom-handler: extension: .php fastcgi.connect: host: 127.0.0.1 port: 9000 type: tcp hosts: "127.0.0.1:8080": paths: /: file.dir: /var/www/app/htdocs redirect: url: /index.php/ internal: YES status: 307 access-log: /dev/stdout
ï¼ããã¯configå ¨ä½ã§ãããçãï¼ï¼
/var/www/app/(htdocs)ã«ã¯ãSlim frameworkã§ã¤ãã£ãPHPã®ã¢ããªãããã¦ããã
説æã¯ä½è ã®kazuhoããã®ã¨ã³ããªã«ããããã
Kazuho's Weblog: H2OとPHPを組み合わせるの、超簡単です(もしくはmod_rewriteが不要な理由)
fileãåå¨ããªãã£ããå é¨ãªãã¤ã¬ã¯ãã§redirectç¯ã«è¡ãããªãã»ã©ã§ãããnginxãããæ°åãã£ãããã¦ããã
çµè«ããããã°ããã®è¨å®ãããã ãã§ãæåæãããã»ã©ä½ã®åé¡ããªãæå¾ éãã«åããããã£ããï¼ï¼
PHPerã«ã¯H2Oãå
ãè¼ãã¦ã¿ããã®ã ã£ãï¼
çãããå ·ä½çã«æ°ã«ãªãã§ãããæ
åè¿°ã®è¨å®ã§
$app->get('/path/phpinfo', function () use ($app){ phpinfo(); exit; });
ã¿ããã«ã«ã¼ã¿ã¼ã«ã«ã¼ããç»é²ããã¦ããã¨ãã¦ã
http://localhost:8080/path/phpinfo?hoge=fuge
ã¿ãããªURLã§ã¢ã¯ã»ã¹ããã¨ããPHPerãªãééããªãæ°ã«ãªã$_SERVERã®ä¸é¨ããã¡ãã
$_SERVER['SERVER_SOFTWARE'] h2o/1.3.0 $_SERVER['SERVER_PROTOCOL'] HTTP/1.1 $_SERVER['SERVER_NAME'] 127.0.0.1 $_SERVER['SERVER_PORT'] 8080 $_SERVER['SERVER_ADDR'] ::1 $_SERVER['REQUEST_URI'] /path/phpinfo?hoge=fuge $_SERVER['HTTP_HOST'] localhost:8080 $_SERVER['REQUEST_METHOD'] GET $_SERVER['REMOTE_PORT'] 59195 $_SERVER['REMOTE_ADDR'] ::1 $_SERVER['QUERY_STRING'] hoge=fuge $_SERVER['PATH_INFO'] /path/phpinfo $_SERVER['SCRIPT_NAME'] /index.php $_SERVER['SCRIPT_FILENAME'] /var/www/app/htdocs/index.php $_SERVER['PHP_SELF'] /index.php/path/phpinfo $_SERVER['argv'] Array ( [0] => hoge=fuge ) $_SERVER['argc'] 1
ãããç¥ããã人ã«ã¯èª¬æãããªãã ããã
PHP_INFOãSCRIPT_NAMEãPHP_SELFãREQUEST_URIããã®ãããã®é£ãåãããæªãã¨æ»ã¬ç³»ã®ãã¬ã¼ã ã¯ã¼ã¯ã¯çµæ§ãããã¾ã俺ã¯å¥½ããªSlimãåé¡ãªãããªã®ã§åé¡ã¯ãªãã
ããã¾ã§ã§æ¢ãã¦ããã°ãH2Oã¨PHPã¯æå¼·ã¿ãã°ï¼ã¿ãããªäºãè¨ãããã§ããã
ããããPHPerã¯ãã³ãã³ã¨éã«æ½ã£ã¦ãããããªãã®ã â¦ã
ã¢ããªã®ç¸ä¹ãã®è©±
ãã®ã¢ããªã¯ä»¥ä¸ã®ãããªè¨å®ã«ãªã£ã¦ããã®ã¯ä¸è¿°ããéãã ãã
/: file.dir: /var/www/app/htdocs redirect: url: /index.php/ internal: YES status: 307
ããã¯
- /path/phpinfoã«ã¢ã¯ã»ã¹ããã
- ãã¡ã¤ã«ããªãã£ãããredirectç¯ã«ãã
- å é¨ãªãã¤ã¬ã¯ãã§index.phpã®URLã«ãªã
- å¿è«ããã¯åå¨ã
- ãã®ãã¡ã¤ã«ã®æ¡å¼µåãphpãªã®ã§ãfile.custom-handler:\n extension: .phpããçºç«ã
- fastCGIã¸ãSCRIPT_FILENAMEã«/var/www/app/htdocs/index.phpæå®ã§ããããã³ã¼ã«ããã
ãã ã¨æããï¼é·ãï¼
ã§ã¯æ°ã«ãªãã®ããåæ§ã®è¨è¿°ã並ã¹ãè¤æ°ã®ä»®æ³ãã¹ã¯æ±ããã®ãï¼ï¼ã¢ããªã®ç¸ä¹ãã¯ã§ããã®ãï¼ï¼
ã³ã¹ãåæ¸ãå«ã°ããç¾ä»£ãã¨ã³ã¸ãã¢ã®ã¡ãããã®ã¿ãªãã*1ãå½ç¶ãªãããµã¼ãã¼å°æ°ãã§ããã°å¹ççã«åæ¸ããããã¤ã¾ããå種ã¢ããªãç¸ä¹ãããããã
/index.phpä¸ã¤ã«ãã¹ã¦ã®ã¢ããªãçµ±åãã¦ã¯ããããªãã®ã ï¼*2
ãã¹ãï¼
ããã§ã¾ãhtdocs以ä¸ã« phpinfo.php ãããã¦ããã®URLãéãã¦ã¿ãã
http://localhost:8080/phpinfo.php
ããã¯åããApacheçãªæåã§ããããããã好æãæã¦ãï¼
ã§ã¯ãä»®æ³ãã¹ããã¦ã¦ã¿ããã
http://localhost:8080/info/asdf
ã¨ããã¨ãã«ãphpinfo.phpãå¼ã¹ãã®ãï¼
以ä¸ã®ããã«ãã¦ã¿ããè¦äºã«ããããï¼ããããã§ããã
/info: file.dir: /var/www/app/htdocs redirect: url: /phpinfo.php/ internal: YES status: 307
( /info:ã¯/: ã®åã«ããã¦ããå¾ã«ããã¦ã大ä¸å¤«ã£ã½ããã©ããããããé å¤å®ã«ãªãã®ãããããããªããæé·ããªï¼ ï¼
ãã¹ãï¼
ã§ã¯ãä¸éã§ã¯ä½ãè¦ãªãããèªåããã°ãã°ããããã¦ãnginxã ã¨è¨å®ãé·ããªã£ã¦é£åãªè¨å®ããã£ã¦ã¿ãã*3
htdocsãå ±ã«ããªãå¥ã®appããæ¥ãæ¨ï¼ãã¦ã³ãï¼ãã¦ã¿ããã
/app2: file.dir: /var/www/app2/htdocs redirect: url: /index.php/ internal: YES status: 307
ãããä¸æãããã ãâ¦ã¨ããã£ããæ®å¿µãä¸æããããªãã£ãã
http://localhost:8080/app2/index.php
ã¯ãã¾ãapp2/htdocs/index.phpãèªã¿è¾¼ã¾ããã®ã ãã
ãæå®ããã¨ãã©ããã/:ã®æ¹ã®ã/var/www/app/htdocs/index.phpãèªã¿è¾¼ã¾ãã模æ§ã
ç§ã®è¨å®ããã¡ãªæ°ããããããã¼ã ï¼
ï¼ä»®æ³ãã¹ã¨url:ã®å
¼ãåãï¼ï¼
ãã¹ãï¼
æ°ãã¨ããªããã¦ããã¡ãã¯ãã°ãã°ããè¦ãæ§æã
/form/index.php ã /form/test ã§ãã³ã ãããããã¡ãã¯htdocsãå ±æã§ããã
/form: file.dir: /var/www/app/htdocs redirect: url: /form/index.php/ internal: YES status: 307
æå¤ãªãã¨ã«ãã¡ã ã£ããï¼è¿½è¨ï¼ã¨ã³ããªåé ã®éãã解決ãã¾ããï¼ï¼
ããã«ããã°ã
http://localhost:8080/form/index.php
ãã²ããããã/ã¨åãç»é¢ã«ãªã£ã¦ãã¾ã£ãã
ã¡ãªã¿ã«ä»¥ä¸ã®ããã«ãã¦ããã¡ã§ããã
ãã ããç´æ¥ãã¡ã¤ã«åã¾ã§æå®ããããä»åº¦ã¯ç®çã®phpãéããã
/form: file.dir: /var/www/app/htdocs/form redirect: url: /index.php/ internal: YES status: 307
ãã¼ã ï¼
ã©ããæãéãã«ãªããªããh2oã®ä»æ§ãªã®ããªã¨æã£ã¦ããã¾ãããã©ããªãã§ããããã
ãã¹ãï¼ã«ã¤ãã¦ã¯ããããªããããããªãã¢ããªã®è¼ãæ¹ãã奴ã¯æ» å¤ã«å± ãªããã俺ãæªãæ°ããããï¼ãã ããããã§ããã¨ãããå¬ããï¼
ãã ããã¹ãï¼ã«ã¤ãã¦ã¯ãããã¾ããªPHPã®ãµã¤ãã§ã¯ãã¾ã«ä½¿ãããæ§æãªã®ã§ãæ¯éåãã¦æ¬²ããã
追è¨ï¼ã¨ã³ããªåé ã®éãã解決ãã¾ããï¼
ã¾ã¨ã
- æ®éã®PHPã¢ããªã¯ãããªãåãï¼ã·ã³ãã«ï¼
- ãã£ãç ®ã®ãApacheã¹ã£ãããªã¢ããªã¯ã¾ã é£ãããã
H2Oã¯nginxã¨ããã¹ã¦ãfastcgiã¨pathï¼locationï¼ã®ä¸»å¾ãé転ãã¦ããã®ãé¢ç½ããæ¡å¼µåãè¦ã¦ç¹å®ã®fastcgiã®ç¹ãå
ï¼ããã§ã¯phpï¼ãç¹å®ããããApache likeãªæè¦ã§ããã楽ã ã
ããããnginxã¯æ¸ããã¨ãããã°ãã³ãã³æ¸ããã®ã§ãpathæ¯ã«PHPï¼ã®ãã¼ã¸ã§ã³ãè¨å®ãï¼ãå¤ãããã§ããã®ãè¯ããããããªãããã ã¾ãããããªéæ·±ãã®ã¯å°çã§ãããproxyã¨ãã¦å¤ç¨ãããã®ãèããã¨*4ãããã¯ããã§ã
ï¼ã¾ããphp5ã¨ãããããæ¡å¼µåãç¨æããã°ï¼ããã¾ãApacheçã ï¼ï¼H2Oã§ãä¼¼ããããªäºãã§ãããã§ããï¼
ã¨ããã§ãkazuhoããã®ãã³ã³ãã£ã°ãã·ã³ãã«ã«ãããããã«ããããã ï¼ãã¨ãããããè¯ãèããããã³ãã³ãããã¦ãã£ã¦ãæãããããã¨ã«ããPHPerã®è¿·æããããâ¦ã
ãã諦ãã¦ãh2o+phpãéãã¦ããã°ï¼proxyãã¦ããã°ï¼ãããã¡ãããï¼ã¨æã£ãï¼â¦ã ãâ¦ãããâ¦æã ã¯â¦ï¼ã
å¾ãh2oã¨å®å
¨ã«é¢ä¿ãªããã.htaccessã¯ãã£ã±ã便å©ã ã¨åèªèããã
è¤éæªå¥ã§èª°ãé©åã«ç解ã§ããªãmod_rewrite+.htaccessã ããã¨ã«ããå¤ã«ã´ããããµãåºããããã®ãã£ã¬ã¯ããªã®ä¸ã§å°ãããã¦ããã®ã¯PHPçã ã
ããããå ´å½ããçãªä¾¿å©ãã¼ã«ã¯å人çã«ã¯ç¡ãã¦ãè¯ããããªãã ããã ã§ç¾å®ã®éç¨ã§è¤æ°ã®ã¢ããªãä¹ãå§ããã¨ãh2oã§ãconfigã¯ä¼¸ã³ã¦ããããã§ãã£ããã§ãã¾ããnginx+php-fpmããæç¶èªã¿ãããã§ããã©ãï¼
h2oããã£ã¨æ£ããæ±ãããä¸çã«ãããããæ·±å¤ã®PHPerã§ãã£ãã
ãã¡ãããã¯ä»¥ä¸ã§ãããããããã°ç¶å ±ããå¾
ã¡ãã ããã
ç¶å ±ã§ãã¾ããã
*1:æ¥è¨ã ããä»æ¥ã¾ãã«ãã客å ããã³ã¹ãã«ããã宣åããã¦ããªããæ°åã«ãªã£ã¦ãããã¤ããã
*2:PHPã®å©ç¹ã§ãããå¿ã¿å«ãããæ¬ ç¹ã§ããã
*3:ãããnginx+fastcgiã ã¨ãassetã¾ã§å«ãã¦èããã¨ç°å¸¸ã«é¢åãªâ¦ãã ããããããããããã ï¼ä¿ºã¯ï¼ï¼
*4:äºä¾ã¨ãè¨è¨ã©ã¡ããå ãããããªãã