<?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>HattaraTecLog &#187; fastcgi</title>
	<atom:link href="http://teclog.hattara.info/?cat=71&#038;feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://teclog.hattara.info</link>
	<description>LinuxネタとかPHPネタとかの技術ログ</description>
	<lastBuildDate>Tue, 18 Nov 2014 12:15:20 +0000</lastBuildDate>
	<language>ja</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.8</generator>
	<item>
		<title>nginx(VirtualDocumentRootもどき）でfastcgi(php-fpm)が動かない</title>
		<link>http://teclog.hattara.info/?p=287</link>
		<comments>http://teclog.hattara.info/?p=287#comments</comments>
		<pubDate>Mon, 20 Dec 2010 11:39:26 +0000</pubDate>
		<dc:creator><![CDATA[hattara]]></dc:creator>
				<category><![CDATA[Cent]]></category>
		<category><![CDATA[fastcgi]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[nginx]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[WebServer]]></category>

		<guid isPermaLink="false">http://teclog.hattara.info/?p=287</guid>
		<description><![CDATA[現在以下のような方法で、VirtualDocumentRootもどきを実装していますが、 PHPのSCRIPT [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>現在以下のような方法で、VirtualDocumentRootもどきを実装していますが、<br />
PHPのSCRIPT_FILENAMEが正常に取得できないようなので、調査しました。</p>
<p>【変更前】</p>
<blockquote><p>
server {<br />
    listen       80;<br />
    server_name ~^(.*)\.hoge.com$;<br />
    if (!-d /home/hoge.com/$1/web) {<br />
        rewrite . http://hoge.com/ redirect;<br />
    }</p>
<p>    location / {<br />
        index  index.php;<br />
        root /home/hoge.com/$1/web;<br />
    }</p>
<p>    location ~ \.php$ {<br />
        fastcgi_pass 127.0.0.1:9000;<br />
        fastcgi_index index.php;<br />
        fastcgi_param SCRIPT_FILENAME /home/hoge.com/$1/web/$fastcgi_script_name;<br />
        fastcgi_param PATH_INFO $fastcgi_script_name;<br />
        include /etc/nginx/fastcgi_params;<br />
    }<br />
}
</p></blockquote>
<p>上記だと、正常に動きませんでした。<br />
原因としては、以下の部分の「$1」の部分に正しい値が入らないためのようでした。</p>
<blockquote><p>
        fastcgi_param SCRIPT_FILENAME /home/hoge.com/$1/web/$fastcgi_script_name;
</p></blockquote>
<p>色々試した結果、$1を別の変数として定義してあげれば動くことを確認しました。<br />
結果、以下のような内容になりました。</p>
<p>【変更後】</p>
<blockquote><p>
server {<br />
    listen       80;<br />
    server_name ~^(.*)\.hoge.com$;<br />
    set $subdomain $1;</p>
<p>    if (!-d /home/hoge.com/$subdomain/web) {<br />
        rewrite . http://hoge.com/ redirect;<br />
    }</p>
<p>    location / {<br />
        index  index.php;<br />
        root /home/hoge.com/$subdomain/web;<br />
    }</p>
<p>    location ~ \.php$ {<br />
        fastcgi_pass 127.0.0.1:9000;<br />
        fastcgi_index index.php;<br />
        fastcgi_param SCRIPT_FILENAME /home/hoge.com/$subdomain/web/$fastcgi_script_name;<br />
        fastcgi_param PATH_INFO $fastcgi_script_name;<br />
        include /etc/nginx/fastcgi_params;<br />
    }<br />
}
</p></blockquote>
<p>とりあえず上記の記述方法で正常動作しました。よかったよかった。</p>
<table cellpadding="0" cellspacing="0" border="0" style=" border:1px solid #ccc; width:170px;">
<tr style="border-style:none;">
<td style="vertical-align:top; border-style:none; padding:10px 10px 0pt;"><a href="http://px.a8.net/svt/ejp?a8mat=1NWEVP+1HL1ZU+249K+BWGDT&#038;a8ejpredirect=http%3A%2F%2Fwww.amazon.co.jp%2FNginx-Http-Server-Clement-Nedelcu%2Fdp%2F1849510865%253FSubscriptionId%253DAKIAJG4HK2PMU5Z4Q6YQ%2526tag%253Da8-affi-10084-22%2526linkCode%253Dxm2%2526camp%253D2025%2526creative%253D165953%2526creativeASIN%253D1849510865" target="_blank"><img border="0" alt="" src="http://ecx.images-amazon.com/images/I/51gax2MB8wL._SS160_.jpg" /></a></td>
</tr>
<tr style="border-style:none;">
<td style="font-size:12px; vertical-align:middle; border-style:none; padding:10px;">
<p style="padding:0; margin:0;"><a href="http://px.a8.net/svt/ejp?a8mat=1NWEVP+1HL1ZU+249K+BWGDT&#038;a8ejpredirect=http%3A%2F%2Fwww.amazon.co.jp%2FNginx-Http-Server-Clement-Nedelcu%2Fdp%2F1849510865%253FSubscriptionId%253DAKIAJG4HK2PMU5Z4Q6YQ%2526tag%253Da8-affi-10084-22%2526linkCode%253Dxm2%2526camp%253D2025%2526creative%253D165953%2526creativeASIN%253D1849510865" target="_blank">Nginx Http Server</a></p>
<p style="color:#cc0000; font-weight:bold; margin-top:10px;">新品価格<br/>￥3,964<span style="font-weight:normal;">から</span><br/><span style="font-size:10px; font-weight:normal;">(2010/12/20 19:36時点)</span></p>
</td>
</tr>
</table>
<p><img border="0" width="1" height="1" src="http://www19.a8.net/0.gif?a8mat=1NWEVP+1HL1ZU+249K+BWGDT" alt=""></p>
]]></content:encoded>
			<wfw:commentRss>http://teclog.hattara.info/?feed=rss2&#038;p=287</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Nginx(virtualhost)+php-fpmでPHPが動作しない（凡ミス編）</title>
		<link>http://teclog.hattara.info/?p=284</link>
		<comments>http://teclog.hattara.info/?p=284#comments</comments>
		<pubDate>Mon, 20 Dec 2010 10:39:17 +0000</pubDate>
		<dc:creator><![CDATA[hattara]]></dc:creator>
				<category><![CDATA[Cent]]></category>
		<category><![CDATA[fastcgi]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[nginx]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[WebServer]]></category>

		<guid isPermaLink="false">http://teclog.hattara.info/?p=284</guid>
		<description><![CDATA[Nginxでvirtualhostを切ってる状態でphp-fpmを動かそうと頑張っていたんですが、 何故か動か [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Nginxでvirtualhostを切ってる状態でphp-fpmを動かそうと頑張っていたんですが、<br />
何故か動かない。<br />
あちこちのサイトで説明されてる内容をそのままやって動かない。</p>
<p>一番下にある洋書のNginxの書籍に書いてある例をやっても動かない。<br />
なんでか悩んでいたんですが、平凡なミスであることがわかりました。</p>
<p>元々の手順としては、nginxが導入されている環境に、</p>
<p>1.PHPとPHP-FPMをyumで簡易インストールする。</p>
<blockquote><p>
yum &#8211;enablerepo=remi install php php-fpm
</p></blockquote>
<p>2.一応nginxのメインの設定にphpの設定を書いておく</p>
<blockquote><p>
vi /etc/nginx/nginx.conf</p>
<p>        # Add<br />
        location ~ .php$ {<br />
            fastcgi_pass 127.0.0.1:9000;<br />
            fastcgi_index index.php;<br />
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;<br />
            include /etc/nginx/fastcgi_params;<br />
        }
</p></blockquote>
<p>3.自分が作成したVirtualHost用の設定ファイル(/etc/nginx/conf.d/aaa.conf)にも追加する</p>
<blockquote><p>
vi /etc/nginx/conf.d/aaa.conf</p>
<p>server {<br />
    listen       80;<br />
    server_name  test.hoge.com;</p>
<p>    location / {<br />
        root   /home/test.hoge.com/web;<br />
        index  index.php;<br />
    }<br />
    location ~ .php$ {<br />
        fastcgi_pass 127.0.0.1:9000;<br />
        fastcgi_index index.php;<br />
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;<br />
        include /etc/nginx/fastcgi_params;<br />
    }<br />
}
</p></blockquote>
<p>4.php-fpmの起動ユーザーをnginxユーザにしてみる　（※必要に応じて変更するかも）</p>
<blockquote><p>
vi /etc/php-fpm.d/www.conf<br />
        user = nginx<br />
        group = nginx
</p></blockquote>
<p>5．サーバ起動時に自動起動するように起動ファイルのリンク作成</p>
<blockquote><p>
ln -s /etc/rc.d/init.d/php-fpm /etc/rc.d/rc3.d/S87php-fpm
</p></blockquote>
<p>6．サーバ起動させてみる。</p>
<blockquote><p>
/etc/rc.d/init.d/php-fpm start
</p></blockquote>
<p>という手順をしたんですが、htmlファイルは問題なく動作するのですが、phpファイルがうごかない。<br />
真っ白になったり、ダウンロードになったり・・意味がわからない。</p>
<p>結論からいうと、ドキュメントルートの設定がおかしくて、表示したいファイルに到達していなかったという事だった。<br />
どこがダメかというと、上記の「3」に問題があったようだ。</p>
<p>具体的には、</p>
<blockquote><p>
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
</p></blockquote>
<p>ここが問題で、document_root変数の下のfastcgi_script_name変数のファイルをkickするわけだが、<br />
$document_root変数の値が、どうも正しく入っていないようだ。<br />
たぶん、メインの/etc/nginx/nginx.confのrootの値を持ってたりするんじゃなかろうかと思うわけだ。</p>
<p>ということで、以下のように変更して、復旧しました。</p>
<blockquote><p>
        fastcgi_param SCRIPT_FILENAME /home/test.hoge.com/web/$fastcgi_script_name;
</p></blockquote>
<table cellpadding="0" cellspacing="0" border="0" style=" border:1px solid #ccc; width:170px;">
<tr style="border-style:none;">
<td style="vertical-align:top; border-style:none; padding:10px 10px 0pt;"><a href="http://px.a8.net/svt/ejp?a8mat=1NWEVP+1HL1ZU+249K+BWGDT&#038;a8ejpredirect=http%3A%2F%2Fwww.amazon.co.jp%2FNginx-Http-Server-Clement-Nedelcu%2Fdp%2F1849510865%253FSubscriptionId%253DAKIAJG4HK2PMU5Z4Q6YQ%2526tag%253Da8-affi-10084-22%2526linkCode%253Dxm2%2526camp%253D2025%2526creative%253D165953%2526creativeASIN%253D1849510865" target="_blank"><img border="0" alt="" src="http://ecx.images-amazon.com/images/I/51gax2MB8wL._SS160_.jpg" /></a></td>
</tr>
<tr style="border-style:none;">
<td style="font-size:12px; vertical-align:middle; border-style:none; padding:10px;">
<p style="padding:0; margin:0;"><a href="http://px.a8.net/svt/ejp?a8mat=1NWEVP+1HL1ZU+249K+BWGDT&#038;a8ejpredirect=http%3A%2F%2Fwww.amazon.co.jp%2FNginx-Http-Server-Clement-Nedelcu%2Fdp%2F1849510865%253FSubscriptionId%253DAKIAJG4HK2PMU5Z4Q6YQ%2526tag%253Da8-affi-10084-22%2526linkCode%253Dxm2%2526camp%253D2025%2526creative%253D165953%2526creativeASIN%253D1849510865" target="_blank">Nginx Http Server</a></p>
<p style="color:#cc0000; font-weight:bold; margin-top:10px;">新品価格<br/>￥3,964<span style="font-weight:normal;">から</span><br/><span style="font-size:10px; font-weight:normal;">(2010/12/20 19:36時点)</span></p>
</td>
</tr>
</table>
<p><img border="0" width="1" height="1" src="http://www19.a8.net/0.gif?a8mat=1NWEVP+1HL1ZU+249K+BWGDT" alt=""></p>
]]></content:encoded>
			<wfw:commentRss>http://teclog.hattara.info/?feed=rss2&#038;p=284</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
