downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

INI 設定> <対話シェル
[edit] Last updated: Fri, 25 May 2012

view this page in

ビルトインウェブサーバー

PHP 5.4.0 から、CLI SAPI にはウェブサーバーの機能が組み込まれるようになりました。

このウェブサーバーは開発用としてのみ設計されたものであり、 実運用に使ってはいけません。

リクエストの処理はシーケンシャルに行います。

URI リクエストの処理は、PHP を開始した時点の作業ディレクトリから行われます。 -t オプションを使えば、ドキュメントルートを明示的に指定することができます。 URI リクエストにファイルが含まれない場合は、指定したディレクトリにある index.php あるいは index.html を返します。どちらも存在しない場合はレスポンスコード 404 を返します。

ウェブサーバーの開始時にコマンドラインで PHP ファイルを指定すると、 そのファイルをウェブサーバーの "ルーター" スクリプトとして使います。 このスクリプトは、各 HTTP リクエストの開始時に動きます。このスクリプトが FALSE を返すと、リクエストされたリソースをそのままの形式で返します。 それ以外の場合はスクリプトの出力をブラウザに返します。

以下にあげる拡張子のファイルについては、標準の MIME タイプを返します。 .css, .gif, .htm, .html, .jpe, .jpeg, .jpg, .js, .png, .svg, そして .txt。 このうち .htm と .svg については、PHP 5.4.4 以降で対応するようになりました。

例1 ウェブサーバーの起動

$ cd ~/public_html
$ php -S localhost:8000

ターミナルには次のように表示されます。

PHP 5.4.0 Development Server started at Thu Jul 21 10:43:28 2011
Listening on localhost:8000
Document root is /home/me/public_html
Press Ctrl-C to quit

http://localhost:8000/ と http://localhost:8000/myscript.html をリクエストした後のターミナルの表示は、 このようになります。

PHP 5.4.0 Development Server started at Thu Jul 21 10:43:28 2011
Listening on localhost:8000
Document root is /home/me/public_html
Press Ctrl-C to quit.
[Thu Jul 21 10:48:48 2011] ::1:39144 GET /favicon.ico - Request read
[Thu Jul 21 10:48:50 2011] ::1:39146 GET / - Request read
[Thu Jul 21 10:48:50 2011] ::1:39147 GET /favicon.ico - Request read
[Thu Jul 21 10:48:52 2011] ::1:39148 GET /myscript.html - Request read
[Thu Jul 21 10:48:52 2011] ::1:39149 GET /favicon.ico - Request read

例2 ドキュメントルートディレクトリを指定した起動

$ cd ~/public_html
$ php -S localhost:8000 -t foo/

ターミナルには次のように表示されます。

PHP 5.4.0 Development Server started at Thu Jul 21 10:50:26 2011
Listening on localhost:8000
Document root is /home/me/public_html/foo
Press Ctrl-C to quit

例3 ルータースクリプトの使用

この例では、画像ファイルをリクエストすればそのまま表示し、HTML ファイルをリクエストすると "Welcome to PHP" と表示します。

<?php
// router.php
if (preg_match('/\.(?:png|jpg|jpeg|gif)$/'$_SERVER["REQUEST_URI"])) {
    return 
false;    // リクエストされたリソースをそのままの形式で扱います。
} else { 
    echo 
"<p>Welcome to PHP</p>";
}
?>
$ php -S localhost:8000 router.php

例4 CLI ウェブサーバーを使っているかどうかのチェック

フレームワークのルータースクリプトを、開発中は CLI ウェブサーバーで使って その後は本番環境のウェブサーバーでも使うという例です。

<?php
// router.php
if (php_sapi_name() == 'cli-server') {
    
/* 静的コンテンツのルーティングをして false を返します */
}
/* 通常の index.php の処理を続きます */
?>
$ php -S localhost:8000 router.php

例5 未サポートのファイル形式の処理

CLI ウェブサーバーで対応していない MIME タイプの静的リソースを扱うには、このようにします。

<?php
// router.php
$path pathinfo($_SERVER["SCRIPT_FILENAME"]);
if (
$path["extension"] == "ogg") {
    
header("Content-Type: video/ogg");
    
readfile($_SERVER["SCRIPT_FILENAME"]);
}
else {
    return 
FALSE;
}
?>
$ php -S localhost:8000 router.php

例6 CLI ウェブサーバーへのリモートマシンからのアクセス

ウェブサーバーを、任意のインターフェイスからポート 8000 でアクセスできるようにするには、このようにします。

$ php -S 0.0.0.0:8000


add a note add a note User Contributed Notes ビルトインウェブサーバー
Stefano F. Rausch 09-Mar-2012 12:25
To develop / deploy websites in 3 stages, i.e. ( 1 ) locally, ( 2 ) with an access controlled dedicated beta / test website in the www and ( 3 ) the production site, you can have ( 1 ) & ( 2 ) using the same domain name - port free - as follows:

- look up the IP address of the beta.web.site

and edit the hosts file to reflect:

- <IP> beta.web.site
- 127.0.0.1 beta.web.site

Start the built-in web server to work locally with:

- sudo php -S beta.web.site:80

and just hit http://beta.web.site as usual. Switching back and forth between ( 1 ) and ( 2 ) is as easy as telling the php engine not to fake a server any more :) Nice!

Happy PHP'ing.

 
show source | credits | sitemap | contact | advertising | mirror sites