<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Virtual Machine &#8211; Mach3.laBlog</title>
	<atom:link href="https://blog.mach3.jp/tag/virtual-machine/feed" rel="self" type="application/rss+xml" />
	<link>https://blog.mach3.jp</link>
	<description></description>
	<lastBuildDate>Fri, 13 Jun 2014 01:52:47 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9</generator>
	<item>
		<title>PHPのビルトインウェブサーバーを活用して動作確認を捗らす話</title>
		<link>https://blog.mach3.jp/2014/06/13/php54-builtin-server.html</link>
		
		<dc:creator><![CDATA[mach3]]></dc:creator>
		<pubDate>Fri, 13 Jun 2014 01:52:47 +0000</pubDate>
				<category><![CDATA[Laboratory]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[Browser]]></category>
		<category><![CDATA[Internet Explorer]]></category>
		<category><![CDATA[mod_rewrite]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Virtual Machine]]></category>
		<category><![CDATA[VM]]></category>
		<guid isPermaLink="false">http://blog.mach3.jp/?p=3844</guid>

					<description><![CDATA[PHPのビルトインウェブサーバーについては以前も軽く触れたのですが、 今回はもう少し細かく噛み砕きつつ、仮想マシンからホストOS（Win/Mac）のサーバーにアクセスして動作確認を捗らせてみます。 まえおき やりたいこと [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>PHPのビルトインウェブサーバーについては<a href="https://blog.mach3.jp/2012/09/21/check-html-with-simple-server.html">以前も軽く触れた</a>のですが、 今回はもう少し細かく噛み砕きつつ、仮想マシンからホストOS（Win/Mac）のサーバーにアクセスして動作確認を捗らせてみます。</p>



<figure class="wp-block-image"><img decoding="async" src="https://lh6.googleusercontent.com/-EOcn2k67Ig4/U5fvKKdSu3I/AAAAAAAACfo/qKn6R_tP4-o/s400/2014-0611-00.png" alt="PHPのビルトインウェブサーバーをもっと活用して動作確認を捗らす話"/></figure>



<p></p>



<span id="more-3844"></span>



<h2 class="wp-block-heading">まえおき</h2>



<h3 class="wp-block-heading">やりたいこと</h3>



<ul class="wp-block-list">
<li>仮想マシン（Windows）で、ローカルで制作中のWebサイトの動作確認をしたい。</li>



<li>せっかくなのでhttpアクセスで閲覧したい。</li>



<li>でもどこかにアップロードしたりローカルサーバ構築したりするのはめんどう。</li>



<li>せっかちなのでDropboxの同期をまっている心の余裕がない。</li>
</ul>



<figure class="wp-block-image"><img decoding="async" src="https://lh5.googleusercontent.com/-erUouvs_1s0/U5fvKWgAgEI/AAAAAAAACfo/TT13XDUstMw/s500/2014-0611-01.png" alt="やりたいこと"/></figure>



<p></p>



<p>こういう時にはPHPビルトインウェブサーバーが簡単でおすすめです。</p>



<ul class="wp-block-list">
<li>好きなディレクトリをドキュメントルートにしてコマンド一発で即時に簡易Webサーバが立ち上がります。</li>



<li>ログがターミナルに流れるのでわかりやすく、どことなくテンションがあがります。</li>



<li>ちょっとしたPHPのスクリプトを確認できるオマケつき。</li>



<li>ルータースクリプトも使える。</li>
</ul>



<h3 class="wp-block-heading">別にやりたくないこと</h3>



<ul class="wp-block-list">
<li>PHPの動作確認はちゃんと仮想マシン上のLinux等の開発環境で行います。<br /><br />（できるだけ本番環境にあわせてチェックを行いたいですし、 PHPを弄る様な方ならば素直にLinux環境のVMで環境構築した方が断然楽だと思います。）</li>
</ul>



<h2 class="wp-block-heading">ビルトインウェブサーバーの使い方</h2>



<figure class="wp-block-image"><img decoding="async" src="https://lh3.googleusercontent.com/-1dPYOnf5kTU/U5fwS_jMYDI/AAAAAAAACf8/EAFwnJWQfU8/s500/2014-0611-02.png" alt="PHPビルトインウェブサーバー"/></figure>



<p><a href="http://www.php.net/manual/ja/features.commandline.webserver.php">  </a></p>



<h3 class="wp-block-heading">PHP5.4+ の導入</h3>



<p>ここでは詳しく触れませんが、ビルトインウェブサーバーを使用するにはPHP5.4以降が必要です。 Windowsでもすぐに動くバイナリが配布されていますし、Macならmacportsやhomebrewで導入も容易だと思います。</p>



<ul class="wp-block-list">
<li><a href="http://windows.php.net/download/">PHP For Windows: Binaries and sources Releases</a></li>



<li><a href="http://www.macports.org/">The MacPorts Project &#8212; Home</a></li>



<li><a href="http://brew.sh/index_ja.html">Homebrew — OS X用パッケージマネージャー</a></li>
</ul>



<h3 class="wp-block-heading">ビルトインウェブサーバーの起動</h3>



<ul class="wp-block-list">
<li><a href="http://www.php.net/manual/ja/features.commandline.webserver.php">PHP: ビルトインウェブサーバー &#8211; Manual</a></li>
</ul>



<p>詳しくは上のURLにある通りです。<code>php</code> コマンドに <code>-S</code> オプションを渡す事でウェブサーバーを起動することができます。 その他オプションの書式は下記のようになります。</p>


<pre class="wp-block-code"><span><code class="hljs language-xml">$ php -S <span class="hljs-tag">&lt;<span class="hljs-name">ホスト名</span>&gt;</span>:<span class="hljs-tag">&lt;<span class="hljs-name">ポート番号</span>&gt;</span> -t <span class="hljs-tag">&lt;<span class="hljs-name">ドキュメントルートのパス</span>&gt;</span>
</code></span></pre>


<p>例えば、localhostの8080番ポートで、 &#8220;public_html&#8221; ディレクトリをドキュメントルートとしてWebサーバを立ち上げる場合は次のようになります。</p>


<pre class="wp-block-code"><span><code class="hljs">$ php -S localhost:8080 -t public_html
</code></span></pre>


<h3 class="wp-block-heading">仮想マシンから確認する</h3>



<p>ホスト名がlocalhostでは仮想環境からアクセスする事ができません。 そこで、次のようにホスト名を <strong>&#8220;0.0.0.0&#8221;</strong> にして起動する事で外部から接続できるようになります。</p>


<pre class="wp-block-code"><span><code class="hljs">$ php -S 0.0.0.0:8080 -t public_html
</code></span></pre>


<p>例えばサーバを起動するマシンのローカルIPアドレスが 192.168.10.3 などだった場合、 仮想マシンのゲストOSから &#8220;http://192.168.10.3:8080&#8221; へアクセスすれば直接動作確認をする事ができるようになります。 （これはあくまで一例で、アクセス先のアドレスは仮想マシンのネットワーク設定によって様々なので注意しましょう）</p>



<p>IEの確認には、Microsoft提供の <a href="http://modern.ie/ja-jp">modern.ie</a> が便利です。 昔はVrtualPC用のイメージだけだったのですが、modern.ie になってからはVirtualBox、VMWare、Parallels等、 様々なプラットフォーム用の仮想マシンイメージを配布しています。</p>



<ul class="wp-block-list">
<li><a href="http://modern.ie/ja-jp">Internet Explorer のテストが簡単に | modern.IE</a></li>
</ul>



<h3 class="wp-block-heading">ルータースクリプトを使う</h3>



<p>このビルトインウェブサーバーの素敵なポイントの一つに、<strong>ルータースクリプト</strong> を簡単に使用出来るという利点があります。 ざっくり言うと、Apacheのmode_rewriteのような処理がPHPスクリプトを通じて行えるようになる代物です。</p>



<p>例えばこれは、Wordpress の .htaccess にある mod_rewrite の記述です。 この記述のおかげで WordPress はURLをデザインする事が出来るわけですね。</p>


<pre class="wp-block-code"><span><code class="hljs language-xml"># BEGIN WordPress
<span class="hljs-tag">&lt;<span class="hljs-name">IfModule</span> <span class="hljs-attr">mod_rewrite.c</span>&gt;</span>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php &#91;L]
<span class="hljs-tag">&lt;/<span class="hljs-name">IfModule</span>&gt;</span>
# END WordPress
</code></span></pre>


<p>ここで記述されているルールは非常に単純で、 「アクセスされたURLにあたるファイルやディレクトリが存在しない場合は、index.phpでレスポンスを返す」だけです。 同様の処理をPHPのルータースクリプトで記述してみましょう。</p>


<pre class="wp-block-code"><span><code class="hljs language-xml"><span class="php"><span class="hljs-meta">&lt;?php</span>
<span class="hljs-comment">/* _router.php */</span>
<span class="hljs-keyword">if</span>(file_exists($_SERVER&#91;<span class="hljs-string">"REQUEST_FILENAME"</span>])){
    <span class="hljs-comment">// false を返すとリクエストされたリソースをそのまま返します</span>
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">false</span>;
} <span class="hljs-keyword">else</span> {
    <span class="hljs-keyword">require</span> <span class="hljs-string">"index.php"</span>;
}
</span></code></span></pre>


<p>あとは、この内容を記述した &#8220;_router.php&#8221; をコマンドに渡すだけです。</p>


<pre class="wp-block-code"><span><code class="hljs">$ php -S 0.0.0.0:8080 -t public_html public_html/_router.php
</code></span></pre>


<p>mod_rewrite のような処理を簡単にテストしたい場合や、 History.pushState を使った構成で制作を進める時などに重宝しています。</p>



<h2 class="wp-block-heading">Gruntタスクで起動する</h2>



<p>Gruntを使って開発をされている方は、<a href="https://github.com/gruntjs/grunt-contrib-connect">grunt-contrib-connect</a> のような感じで開発用サーバを起動したいと考えるでしょう。 ちょうどそのようなプラグインが公開されていたので、ここはひとつ活用させて頂きます。</p>



<ul class="wp-block-list">
<li><a href="https://github.com/sindresorhus/grunt-php">sindresorhus/grunt-php @ Github</a></li>
</ul>



<p>タスク設定例:</p>


<pre class="wp-block-code"><span><code class="hljs language-css"><span class="hljs-selector-tag">grunt</span><span class="hljs-selector-class">.initConfig</span>({
    <span class="hljs-attribute">php</span>: {
        dist: {
            options: {
                port: <span class="hljs-number">8080</span>,
                hostname: <span class="hljs-string">"0.0.0.0"</span>,
                base: <span class="hljs-string">"public_html"</span>,
                router: <span class="hljs-string">"_router.php"</span>,
                keepalive: true
            }
        }
    }
});
</code></span></pre>


<p>router のパスは base からの相対パスになります。</p>



<p>JavaScriptやCSSのコンパイル等で <strong>watch</strong> タスクを利用している場合は、サーバーの起動と同時に行えるようにしたいですね。 その際には <code>keepalive</code> を <strong>false</strong> にし、次のように watch の前に登録して起動します。</p>


<pre class="wp-block-code"><span><code class="hljs language-javascript"><span class="hljs-comment">// `$ grunt dev` で起動</span>
grunt.registerTask(<span class="hljs-string">"dev"</span>, &#91;<span class="hljs-string">"php:dist"</span>, <span class="hljs-string">"watch"</span>]);
</code></span></pre>


<p>Grunt で php を扱うプラグインにはこの他にも <a href="https://github.com/ddprrt/connect-php">ddprrt/connect-php</a> があります。 こちらは grunt-conrib-connect のミドルウェアとして動かすタイプのライブラリで、 5.4+ でなくても動くかわりに、ルータースクリプト等は使えなさそうです。gruntベースで自前で書けという事でしょう。</p>



<p>また、プラグインを探していた当初、<strong>grunt-php</strong> が見つからなかった為、 ほぼ同じような動作をする物を自前で書いて使っていたのは<a href="https://github.com/mach3/grunt-phps">秘密</a>です。 私が書いた物よりも grunt-php の方が優秀そうなので、何も書かなかった事にしました。 grunt-php に無くて自前の物にある機能といえばアクセスログの出力ぐらいで、オプションの名前などもほとんど同じでした。 どちらも grunt-contrib-connect を参考にしているので当たり前ですね。</p>



<h2 class="wp-block-heading">まとめ</h2>



<p>単純に確認用のサーバを起動したいのならば grunt-contrib-connect で十分なのですが、 URLのリライト等をしたい場合などはPHPビルトインウェブサーバーの方が楽に設定出来る気がします。 Gruntを使わない人でも簡単に扱えるは大きなメリットですね。</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
