<?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>bluegold &#187; fastcgi</title>
	<atom:link href="http://blog.bluegold.me/tag/fastcgi/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.bluegold.me</link>
	<description>OpenSolaris と MacBook で自宅ネットワークを構築するメモ</description>
	<lastBuildDate>Thu, 26 Aug 2010 14:38:53 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>nginx を SMF で管理する</title>
		<link>http://blog.bluegold.me/2010/08/management-nginx-with-smf/</link>
		<comments>http://blog.bluegold.me/2010/08/management-nginx-with-smf/#comments</comments>
		<pubDate>Mon, 02 Aug 2010 16:36:59 +0000</pubDate>
		<dc:creator>bg</dc:creator>
				<category><![CDATA[OpenSolaris]]></category>
		<category><![CDATA[fastcgi]]></category>
		<category><![CDATA[nginx]]></category>
		<category><![CDATA[opensolaris]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://blog.bluegold.me/?p=500</guid>
		<description><![CDATA[nginx や PHP の fast-cgi のプロセスを OpenSolaris の SMF で管理するための manifest の紹介。]]></description>
			<content:encoded><![CDATA[<p>もう1年以上前に書いていた下書きをようやく公開。</p>
<p><a href="/2008/11/nginx-php-fastcgi/">前回（と言っても1年半前か）</a> までで nginx と PHP の fast-cgi のビルドと設定が終わったので、OS の起動時に自動的に起動するようにサービスとして登録を行います。</p>
<p>OpenSolaris ではサービスの管理に <a href="http://en.wikipedia.org/wiki/Service_Management_Facility">SMF</a> という仕組みを使用しています。SMF はサービスの依存関係を記述できたり、サービスの起動を監視して自動的に再起動してくれたりと、Linux などで使われている /etc/init.d の rc スクリプトに比べるとメリットがありますが、コマンドが独自だったり、自分で SMF にサービスを登録するには manifest と呼ばれる XML ファイルを書く必要があるので、慣れるまではハードルがあります。（この辺は Mac OS X の launchd と似ている。）</p>
<p><span id="more-500"></span></p>
<p> 以下が、私が nginx を管理するために作成した nginx.xml です。</p>
<pre class="brush: xml;">
&lt;?xml version='1.0'?&gt;
&lt;!DOCTYPE service_bundle SYSTEM '/usr/share/lib/xml/dtd/service_bundle.dtd.1'&gt;
&lt;service_bundle type='manifest' name='export'&gt;
  &lt;service name='network/nginx' type='service' version='0'&gt;
    &lt;create_default_instance enabled='true'/&gt;
    &lt;single_instance/&gt;
    &lt;dependency name='fs' grouping='require_all' restart_on='none' type='service'&gt;
      &lt;service_fmri value='svc:/system/filesystem/local'/&gt;
    &lt;/dependency&gt;
    &lt;dependency name='net' grouping='require_all' restart_on='none' type='service'&gt;
      &lt;service_fmri value='svc:/network/loopback'/&gt;
    &lt;/dependency&gt;
    &lt;exec_method name='start' type='method' exec='/usr/local/sbin/nginx -c /usr/local/conf/nginx.conf' timeout_seconds='60'&gt;
      &lt;method_context working_directory='/usr/local/logs'&gt;
        &lt;method_credential user='root' group='root'/&gt;
        &lt;method_environment&gt;
          &lt;envvar name='PATH' value='/usr/bin:/bin:/usr/local/bin'/&gt;
        &lt;/method_environment&gt;
      &lt;/method_context&gt;
    &lt;/exec_method&gt;
    &lt;exec_method name='stop' type='method' exec=':kill' timeout_seconds='60'&gt;
      &lt;method_context/&gt;
    &lt;/exec_method&gt;
  &lt;/service&gt;
&lt;/service_bundle&gt;
</pre>
<p>このファイルを以下のように SMF に登録します。</p>
<pre class="bq">
# svccfg -v import nginx.xml
</pre>
<p>同様に PHP の FastCGI のプロセスも SMF に登録します。</p>
<pre class="brush: xml;">
&lt;?xml version=&quot;1.0&quot;?&gt;
&lt;!DOCTYPE service_bundle SYSTEM &quot;/usr/share/lib/xml/dtd/service_bundle.dtd.1&quot;&gt;
&lt;service_bundle type=&quot;manifest&quot; name=&quot;php-fcgi&quot;&gt;
 &lt;service name=&quot;network/php-fcgi&quot; type=&quot;service&quot; version=&quot;0&quot;&gt;
   &lt;create_default_instance enabled=&quot;true&quot;/&gt;
   &lt;single_instance/&gt;

   &lt;dependency name=&quot;net&quot; grouping=&quot;require_all&quot; restart_on=&quot;none&quot; type=&quot;service&quot;&gt;
     &lt;service_fmri value=&quot;svc:/network/loopback&quot;/&gt;
   &lt;/dependency&gt;

   &lt;exec_method name=&quot;start&quot; type=&quot;method&quot; exec=&quot;/usr/local/sbin/php-fcgi.sh&quot; timeout_seconds=&quot;60&quot;&gt;
     &lt;method_context working_directory=&quot;/usr/local/www&quot;&gt;
      &lt;method_credential user=&quot;php&quot; group=&quot;webservd&quot; privileges=&quot;basic,net_privaddr&quot;/&gt;
      &lt;!--method_credential user=&quot;root&quot; group=&quot;root&quot; /--&gt;
       &lt;method_environment&gt;
         &lt;envvar name=&quot;PATH&quot; value=&quot;/usr/php/5.2/bin:/usr/local/sbin:/usr/bin:/bin&quot; /&gt;
       &lt;/method_environment&gt;
     &lt;/method_context&gt;
   &lt;/exec_method&gt;

    &lt;exec_method name='stop' type='method' exec=':kill' timeout_seconds='60'&gt;
      &lt;method_context/&gt;
    &lt;/exec_method&gt;

 &lt;/service&gt;
&lt;/service_bundle&gt;
</pre>
<p>この xml も同じように svccfg コマンドで登録します。</p>
<p>起動用の shell スクリプトは<a href="http://drupal.org/node/110224">こちらのサイト</a>のものを、ほぼそのまま使っています。</p>
<pre class="brush: bash;">
#!/bin/bash

LC_ALL=ja_JP.UTF-8

#FastCGI Webserver path
FCGI_WEB_SERVER_ADDRS=&quot;127.0.0.1&quot;

## ABSOLUTE path to the PHP binary
PHPFCGI=&quot;/usr/php/bin/php-cgi&quot;

## tcp-port to bind on
FCGIPORT=&quot;9000&quot;

## IP to bind on
FCGIADDR=&quot;127.0.0.1&quot;

## number of PHP children to spawn
PHP_FCGI_CHILDREN=5

## number of request before php-process will be restarted
PHP_FCGI_MAX_REQUESTS=1000

# allowed environment variables sperated by spaces
ALLOWED_ENV=&quot;PATH&quot;

## if this script is run as root switch to the following user
USERID=webservd

################## no config below this line

ALLOWED_ENV=&quot;$ALLOWED_ENV PHP_FCGI_CHILDREN&quot;
ALLOWED_ENV=&quot;$ALLOWED_ENV PHP_FCGI_MAX_REQUESTS&quot;
ALLOWED_ENV=&quot;$ALLOWED_ENV FCGI_WEB_SERVER_ADDRS&quot;

if test x$UID = x0; then
  EX=&quot;/bin/su -m -c \&quot;$PHPFCGI -q -b $FCGIADDR:$FCGIPORT\&quot; $USERID&quot;
else
  EX=&quot;$PHPFCGI -b $FCGIADDR:$FCGIPORT&quot;
fi

echo $EX

# copy the allowed environment variables
E=

for i in $ALLOWED_ENV; do
  E=&quot;$E $i=${!i}&quot;
done

# clean environment and set up a new one
nohup env - $E sh -c &quot;$EX&quot; &amp;
</pre>
<p>サービスが SMF に登録できれば、svcadm コマンドで起動停止を切り替える事ができます。</p>
<h4>起動</h4>
<pre class="bq">
# svcadm enable nginx
# svcadm enable php-fastcgi
</pre>
<h4>停止</h4>
<pre class="bq">
# svcadm disable nginx
# svcadm disable php-fastcgi
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.bluegold.me/2010/08/management-nginx-with-smf/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mercurial: nginx で hgwebdir.fcgi のセットアップ</title>
		<link>http://blog.bluegold.me/2010/02/setup-mercurial-hgwebdir-fcgi-on-nginx/</link>
		<comments>http://blog.bluegold.me/2010/02/setup-mercurial-hgwebdir-fcgi-on-nginx/#comments</comments>
		<pubDate>Sun, 07 Feb 2010 15:04:09 +0000</pubDate>
		<dc:creator>bg</dc:creator>
				<category><![CDATA[技術]]></category>
		<category><![CDATA[fastcgi]]></category>
		<category><![CDATA[mercurial]]></category>
		<category><![CDATA[nginx]]></category>
		<category><![CDATA[opensolaris]]></category>

		<guid isPermaLink="false">http://blog.bluegold.me/?p=379</guid>
		<description><![CDATA[Mercurial の hgwebdir.fcgi を nginx で使う際の設定例]]></description>
			<content:encoded><![CDATA[<h2>Mercurial 付属の hgwebdir.fcgi を nginx で動かすメモ</h2>
<p>CVS（やSubversion）をバージョン管理システムに使っていた頃はリポジトリの情報を見るのに<a href="http://www.viewvc.org/">ViewVC</a>を使っていましたが、ViewVCはMercurialをサポートしていません。Mercurial にはウェブ用の管理画面(hgweb/hgwebdir)が付属しているので、別のツールを使うニーズが少ないんでしょうね。</p>
<p>Mercurial のリポジトリ毎に対応する Redmine のプロジェクトが存在するので、そちらを見ればおおよその事は分かるのですが、Redmineのリポジトリブラウザはブランチを意識して作られていないので不便といえば不便です。
</p>
<p>hgwebの事は Mercurial への移行を検討していたときにも調べていたんですが、Django, flup, setuptools, ez_setup.py, easy_install, .. とPythonに不慣れな人間にはなじみのない単語が続々と出てきたので、ちょっと敬遠していましたが、ちょっと時間が取れたのでチャレンジしてみました。</p>
<p><span id="more-379"></span></p>
<p>hgwebdir のセットアップをする前に必要なソフトウェアのインストールを行います。環境は OpenSolaris 2009.06 で、可能な限り pkg を使ってインストールしていきます。インストールしたのは以下のパッケージです。</p>
<pre class="bq">
SUNWPython26
SUNWPython26-extra
SUNWpython26-setuptools
</pre>
</p>
<p>OpenSolaris 2009.06 のパッケージ管理システムにも Mercurial はありますが、バージョンが 1.1.2 と古く、クライアントの環境と合わないので、最新バージョンを easy_install からインストールします。</p>
<pre class="brush: bash;">
$ pfexec easy_install Mercurial
$ hg --version
Mercurial Distributed SCM (version 1.4.2)

Copyright (C) 2005-2009 Matt Mackall &lt;mpm@selenic.com&gt; and others
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
</pre>
</p>
<p>次に flup が必要だそうなのでインストールします。</p>
<pre class="brush: bash;">
$ pfexec easy_install flup
</pre>
</p>
<p>hgwebdir を FastCGI で動作させるのにはいろいろな方法があるらしいのですが、Python特有のやり方はよく分からないので、lighttpd の <a href="http://redmine.lighttpd.net/projects/spawn-fcgi">spawn-fcgi</a>を使いました。インストールは以下の通り簡単です。</p>
<pre class="brush: bash;">
$ wget http://www.lighttpd.net/download/spawn-fcgi-1.6.3.tar.gz
$ gzip -dc spawn-fcgi-1.6.3.tar.gz | tar xvf -
$ cd pawn-fcgi-1.6.3
$ ./configure
$ make
$ pfexec make install
</pre>
</p>
<p>ここまででようやく下準備がおわりました。<br />
hgwebdir.fcgi は /usr/demo/mercurial/hgwebdir.fcgi をコピーして使っています。<br />
UTF-8 で表示するために、HGENCODING の設定を行う部分のコメントをはずしています。</p>
<pre class="bq">
os.environ["HGENCODING"] = "UTF-8"
</pre>
</p>
<p>hgweb.config は単純です。</p>
<pre class="bq">
[paths]
/ = /path/to/repos/*
</pre>
<p>これを以下のようなスクリプトで起動しています。</p>
<pre class="bq">
#!/bin/sh

LANG=ja_JP.UTF-8
export LANG

FCGI=/opt/local/bin/spawn-fcgi
exec $FCGI -s /tmp/.fcgi_hgwebdir -G webserved -M 0660 ./hgwebdir.fcgi
</pre>
</p>
<p>nginx の設定ファイルは以下のようになりました。<br />
初めて try_files を使ってみましたが、設定がとてもシンプルになりますね。<br />
hgwebdir は PATH_INFO を利用してブラウズするリポジトリを探すようなので、その部分の置き換えにちょっと手を入れています。</p>
<pre class="bq">
server {
    listen       80;
    server_name  hg.example.com;
    root   /path/to/public;
    index  index.html;

    access_log  /var/log/nginx/hg-access.log  main;

    location / {
        try_files $uri @hgweb;
    }

    location @hgweb {
        fastcgi_pass   unix:/tmp/.fcgi_hgwebdir;
        fastcgi_read_timeout    5m;
        set $path_info "";
        if ($fastcgi_script_name ~ "^/(.*)$") {
            set $path_info $1;
        }
        fastcgi_param PATH_INFO $path_info;
    }
}
</pre></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.bluegold.me/2010/02/setup-mercurial-hgwebdir-fcgi-on-nginx/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>nginx のバージョンアップ</title>
		<link>http://blog.bluegold.me/2009/06/nginx-update-0759/</link>
		<comments>http://blog.bluegold.me/2009/06/nginx-update-0759/#comments</comments>
		<pubDate>Fri, 05 Jun 2009 16:57:50 +0000</pubDate>
		<dc:creator>bg</dc:creator>
				<category><![CDATA[技術]]></category>
		<category><![CDATA[fastcgi]]></category>
		<category><![CDATA[nginx]]></category>

		<guid isPermaLink="false">http://blog.bluegold.me/?p=184</guid>
		<description><![CDATA[nginx を 0.6系から0.7系にバージョンアップする手順について。]]></description>
			<content:encoded><![CDATA[<p><img src="http://wp-cdn.bluegold.me/2008/10/nginx-small.png" width="150" height="50" alt="" class="title_img" /></p>
<p><a href="http://nginx.net/">nginx</a> のバージョン 0.7 系が stable になっていたので、サーバソフトウェアのアップデートを行いました。（久しぶりのブログの更新は OpenSolaris 2009.06 について書こうと思ってたんですが、これはまた後で。。。）
</p>
<p>インストールしたのは nginx 0.7.59 です。<a href="http://forum.nginx.org/read.php?2,2335">リリースのアナウンス</a>が 25 May 2009 なので、2週間くらい前に出たばかりだったようです。 0.6系と0.7系の間の変更点についてまとまっている資料を探したのですが、見つかりませんでした。</p>
<p>Change Log を見るとかなりの修正箇所があるようですが、大きくまとめると以下の機能が増えているようです。</p>
<pre class="brush: bash;">
*) caching of proxied and FastCGI servers;
*) &quot;try_files&quot; directive;
*) the &quot;location&quot; and &quot;server_name&quot; directives support captures in regular expressions;
*) XLST and image filters;
*) a preliminary IPv6 support;
*) nginx/Windows.
</pre>
<p>WindowsサポートとIPv6、あまり関係なさそう。。。
</p>
<p><span id="more-184"></span></p>
<p>
try_filesはファイルを探す順番を指定するディレクティブで、例えば「あるファイルが存在する場合はメンテナンス画面を表示する」ような場合、今までは if 文をいくつか書いて条件分岐させていたのをすっきりと記述することが出来るようです。設定例を見るといろいろな応用が出来るようです。
</p>
<p>
インストールはいつもの通り</p>
<pre class="brush: bash;">
./configure --with-http_stub_status_module
                   --with-http_gzip_static_module
                   --with-cc-opt='-O3'
make
make install
</pre>
<p>だけで大丈夫でした。</p>
<p>ただ、私の環境ではなぜか configure が途中で失敗していて、しばらくはまりました。結局は .bash_profile で環境変数 CC と CFLAGS を指定していたのが原因でした。nginx の configure スクリプトは autoconf で作られたものでは無いので、予想もつかないところが問題になるなぁ。
</p>
<p>nginx 0.6 系で使用していた nginx.conf で起動させたところ、2点ほど問題がありました。</p>
<ol>
<li>gzip_types に text/html を設定していると警告が出る。
<pre class="brush: bash;">
gzip_types text/plain text/html text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;
</pre>
<p>0.6系では上のような設定で問題ありませんでしたが、0.7 では以下の警告が出ます。</p>
<blockquote><p>
duplicate MIME type &#8220;text/html&#8221; in /etc/nginx/nginx.conf
</p></blockquote>
<p>text/html は暗黙的に設定されているようなので、以下のように修正。</p>
<pre class="brush: bash;">
gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;
</pre>
</li>
<li>特定のPHPファイルだけ BASIC認証させていたが、PHPとして実行されなくなった。
<p>WordPress のログインページを BASIC 認証をさせるために、以下のような設定をしていました。</p>
<pre class="brush: bash;">
    location /wordpress/wp-login.php {
        auth_basic &quot;secret&quot;;
        auth_basic_user_file htpasswd;
    }
</pre>
<p>nginx 0.6 系では BASIC 認証後に wp-login.php が実行されていましたが、0.7系では BASIC認証はされるものの、PHPファイルが実行されずにソースファイルがそのまま転送されてきました。</p>
<p>どのように修正すべきか分からなかったので、以下のように fastcgi の設定を入れて ad hoc に修正しました。</p>
<pre class="brush: bash;">
    location /wordpress/wp-login.php {
        auth_basic &quot;secret&quot;;
        auth_basic_user_file htpasswd;

        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $fastcgi_script_name;
        include        fastcgi_params;
    }
</pre>
<p>こういうケースでは今後は try_files ディレクティブを使えって事なんでしょうね。
</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://blog.bluegold.me/2009/06/nginx-update-0759/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>nginxでphpを利用する</title>
		<link>http://blog.bluegold.me/2008/11/nginx-php-fastcgi/</link>
		<comments>http://blog.bluegold.me/2008/11/nginx-php-fastcgi/#comments</comments>
		<pubDate>Wed, 12 Nov 2008 15:09:58 +0000</pubDate>
		<dc:creator>bg</dc:creator>
				<category><![CDATA[技術]]></category>
		<category><![CDATA[fastcgi]]></category>
		<category><![CDATA[nginx]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://blog.bluegold.me/?p=72</guid>
		<description><![CDATA[nginx で php を利用するための FastCGI の設定方法について説明します。]]></description>
			<content:encoded><![CDATA[<p><img src="http://wp-cdn.bluegold.me/2008/10/nginx-small.png" width="150" height="50" alt="" class="title_img" /></p>
<p>
<a href="http://nginx.net/" title="http://nginx.net/">nginx</a>からphpを利用するには、FastCGIを有効にしてphpをビルドしておく必要があります。<br />
php-5.2.6 を以下のようにビルドしました。
</p>
<p><pre class="brush: bash;">
./configure
    --with-curl=/usr --enable-fastcgi
    --enable-mbstring --enable-zend-multibyte
    --enable-mbregex --with-mysql
    --with-mcrypt --with-mhash
    --with-openssl --with-gd
    --enable-gd-native-ttf --enable-gd-jis-conv
    --with-jpeg-dir=/usr --with-xpm-dir=/usr
    --with-freetype-dir=/usr
make
make install
</pre>
</p>
<p>メールで記事を投稿する為に openssl と gd の関係のオプションを追加してます。<br />
openssl は gmail に対して POP で接続する為に、gd は<a href="http://wppluginsj.sourceforge.jp/ktai_entry/" title="Ktai Entry">Ktai Entry</a>で画像を添付したメールを処理するのに必要でした。</p>
<p>FastCGIのプロセスを以下のように起動します。</p>
<pre class="brush: bash;">
/usr/local/bin/php-cgi -q -b 127.0.0.1:9000
</pre>
</p>
<p>127.0.0.1:9000 は FastCGI の接続を待ち受ける IPアドレスとポート番号です。<br />
この値は環境に合わせて別の物に変更する事が可能です。
</p>
<p><span id="more-72"></span></p>
<p>続いて nginx 側の設定ファイルを作成します。</p>
<p><a href="/2008/10/configure-nginx/" title="blog構築メモ: nginx を設定する">前回</a>の記事で基本的な設定は nginx.conf に書いてあるので、ここでは VirtualHost の設定だけを記述します。</p>
<p>phpをFastCGIで実行するのに最低限必要な設定はこれだけです。</p>
<p><pre class="brush: bash;">
server {
    listen       80;
    server_name  bluegold.me blog.bluegold.me;
    root   /var/www/blog;
    index  index.php index.html index.htm;
&amp;lt;p&amp;gt;    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /var/www/blog/$fastcgi_script_name;
        include        fastcgi_params;
    }
}
</pre>
</p>
<p>127.0.0.1:9000 の部分は FastCGI のプロセスのオプションと同じ値を設定します。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.bluegold.me/2008/11/nginx-php-fastcgi/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
