<?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/"
		xmlns:xhtml="http://www.w3.org/1999/xhtml"
>

<channel>
	<title>おいぬま日報 &#187; Apache</title>
	<atom:link href="http://tech.lampetty.net/tech/index.php/archives/category/apache/feed" rel="self" type="application/rss+xml" />
	<link>http://tech.lampetty.net/tech</link>
	<description>Apache, MySQL, Perl, Python あたり</description>
	<lastBuildDate>Sun, 05 Feb 2012 12:36:09 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://tech.lampetty.net/tech/index.php/archives/category/apache/feed" />
<atom:link rel="hub" href="http://pubsubhubbub.appspot.com"/><atom:link rel="hub" href="http://superfeedr.com/hubbub"/>		<item>
		<title>PHPでFatal error: Allowed memory size of &#8230;が出た場合の対処方法</title>
		<link>http://tech.lampetty.net/tech/index.php/archives/341?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=php%25e3%2581%25a7fatal-error-allowed-memory-size-of-%25e3%2581%258c%25e5%2587%25ba%25e3%2581%259f%25e5%25a0%25b4%25e5%2590%2588%25e3%2581%25ae%25e5%25af%25be%25e5%2587%25a6%25e6%2596%25b9%25e6%25b3%2595</link>
		<comments>http://tech.lampetty.net/tech/index.php/archives/341#comments</comments>
		<pubDate>Sat, 07 Nov 2009 14:56:30 +0000</pubDate>
		<dc:creator>oinume</dc:creator>
				<category><![CDATA[Apache]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://tech.lampetty.net/tech/?p=341</guid>
		<description><![CDATA[
		<script type="text/javascript"><!--
			show_rakuten('#book13992207', '<!-- cache --><div class="rakuten_image"><a href="http://hb.afl.rakuten.co.jp/hgc/0e871b08.72e65fa8.0e871b0a.296b5a1b/?pc=http%3A%2F%2Fitem.rakuten.co.jp%2Fbook%2F6794779%2F"><img src="http://thumbnail.image.rakuten.co.jp/@0_mall/book/cabinet/7741/77414437.jpg?_ex=128x128"><br /><p class="rakuten_logo"><!-- Rakuten Web Services Attribution Snippet FROM HERE --><a href="http://webservice.rakuten.co.jp/" target="_blank"><img src="http://webservice.rakuten.co.jp/img/credit/200709/credit_4936.gif" border="0" alt="楽天ウェブサービスセンター" title="楽天ウェブサービスセンター" width="49" height="36"/></a><!-- Rakuten Web Services Attribution Snippet TO HERE --></p><font class="rakuten_warn"><a href="#" class="tooltip">[ご利用にあたって]<span>このサイトで掲載されている情報は、「おいぬま日報」の作成者により運営されています。価格、販売可能情報は、変更される場合があります。購入時に楽天市場店舗（www.rakuten.co.jp）に表示されている価格が、その商品の販売に適用されます。</span></a></font></div><div class="rakuten_info"><p class="rakuten_itemname"><a href="http://hb.afl.rakuten.co.jp/hgc/0e871b08.72e65fa8.0e871b0a.296b5a1b/?pc=http%3A%2F%2Fitem.rakuten.co.jp%2Fbook%2F6794779%2F">【送料無料】パーフェクトPHP</a></p><p class="rakuten_caption">Perfect series 小川雄大 柄沢聡太郎 技術評論社発行年月：2010年12月 ページ数：591p サイズ：単行本 ISBN：978477414437……</p><p><em>販売価格：</em> 3780 円 <font class="rakuten_time">(12/02/10 01:36 更新)</font></p><p><em>販売店舗：</em> <a href="http://www.rakuten.co.jp/book/">楽天ブックス</a></p></div><!-- cache -->');
		// -->
		</script>
		あまりPHPのことわからないのですが、WordPressで Fatal error: Allowed memory size of 8388608 bytes exhausted というエラーメッセージが出ました。ぐぐる [...]]]></description>
			<content:encoded><![CDATA[
		<script type="text/javascript"><!--
			show_rakuten('#book13992207', '<!-- cache --><div class="rakuten_image"><a href="http://hb.afl.rakuten.co.jp/hgc/0e871b08.72e65fa8.0e871b0a.296b5a1b/?pc=http%3A%2F%2Fitem.rakuten.co.jp%2Fbook%2F6794779%2F"><img src="http://thumbnail.image.rakuten.co.jp/@0_mall/book/cabinet/7741/77414437.jpg?_ex=128x128"><br /><p class="rakuten_logo"><!-- Rakuten Web Services Attribution Snippet FROM HERE --><a href="http://webservice.rakuten.co.jp/" target="_blank"><img src="http://webservice.rakuten.co.jp/img/credit/200709/credit_4936.gif" border="0" alt="楽天ウェブサービスセンター" title="楽天ウェブサービスセンター" width="49" height="36"/></a><!-- Rakuten Web Services Attribution Snippet TO HERE --></p><font class="rakuten_warn"><a href="#" class="tooltip">[ご利用にあたって]<span>このサイトで掲載されている情報は、「おいぬま日報」の作成者により運営されています。価格、販売可能情報は、変更される場合があります。購入時に楽天市場店舗（www.rakuten.co.jp）に表示されている価格が、その商品の販売に適用されます。</span></a></font></div><div class="rakuten_info"><p class="rakuten_itemname"><a href="http://hb.afl.rakuten.co.jp/hgc/0e871b08.72e65fa8.0e871b0a.296b5a1b/?pc=http%3A%2F%2Fitem.rakuten.co.jp%2Fbook%2F6794779%2F">【送料無料】パーフェクトPHP</a></p><p class="rakuten_caption">Perfect series 小川雄大 柄沢聡太郎 技術評論社発行年月：2010年12月 ページ数：591p サイズ：単行本 ISBN：978477414437……</p><p><em>販売価格：</em> 3780 円 <font class="rakuten_time">(12/02/10 01:36 更新)</font></p><p><em>販売店舗：</em> <a href="http://www.rakuten.co.jp/book/">楽天ブックス</a></p></div><!-- cache -->');
		// -->
		</script>
		<p>あまりPHPのことわからないのですが、WordPressで</p>
<pre>
Fatal error: Allowed memory size of 8388608 bytes exhausted
</pre>
<p>というエラーメッセージが出ました。ぐぐると「php.iniのmemory_limitを64Mとか大きな値に設定すべし」とあるのですが、Ubuntuの /usr/share/php5/php.ini を</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">memory_limit = 64M</pre></div></div>

<p>と直しても改善しませんでした。で、さらに調べてみるとどうやら /etc/php5/apache2/php.ini なんていうファイルが&#8230; これを上記と同じように64Mにしてapacheを再起動すると、めでたくエラーは出なくなりました。紛らわしいからphp.iniは一つにしておいてほしい&#8230;</p>
<p><div id="book13992207" class="rakuten_details">Now Loading...</div></p>
]]></content:encoded>
			<wfw:commentRss>http://tech.lampetty.net/tech/index.php/archives/341/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://tech.lampetty.net/tech/index.php/archives/341" />
	</item>
		<item>
		<title>Apacheでリバースプロキシ(mod_proxy)を活用する(3) &#8211; 負荷分散編</title>
		<link>http://tech.lampetty.net/tech/index.php/archives/288?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=apache%25e3%2581%25a7%25e3%2583%25aa%25e3%2583%2590%25e3%2583%25bc%25e3%2582%25b9%25e3%2583%2597%25e3%2583%25ad%25e3%2582%25ad%25e3%2582%25b7mod_proxy%25e3%2582%2592%25e6%25b4%25bb%25e7%2594%25a8%25e3%2581%2599%25e3%2582%258b-%25e8%25b2%25a0%25e8%258d%25b7%25e5%2588%2586%25e6%2595%25a3%25e7%25b7%25a8</link>
		<comments>http://tech.lampetty.net/tech/index.php/archives/288#comments</comments>
		<pubDate>Sun, 12 Apr 2009 13:06:20 +0000</pubDate>
		<dc:creator>oinume</dc:creator>
				<category><![CDATA[Apache]]></category>
		<category><![CDATA[mod_proxy]]></category>

		<guid isPermaLink="false">http://tech.lampetty.net/tech/?p=288</guid>
		<description><![CDATA[前回のApacheでリバースプロキシ(mod_proxy)を活用する(2) &#8211; 設定編ではフロントエンドのリバースプロキシとバックエンドのCGIサーバの設定を行ないました。今回は、サイトの負荷が大きくなってき [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://tech.lampetty.net/tech/index.php/archives/246">前回のApacheでリバースプロキシ(mod_proxy)を活用する(2) &#8211; 設定編</a>ではフロントエンドのリバースプロキシとバックエンドのCGIサーバの設定を行ないました。今回は、サイトの負荷が大きくなってきた時にバックエンドのサーバを複数台にする設定を説明します。イメージ図としては下記のような感じです。</p>
<p style="text-align: center;"><a class="" rel="lightbox" title="リバースプロキシでの負荷分散のイメージ" href="http://www.flickr.com/photos/8250493@N05/3433727583/"><img class="alignnone" src="http://static.flickr.com/3578/3433727583_883728521b.jpg" alt="リバースプロキシでの負荷分散のイメージ"></a></p>
<h3>バックエンドの準備</h3>
<p>まずは負荷分散が確認できるようにバックエンドのサーバを2台に増やします。前回作成した /etc/apache2/httpd_backend_8080.conf をコピーして /etc/apache2/httpd_backend_8081.conf を作成し、このhttpd_backend_8081.confがListenするポートを以下のように8080から8081にします。</p>

<div class="wp_syntax"><div class="code"><pre class="diff" style="font-family:monospace;"><span style="color: #888822;">--- httpd_backend_8080.conf     2009-04-07 00:32:55.000000000 +0900</span>
<span style="color: #888822;">+++ httpd_backend_8081.conf     2009-04-07 00:36:09.000000000 +0900</span>
<span style="color: #440088;">@@ -1,5 +1,5 @@</span>
 #
<span style="color: #991111;">-# /etc/apache2/httpd_backend_8080.conf</span>
<span style="color: #00b000;">+# /etc/apache2/httpd_backend_8081.conf</span>
 #
 User www-data
 Group www-data
<span style="color: #440088;">@@ -12,15 +12,15 @@</span>
 LoadModule mime_module /usr/lib/apache2/modules/mod_mime.so
&nbsp;
 ServerRoot &quot;/etc/apache2&quot;
<span style="color: #991111;">-Listen 8080</span>
<span style="color: #991111;">-ServerName localhost:8080</span>
<span style="color: #991111;">-PidFile /tmp/httpd_backend_8080.pid</span>
<span style="color: #00b000;">+Listen 8081</span>
<span style="color: #00b000;">+ServerName localhost:8081</span>
<span style="color: #00b000;">+PidFile /tmp/httpd_backend_8081.pid</span>
 ServerAdmin example_at_example.com
 DocumentRoot &quot;/var/www/html&quot;
 TypesConfig /etc/mime.types
 LogFormat &quot;%h %l %u %t \&quot;%r\&quot; %&gt;s %b \&quot;%<span style="">&#123;</span>Referer<span style="">&#125;</span>i\&quot; \&quot;%<span style="">&#123;</span>User-Agent<span style="">&#125;</span>i\&quot;&quot; combined
<span style="color: #991111;">-ErrorLog /tmp/error_8080.log</span>
<span style="color: #991111;">-CustomLog /tmp/access_8080.log combined</span>
<span style="color: #00b000;">+ErrorLog /tmp/error_8081.log</span>
<span style="color: #00b000;">+CustomLog /tmp/access_8081.log combined</span>
 UseCanonicalName Off
 AddHandler cgi-script .pl</pre></div></div>

<p>ポートを変更したら、下記のコマンドでこの8081でListenするApacheを立ち上げておきます。</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>apache2ctl <span style="color: #660033;">-f</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>apache2<span style="color: #000000; font-weight: bold;">/</span>httpd_backend_8081.conf <span style="color: #660033;">-k</span> start</pre></div></div>

<p>8080のサーバも停止しているようであれば起動しておきましょう。</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>apache2ctl <span style="color: #660033;">-f</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>apache2<span style="color: #000000; font-weight: bold;">/</span>httpd_backend_8080.conf <span style="color: #660033;">-k</span> start</pre></div></div>

<h3>リバースプロキシで負荷分散の設定</h3>
<p>バックエンドのサーバを2台に増設したら、その2台にリクエストを振り分けられるようにフロントエンドのリバースプロキシを設定し直します。やり方は色々ありますが、まずは一番シンプルな「リクエストをランダムに2台に振り分ける」というやり方を試してみたいと思います。</p>
<p>前回の設定では</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">RewriteEngine on
RewriteRule ^/(.+)\.pl$ http://localhost:8080/index.pl [L,P,QSA]
RewriteRule ^/(.+)\.(gif)$ /var/www/html/$1.$2 [L,QSA]</pre></div></div>

<p>というように、.plファイルへのアクセスを localhost:8080 に振り分ける設定でした。今回はこの部分を変更して、localhost:8080とlocalhost:8081に振り分けるように設定します。具体的には、前回のhttpd.confに対して</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">RewriteMap server rnd:/usr/local/httpd_proxy_2.2.11/conf/server.txt</pre></div></div>

<p>という設定をRewriteRuleの前に追加し、RewriteRuleを下記のように変更します。</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">RewriteRule ^/(.+)\.pl$ http://localhost:8080/index.pl [P,QSA]
            ↓
RewriteRule ^/(.+)\.pl$ http://${server:backend}/index.pl [L,P,QSA]</pre></div></div>

<p>つまり前回のhttpd.confの設定との差分は下記のようになります。</p>

<div class="wp_syntax"><div class="code"><pre class="diff" style="font-family:monospace;"><span style="color: #888822;">--- httpd.conf.entry246 2009-04-12 17:22:07.000000000 +0900</span>
<span style="color: #888822;">+++ httpd.conf  2009-04-12 17:29:49.000000000 +0900</span>
<span style="color: #440088;">@@ -421,6 +421,7 @@</span>
 &lt;/Directory&gt;
&nbsp;
 RewriteEngine on
<span style="color: #991111;">-RewriteRule ^/<span style="">&#40;</span>.+<span style="">&#41;</span>\.pl$ http://localhost:8080/index.pl <span style="">&#91;</span>P,QSA<span style="">&#93;</span></span>
<span style="color: #00b000;">+RewriteMap server rnd:/usr/local/httpd_proxy_2.2.11/conf/server.txt</span>
<span style="color: #00b000;">+RewriteRule ^/<span style="">&#40;</span>.+<span style="">&#41;</span>\.pl$ http://$<span style="">&#123;</span>server:backend<span style="">&#125;</span>/index.pl <span style="">&#91;</span>L,P,QSA<span style="">&#93;</span></span>
 RewriteRule ^/<span style="">&#40;</span>.+<span style="">&#41;</span>\.<span style="">&#40;</span>gif<span style="">&#41;</span>$ /var/www/html/$1.$2 <span style="">&#91;</span>L,QSA<span style="">&#93;</span></pre></div></div>

<p>httpd.confを書き換えたら、RewriteMapで指定した /usr/local/httpd_proxy_2.2.11/conf/server.txt  というファイルを作成します。</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># echo 'backend localhost:8080|localhost:8081' &gt;  /usr/local/httpd_proxy_2.2.11/conf/server.txt</span></pre></div></div>

<p>これらの設定は何を意味するかというと</p>
<blockquote><p>
RewriteRuleで指定したパターンにマッチするリクエストがあった場合、RewriteMapで定義したサーバのどれか1台にランダムでリクエストを転送する
</p></blockquote>
<p>ということです。RewriteMapというディレクティブでは /usr/local/httpd_proxy_2.2.11/conf/server.txt  というファイルを指定して、そのファイルの中身は</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">backend localhost:<span style="color: #000000;">8080</span><span style="color: #000000; font-weight: bold;">|</span>localhost:<span style="color: #000000;">8081</span></pre></div></div>

<p>となっています。ここで定義したバックエンドのサーバをその後ろのRewirteRuleで ${server:backend} として参照しているわけです。前回の設定では1台にしかリクエストを転送していませんでしたが、このような設定をすることで「どれか1台にランダムで」という形で負荷分散することが可能になります。</p>
<p>というわけで、設定が完了したら mod_proxyサーバを再起動します。</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>httpd_proxy_2.2.11<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>apachectl <span style="color: #660033;">-k</span> restart</pre></div></div>

<h3>動作確認</h3>
<p>サーバが起動したらブラウザから http://example/index.pl に何回かアクセスしてみましょう。(exampleの部分は自身のホスト名に適宜変更してください)</p>
<p>/tmp/access_8080.logと/tmp/access_8081.log の両方に下記のようなログが残っているはずです。</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">127.0.0.1 - - <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">12</span><span style="color: #000000; font-weight: bold;">/</span>Apr<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">2009</span>:<span style="color: #000000;">17</span>:<span style="color: #000000;">46</span>:<span style="color: #000000;">35</span> +0900<span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #ff0000;">&quot;GET /index.pl HTTP/1.1&quot;</span> <span style="color: #000000;">200</span> <span style="color: #000000;">129</span> <span style="color: #ff0000;">&quot;-&quot;</span> <span style="color: #ff0000;">&quot;Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; ja-JP-mac; rv:1.9.0.8) Gecko/2009032608 Firefox/3.0.8&quot;</span></pre></div></div>

<p>これが確認できれば localhost:8080 と localhost:8081 の2台のバックエンドのサーバでの負荷分散環境が構築できたということになります。(なお、例によってそのままの設定だとセキュリティに問題があるので、作業が終了したら起動したApacheのプロセスは止めておくことをお奨めします)</p>
<h3>まとめ</h3>
<p>mod_proxyを使った負荷分散環境の構築は、バックエンドのサーバさえしっかり準備ができていればリバースプロキシの設定はかなり簡単だったのではないでしょうか。このような複数台のバックエンドのサーバにリクエストを振り分けるような設定は負荷分散としても有効ですが、その他の使い道として「バックエンドのサーバを1台メンテナンスで一時的に止めたい」というような時にサイトを止めることなくメンテナンスが可能になります。(具体的には、server.txtからメンテナンス対象のサーバを削除するだけでリクエストが転送されなくなります)</p>
<p>このように、リバースプロキシの用途は単純な負荷分散以外にもあるので、設定の仕方を知っておくだけでサイトの運用に非常に役立つと思います。</p>
<div class="tmkm-amazon-view">
	<p><a href="http://www.amazon.co.jp/Apache%E3%82%AF%E3%83%83%E3%82%AF%E3%83%96%E3%83%83%E3%82%AF-%E7%AC%AC2%E7%89%88-%E2%80%95Web%E3%82%B5%E3%83%BC%E3%83%90%E7%AE%A1%E7%90%86%E8%80%85%E3%81%AE%E3%81%9F%E3%82%81%E3%81%AE%E3%83%AC%E3%82%B7%E3%83%94%E9%9B%86-Ken-Coar/dp/4873113814%3FSubscriptionId%3DAKIAJGVWECUONT35GEJA%26tag%3Dkazzhomeunixo-22%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D4873113814"><img src="http://ecx.images-amazon.com/images/I/515t-Vj3AAL._SL160_.jpg" border="0" alt="" /></a></p>
	<p><a href="http://www.amazon.co.jp/Apache%E3%82%AF%E3%83%83%E3%82%AF%E3%83%96%E3%83%83%E3%82%AF-%E7%AC%AC2%E7%89%88-%E2%80%95Web%E3%82%B5%E3%83%BC%E3%83%90%E7%AE%A1%E7%90%86%E8%80%85%E3%81%AE%E3%81%9F%E3%82%81%E3%81%AE%E3%83%AC%E3%82%B7%E3%83%94%E9%9B%86-Ken-Coar/dp/4873113814%3FSubscriptionId%3DAKIAJGVWECUONT35GEJA%26tag%3Dkazzhomeunixo-22%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D4873113814">Apacheクックブック 第2版 ―Webサーバ管理者のためのレシピ集</a></p>
	<p><em>著者／訳者：</em>Ken Coar Rich Bowen </p>
	<p><em>出版社：</em>オライリージャパン( 2008-09-26 )</p>
	<p>大型本 ( 308 ページ )</p>
<hr class="tmkm-amazon-clear" /></div>
]]></content:encoded>
			<wfw:commentRss>http://tech.lampetty.net/tech/index.php/archives/288/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://tech.lampetty.net/tech/index.php/archives/288" />
	</item>
		<item>
		<title>Apache + mod_fcgid + tDiaryでrubyプロセスが残りっぱなし</title>
		<link>http://tech.lampetty.net/tech/index.php/archives/275?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=apache-mod_fcgid-tdiary%25e3%2581%25a7ruby%25e3%2583%2597%25e3%2583%25ad%25e3%2582%25bb%25e3%2582%25b9%25e3%2581%258c%25e6%25ae%258b%25e3%2582%258a%25e3%2581%25a3%25e3%2581%25b1%25e3%2581%25aa%25e3%2581%2597</link>
		<comments>http://tech.lampetty.net/tech/index.php/archives/275#comments</comments>
		<pubDate>Mon, 16 Feb 2009 12:08:36 +0000</pubDate>
		<dc:creator>oinume</dc:creator>
				<category><![CDATA[Apache]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[tDiary]]></category>

		<guid isPermaLink="false">http://tech.lampetty.net/tech/?p=275</guid>
		<description><![CDATA[古いおいぬま日報(不定期)のtDiaryなのですが、Ubuntu 8.10にしてからどうも調子が悪くて悩んでいます(*1)。というのは、tDiaryをapacheのmod_fcgid で動かしているのですが、CPU使用率 [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://diary.lampetty.net/">古いおいぬま日報(不定期)</a>のtDiaryなのですが、Ubuntu 8.10にしてからどうも調子が悪くて悩んでいます(*1)。というのは、tDiaryをapacheのmod_fcgid で動かしているのですが、CPU使用率100%近くなっているFastCGIのrubyプロセスがずっと残りっぱなしになっているようなのです。おかげで無駄にCPUリソースを食って非エコ状態になっています。<br />
*1) 環境はUbuntu 8.10 + Apache 2.2.11 + mod_fcgid 2.2 + tDiary 2.2.0</p>
<p>apacheのエラーログには</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>Mon Feb <span style="color: #000000;">16</span> <span style="color: #000000;">20</span>:<span style="color: #000000;">17</span>:<span style="color: #000000;">26</span> <span style="color: #000000;">2009</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>notice<span style="color: #7a0874; font-weight: bold;">&#93;</span> mod_fcgid: process <span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>kazuhiro<span style="color: #000000; font-weight: bold;">/</span>www<span style="color: #000000; font-weight: bold;">/</span>diary<span style="color: #000000; font-weight: bold;">/</span>htdocs<span style="color: #000000; font-weight: bold;">/</span>index.fcgi<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">30333</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">exit</span><span style="color: #7a0874; font-weight: bold;">&#40;</span>communication error<span style="color: #7a0874; font-weight: bold;">&#41;</span>, get stop signal <span style="color: #000000;">9</span></pre></div></div>

<p>と何やらあやしげなログが出ているのですが、なぜ communication errorになるのかさっぱりわかりません。</p>
<p>該当プロセスをkillすればCPUリソース使い過ぎな状態は回避できるので、10分以上経ったrubyプロセスを強制的にkillするスクリプトをでっち上げてcronで動かすように。</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/usr/local/bin/perl</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># $ sudo aptitude install libproc-processtable-perl</span>
<span style="color: #000000; font-weight: bold;">use</span> strict<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> Proc<span style="color: #339933;">::</span><span style="color: #006600;">ProcessTable</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$pt</span> <span style="color: #339933;">=</span> Proc<span style="color: #339933;">::</span><span style="color: #006600;">ProcessTable</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">new</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'cache_ttys'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">for</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$p</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">@</span><span style="color: #009900;">&#123;</span> <span style="color: #0000ff;">$pt</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">table</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$p</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">cmndline</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">/^ruby.+index\.fcgi/</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$elapsed</span> <span style="color: #339933;">=</span> <span style="color: #000066;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #0000ff;">$p</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">start</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$elapsed</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">60</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #0000ff;">$p</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">kill</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066;">print</span> <span style="color: #000066;">sprintf</span> <span style="color: #ff0000;">&quot;%s (%s) killed!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$p</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">pid</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$p</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">fname</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>mod_fcgidじゃなくてmod_fastcgiにすれば改善しそうな気もしますが、もうちょっと切羽詰まってから試してみようと思います。それにして<a href="http://search.cpan.org/dist/Proc-ProcessTable/ProcessTable.pm">Proc::ProcessTableモジュール</a>は便利でした。</p>
]]></content:encoded>
			<wfw:commentRss>http://tech.lampetty.net/tech/index.php/archives/275/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://tech.lampetty.net/tech/index.php/archives/275" />
	</item>
		<item>
		<title>Apacheでリバースプロキシ(mod_proxy)を活用する(2) &#8211; 設定編</title>
		<link>http://tech.lampetty.net/tech/index.php/archives/246?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=apache%25e3%2581%25a7%25e3%2583%25aa%25e3%2583%2590%25e3%2583%25bc%25e3%2582%25b9%25e3%2583%2597%25e3%2583%25ad%25e3%2582%25ad%25e3%2582%25b7mod_proxy%25e3%2582%2592%25e6%25b4%25bb%25e7%2594%25a8%25e3%2581%2599%25e3%2582%258b-%25e8%25a8%25ad%25e5%25ae%259a%25e7%25b7%25a8</link>
		<comments>http://tech.lampetty.net/tech/index.php/archives/246#comments</comments>
		<pubDate>Thu, 05 Feb 2009 14:15:05 +0000</pubDate>
		<dc:creator>oinume</dc:creator>
				<category><![CDATA[Apache]]></category>
		<category><![CDATA[mod_proxy]]></category>

		<guid isPermaLink="false">http://tech.lampetty.net/tech/?p=246</guid>
		<description><![CDATA[Apacheでリバースプロキシ(mod_proxy)を活用する(1) &#8211; インストール編に続いて、今回は実際にリバースプロキシサーバを構築してみたいと思います。 まずはバックエンドのサーバを準備 リバースプロ [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://tech.lampetty.net/tech/index.php/archives/238">Apacheでリバースプロキシ(mod_proxy)を活用する(1) &#8211; インストール編</a>に続いて、今回は実際にリバースプロキシサーバを構築してみたいと思います。</p>
<h3>まずはバックエンドのサーバを準備</h3>
<p>リバースプロキシを構築する前に、リバースプロキシサーバからリクエストが転送されるバックエンドのアプリケーションサーバを用意しておきます。今回はUbuntuでaptからインストールできるApacheを使用します。apache2パッケージがインストールされていない場合は、まずはこれをインストールしておきましょう。</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #c20cb9; font-weight: bold;">install</span> apache2</pre></div></div>

<p>インストールが完了したら下記のようなアプリケーションサーバ(といってもただCGIスクリプトを動かすだけのサーバ)の設定ファイルを /etc/apache2/httpd_backend_8080.conf として用意します。</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">#
# /etc/apache2/httpd_backend_8080.conf
#
User www-data
Group www-data
# modules
LoadModule authn_default_module /usr/lib/apache2/modules/mod_authn_default.so
LoadModule authz_default_module /usr/lib/apache2/modules/mod_authz_default.so
LoadModule authz_host_module /usr/lib/apache2/modules/mod_authz_host.so
LoadModule cgi_module /usr/lib/apache2/modules/mod_cgi.so
LoadModule dir_module /usr/lib/apache2/modules/mod_dir.so
LoadModule mime_module /usr/lib/apache2/modules/mod_mime.so
&nbsp;
ServerRoot &quot;/etc/apache2&quot;
Listen 8080
ServerName localhost:8080
PidFile /tmp/httpd_backend_8080.pid
ServerAdmin example_at_example.com
DocumentRoot &quot;/var/www/html&quot;
TypesConfig /etc/mime.types
LogFormat &quot;%h %l %u %t \&quot;%r\&quot; %&gt;s %b \&quot;%{Referer}i\&quot; \&quot;%{User-Agent}i\&quot;&quot; combined
ErrorLog /tmp/error_8080.log
CustomLog /tmp/access_8080.log combined
UseCanonicalName Off
AddHandler cgi-script .pl
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;IfModule</span> prefork.c<span style="color: #000000; font-weight: bold;">&gt;</span></span>
    StartServers         1
    MinSpareServers      1
    MaxSpareServers      5
    MaxClients          20
    MaxRequestsPerChild 50
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/IfModule<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Directory</span> /var/www/html<span style="color: #000000; font-weight: bold;">&gt;</span></span>
    Options ExecCGI FollowSymLinks
    AllowOverride None
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Directory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>設定ファイルを用意したら以下のコマンドでDocumentRootで指定したディレクトリを作成し、その下にCGIスクリプトである index.pl を用意します。</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">mkdir</span> <span style="color: #660033;">-p</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>www<span style="color: #000000; font-weight: bold;">/</span>html
$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">chmod</span> <span style="color: #000000;">755</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>www<span style="color: #000000; font-weight: bold;">/</span>html
$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">vi</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>www<span style="color: #000000; font-weight: bold;">/</span>html<span style="color: #000000; font-weight: bold;">/</span>index.pl</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/usr/bin/perl</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">use</span> strict<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> CGI<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$q</span> <span style="color: #339933;">=</span> CGI<span style="color: #339933;">-&gt;</span><span style="color: #006600;">new</span><span style="color: #339933;">;</span>
<span style="color: #000066;">print</span> <span style="color: #0000ff;">$q</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">header</span><span style="color: #009900;">&#40;</span>
    <span style="color: #339933;">-</span>type <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'text/html'</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066;">print</span> <span style="color: #cc0000; font-style: italic;">&lt;&lt;&quot;EOS&quot;;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Hello world&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h1&gt;Hello world!!&lt;/h1&gt;
&lt;img src=&quot;httpd_logo_wide.gif&quot; /&gt;
&lt;/body&gt;
&lt;/html&gt;
EOS</span></pre></div></div>

<p>index.pl を作成したら実行権限を付与します。そしてサンプルのための画像を Apache のサイトからダウンロードして /var/www/html/httpd_logo_wide.gif として保存します。</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">chmod</span> +x <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>www<span style="color: #000000; font-weight: bold;">/</span>html<span style="color: #000000; font-weight: bold;">/</span>index.pl
$ <span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>www<span style="color: #000000; font-weight: bold;">/</span>html
$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">wget</span> http:<span style="color: #000000; font-weight: bold;">//</span>httpd.apache.org<span style="color: #000000; font-weight: bold;">/</span>images<span style="color: #000000; font-weight: bold;">/</span>httpd_logo_wide.gif</pre></div></div>

<p>これで準備が整いました。アプリケーションサーバの Apache を起動しましょう。</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>apache2ctl <span style="color: #660033;">-f</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>apache2<span style="color: #000000; font-weight: bold;">/</span>httpd_backend_8080.conf <span style="color: #660033;">-k</span> start</pre></div></div>

<p>起動したら http://example:8080/index.pl にアクセスして Hello world!! なページが表示されることを確認します。example のホスト名は例なので、お手持ちのマシン名に置き換えてください。これでバックエンドのサーバの設定は完了です。もし起動しない場合はエラーログが /tmp/error.log にあるので、これを確認してみてください。</p>
<h3>リバースプロキシの準備</h3>
<p>次は肝心のリバースプロキシの設定です。<a href="http://tech.lampetty.net/tech/index.php/archives/238">前回</a> /usr/local/httpd_proxy_2.2.11 にインストールしたので、/usr/local/httpd_proxy_2.2.11/conf/httpd.conf を以下のように修正します。<br />
リバースプロキシの設定については <a href="http://httpd.apache.org/docs/2.2/ja/mod/mod_proxy.html#proxypassreverse">ProxyPassReverse</a> を使うやり方と mod_rewrite の [P] を使う2種類のやり方があると思いますが、今回は mod_rewrite でやりたいと思います。</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">--- httpd.conf.orig     2009-02-01 13:06:48.000000000 +0900
+++ httpd.conf  2009-02-04 22:40:14.000000000 +0900
@@ -106,7 +106,7 @@
 # documents. By default, all requests are taken from this directory, but
 # symbolic links and aliases may be used to point to other locations.
 #
-DocumentRoot &quot;/usr/local/httpd_proxy_2.2.11/htdocs&quot;
+DocumentRoot &quot;/var/www/html&quot;
&nbsp;
 #
 # Each directory to which Apache has access can be configured with respect
@@ -412,3 +412,15 @@
 SSLRandomSeed startup builtin
 SSLRandomSeed connect builtin
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/IfModule<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
+
+<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Directory</span> /var/www/html<span style="color: #000000; font-weight: bold;">&gt;</span></span>
+    Options Indexes FollowSymLinks MultiViews
+    AllowOverride Indexes Limit
+    Order allow,deny
+    Allow from all
+<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Directory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
+
+RewriteEngine on
+RewriteRule ^/(.+)\.pl$ http://localhost:8080/index.pl [L,P,QSA]
+RewriteRule ^/(.+)\.(gif)$ /var/www/html/$1.$2 [L,QSA]</pre></div></div>

<p>修正内容を順番に大雑把に解説すると</p>
<ol>
<li>DocumentRootの変更</li>
<li>/var/www/htmlディレクトリの設定</li>
<li>mod_rewriteによるリバースプロキシの設定</li>
</ol>
<p>となります。詳しく見ておきたいのは3.の部分で</p>
<pre>
RewriteEngine on
</pre>
<p>というディレクティブで mod_rewrite によるURLの書き換えを有効にし</p>
<pre>
RewriteRule ^/(.+)\.pl$ http://localhost:8080/index.pl [L,P,QSA]
</pre>
<p>で 拡張子が .pl のファイルを先ほど設定したバックエンドのサーバに転送し</p>
<pre>
RewriteRule ^/(.+)\.(gif)$ /var/www/html/$1.$2 [L,QSA]
</pre>
<p>拡張子が .pl 以外の .gif ファイルはこのリバースプロキシサーバ自体が処理する、という設定になっています。つまり、index.pl 内で使用しているスタティックな画像ファイル httpd_logo_wide.gif はプロキシサーバによって処理されますが、index.pl のファイルは動的にコンテンツを生成するCGIスクリプトなので、バックエンドのアプリケーションサーバにリクエストが転送されます。</p>
<p>ちなみに RewriteRule で使っている L, P, QSA のようなオプションの意味ですが</p>
<ul>
<li>L &#8211; このルールが適用されたらそれ以降のルールは実行されない</li>
<li>P &#8211; プロキシとしてURLの書き換えを行う</li>
<li>QSA &#8211; URLの?以降のQUERY_STRINGを転送先のURLに常につける</li>
</ul>
<p>という意味になります。詳しくは<a href='http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html'>mod_rewrite &#8211; Apache HTTP Server</a>に書いてあるので参照してみてください。</p>
<h3>動作確認</h3>
<p>これで全ての準備が整ったので mod_proxy サーバを起動します。</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>httpd_proxy_2.2.11<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>apachectl <span style="color: #660033;">-k</span> start</pre></div></div>

<p>そしてブラウザから http://example/index.pl にアクセスしてみましょう。Hello world!! のあとにApache のロゴが表示されているでしょうか。表示されていればリバースプロキシサーバの構築は完了ですが、念のため本当にリバースプロキシで動いているのか、アクセスログを見て確認してみましょう。リバースプロキシのログは /usr/local/httpd_proxy_2.2.11/logs/access_log にあります。</p>
<pre>
192.168.1.54 - - [05/Feb/2009:02:12:43 +0900] "GET /index.pl HTTP/1.1" 200 129
192.168.1.54 - - [05/Feb/2009:02:12:44 +0900] "GET /httpd_logo_wide.gif HTTP/1.1" 200 -
</pre>
<p>このログには上のような2つのログが出ているはずです。次にバックエンドのサーバのログを見てみます。これは /tmp/access.log にあります。</p>
<pre>
127.0.0.1 - - [05/Feb/2009:02:12:43 +0900] "GET /index.pl HTTP/1.1" 200 129 "-" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; ja-JP-mac; rv:1.9.0.5) Gecko/2008120121 Firefox/3.0.5"
</pre>
<p>こちらのログには index.plのアクセスのみで httpd_logo_wide.gif へのアクセスはないはずです。これが確認できれば mod_proxy によるリバースプロキシサーバ構築に成功ということになります。ここまで設定できれば、あとは応用で「バックエンドのサーバを2台にして運用する」ということも可能になります。次回はそういう負荷分散も視野にいれたエントリを書きたいと思います。</p>
<p>なお、今回記載した Apache の設定は、簡潔なサンプルであるためセキュリティ的にはよろしくありません。なので、インターネットにつながっているサーバではこの記事の内容を試す場合、動作確認後すみやかに Apache のプロセスを落としておいた方が良いかと思います。</p>
<p>2009/04/06: 後世のためにバックエンドの設定ファイルの名前を /etc/apache2/httpd_backend.conf から /etc/apache2/httpd_backend_8080.conf に変更しました。</p>
<div class="tmkm-amazon-view">
	<p><a href="http://www.amazon.co.jp/Apache%E3%82%AF%E3%83%83%E3%82%AF%E3%83%96%E3%83%83%E3%82%AF-%E7%AC%AC2%E7%89%88-%E2%80%95Web%E3%82%B5%E3%83%BC%E3%83%90%E7%AE%A1%E7%90%86%E8%80%85%E3%81%AE%E3%81%9F%E3%82%81%E3%81%AE%E3%83%AC%E3%82%B7%E3%83%94%E9%9B%86-Ken-Coar/dp/4873113814%3FSubscriptionId%3DAKIAJGVWECUONT35GEJA%26tag%3Dkazzhomeunixo-22%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D4873113814"><img src="http://ecx.images-amazon.com/images/I/515t-Vj3AAL._SL160_.jpg" border="0" alt="" /></a></p>
	<p><a href="http://www.amazon.co.jp/Apache%E3%82%AF%E3%83%83%E3%82%AF%E3%83%96%E3%83%83%E3%82%AF-%E7%AC%AC2%E7%89%88-%E2%80%95Web%E3%82%B5%E3%83%BC%E3%83%90%E7%AE%A1%E7%90%86%E8%80%85%E3%81%AE%E3%81%9F%E3%82%81%E3%81%AE%E3%83%AC%E3%82%B7%E3%83%94%E9%9B%86-Ken-Coar/dp/4873113814%3FSubscriptionId%3DAKIAJGVWECUONT35GEJA%26tag%3Dkazzhomeunixo-22%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D4873113814">Apacheクックブック 第2版 ―Webサーバ管理者のためのレシピ集</a></p>
	<p><em>著者／訳者：</em>Ken Coar Rich Bowen </p>
	<p><em>出版社：</em>オライリージャパン( 2008-09-26 )</p>
	<p>大型本 ( 308 ページ )</p>
<hr class="tmkm-amazon-clear" /></div>
]]></content:encoded>
			<wfw:commentRss>http://tech.lampetty.net/tech/index.php/archives/246/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://tech.lampetty.net/tech/index.php/archives/246" />
	</item>
		<item>
		<title>Apacheでリバースプロキシ(mod_proxy)を活用する(1) &#8211; インストール編</title>
		<link>http://tech.lampetty.net/tech/index.php/archives/238?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=apache%25e3%2581%25a7%25e3%2583%25aa%25e3%2583%2590%25e3%2583%25bc%25e3%2582%25b9%25e3%2583%2597%25e3%2583%25ad%25e3%2582%25ad%25e3%2582%25b7mod_proxy%25e3%2582%2592%25e6%25b4%25bb%25e7%2594%25a8%25e3%2581%2599%25e3%2582%258b%25e3%2582%25a4%25e3%2583%25b3%25e3%2582%25b9%25e3%2583%2588%25e3%2583%25bc%25e3%2583%25ab</link>
		<comments>http://tech.lampetty.net/tech/index.php/archives/238#comments</comments>
		<pubDate>Wed, 28 Jan 2009 17:42:57 +0000</pubDate>
		<dc:creator>oinume</dc:creator>
				<category><![CDATA[Apache]]></category>

		<guid isPermaLink="false">http://tech.lampetty.net/tech/?p=238</guid>
		<description><![CDATA[Apacheはとてもとても有名なWeb Serverですが、今日はそのチューニング方法の一つであるリバースプロキシについて書きたいと思います。 Webサーバの種類 一口にWebサーバと言っても、ダイナミックなWebコンテ [...]]]></description>
			<content:encoded><![CDATA[<p>Apacheはとてもとても有名なWeb Serverですが、今日はそのチューニング方法の一つであるリバースプロキシについて書きたいと思います。</p>
<h3>Webサーバの種類</h3>
<p>一口にWebサーバと言っても、ダイナミックなWebコンテンツを生成するサイトにおいてWebサーバがserveするものは2種類あります。一つはプログラムで動的に生成されるHTMLファイル。もう一つがGIFやJPEG, PNGなどの画像、および Flash, JavaScript, CSSなどの静的なファイルです。</p>
<p>プログラムで動的にHTMLを生成するためには、そのプログラムを動かす実行環境がWebサーバ(Apache)と連携する必要があります。いわゆるCGIとか最近だとFastCGI, mod_php, mod_perl, mod_rails, mod_pythonなどのスクリプト環境がよく使われています。これらの環境は昨今では「事前に実行環境(インタープリター)の起動をしてプログラムをコンパイルしておきそれを再利用する」というような使われ方をするため、Apacheがクライアントからの要求を処理するために生成する子プロセスのメモリ使用量が肥大化し、プロセスを起動する処理が重くなる傾向にあります。</p>
<p>一方で、単純にファイルの中身をそのままクライアントに渡せばいいようなスタティックなファイルが、このような肥大化したプロセスで処理されることには非常に無駄があります。そこでリバースプロキシの出番です。リバースプロキシの動作は凄くシンプルで、リクエストを受けたら、ダイナミックなページのリクエストのみバックエンドのアプリケーションサーバに渡し、それ以外のスタティックなファイルは自分自身で直接処理します(*1)。図にするとこんな感じでしょうか。</p>
<p style="text-align: center;"><a class="" rel="lightbox" title="リバースプロキシのイメージ" href="http://www.flickr.com/photos/8250493@N05/3260285170/"><img class="alignnone" src="http://static.flickr.com/3420/3260285170_53e07990d8.jpg" alt="リバースプロキシのイメージ"></a></p>
<p>このような単純な動作なので、リバースプロキシ自体にはmod_perlやmod_phpなどを組み込む必要がなくなり、プロセスのサイズは非常にスリムになります。実際このブログを動かしているマシンでも</p>
<ul>
<li>リバースプロキシのプロセスサイズは3.5MB程度</li>
<li>mod_phpのプロセスサイズは25MB程度</li>
</ul>
<p>となっており、一プロセスあたりで20MBも違います。</p>
<p>さらに、リバースプロキシは動作がシンプルなので、ApacheのMPM(Multi-Proccessing Module)にworkerというものを選択することができます。worker mpmはクライアントからのリクエストを処理するのにプロセスより軽量なスレッドを使用するため(*2)、prefork mpmと比べて動作が軽いという特徴があります。</p>
<p>*1)これは設定自体でどうにでも変更できます<br />
*2)厳密に言うとプロセス＋スレッドのハイブリッド方式です</p>
<p>また、フロントエンドのリバースプロキシ＋バックエンドのアプリケーションサーバという構成にしておくことで、リバースプロキシは止めずにバックエンドのサーバを増やしたり交換したりすることが可能です。このため<strong>負荷分散のためのメンテナンスが非常にやりやすくなります</strong>。このあたりはmod_proxy_balancerというモジュールの話になるので今回は割愛しますが、また機会があればということで。</p>
<h3>Apache 2.2.11のインストール</h3>
<p>前置きが長くなりましたが、リバースプロキシを導入するメリットがわかったところで実際にApacheをインストールしてみたいと思います。環境は Ubuntu 8.10 Interpid です。<br />
まずは最新版である2.2.11のソースを <a href="http://httpd.apache.org/download.cgi">Apache本家</a> からダウンロードしておきます。</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">tar</span> xvzf httpd-2.2.11.tar.gz
<span style="color: #666666; font-style: italic;"># ちょっとでも最適化する</span>
$ <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">CFLAGS</span>=<span style="color: #ff0000;">&quot;-O3&quot;</span>
$ <span style="color: #7a0874; font-weight: bold;">cd</span> httpd-2.2.11
$ .<span style="color: #000000; font-weight: bold;">/</span>configure <span style="color: #660033;">--prefix</span>=<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>httpd_proxy_2.2.11 \
 <span style="color: #660033;">--with-mpm</span>=worker \
 <span style="color: #660033;">--enable-shared</span> \
 <span style="color: #660033;">--enable-so</span> \
 <span style="color: #660033;">--disable-asis</span> \
 <span style="color: #660033;">--disable-cgid</span> \
 <span style="color: #660033;">--disable-proxy-connect</span> \
 <span style="color: #660033;">--disable-proxy-ftp</span> \
 <span style="color: #660033;">--disable-proxy-ajp</span> \
 <span style="color: #660033;">--disable-userdir</span> \
 <span style="color: #660033;">--disable-actions</span> \
 <span style="color: #660033;">--enable-modules</span>=<span style="color: #ff0000;">'rewrite proxy proxy_balancer proxy_http cgi deflate headers expires'</span> \
 <span style="color: #660033;">--enable-mods-shared</span>=<span style="color: #ff0000;">'ssl cache disk_cache mem_cache'</span> \
 <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #c20cb9; font-weight: bold;">make</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">make</span> <span style="color: #c20cb9; font-weight: bold;">install</span></pre></div></div>

<p>でインストールが完了です。configureにいろいろとオプションを渡していますが、これらは</p>
<ul>
<li>確実に使用するモジュールを静的にリンクさせる</li>
<li>不要なモジュールは無効にする</li>
<li>必要になるかもしれないモジュールはDSOとして組み込めるようにしておく</li>
</ul>
<p>という指定です。/usr/local/httpd_proxy_2.2.11 にApacheのファイル一式がインストールされているので、起動確認を行いましょう。</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>httpd_proxy_2.2.11<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>apachectl start</pre></div></div>

<p>でサーバが起動するので、http://localhost/ にアクセスしてみてください。&#8221;It works!&#8221;と表示されればインストール成功です。次回は具体的なリバースプロキシの設定方法を説明します。</p>
<div class="tmkm-amazon-view">
	<p><a href="http://www.amazon.co.jp/Apache%E3%83%9D%E3%82%B1%E3%83%83%E3%83%88%E3%83%AA%E3%83%95%E3%82%A1%E3%83%AC%E3%83%B3%E3%82%B9-POCKET-REFERENCE-WINGS-%E3%83%97%E3%83%AD%E3%82%B8%E3%82%A7%E3%82%AF%E3%83%88/dp/4774142239%3FSubscriptionId%3DAKIAJGVWECUONT35GEJA%26tag%3Dkazzhomeunixo-22%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D4774142239"><img src="http://ecx.images-amazon.com/images/I/51kZlS07-rL._SL160_.jpg" border="0" alt="" /></a></p>
	<p><a href="http://www.amazon.co.jp/Apache%E3%83%9D%E3%82%B1%E3%83%83%E3%83%88%E3%83%AA%E3%83%95%E3%82%A1%E3%83%AC%E3%83%B3%E3%82%B9-POCKET-REFERENCE-WINGS-%E3%83%97%E3%83%AD%E3%82%B8%E3%82%A7%E3%82%AF%E3%83%88/dp/4774142239%3FSubscriptionId%3DAKIAJGVWECUONT35GEJA%26tag%3Dkazzhomeunixo-22%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D4774142239">Apacheポケットリファレンス (POCKET REFERENCE)</a></p>
	<p><em>著者／訳者：</em>WINGS プロジェクト 高江 賢</p>
	<p><em>出版社：</em>技術評論社( 2010-04-10 )</p>
	<p>単行本（ソフトカバー） ( 496 ページ )</p>
<hr class="tmkm-amazon-clear" /></div>
]]></content:encoded>
			<wfw:commentRss>http://tech.lampetty.net/tech/index.php/archives/238/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://tech.lampetty.net/tech/index.php/archives/238" />
	</item>
		<item>
		<title>Apacheが出力するServerヘッダを隠す</title>
		<link>http://tech.lampetty.net/tech/index.php/archives/124?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=apache%25e3%2581%258c%25e5%2587%25ba%25e5%258a%259b%25e3%2581%2599%25e3%2582%258bserver%25e3%2583%2598%25e3%2583%2583%25e3%2583%2580%25e3%2582%2592%25e9%259a%25a0%25e3%2581%2599</link>
		<comments>http://tech.lampetty.net/tech/index.php/archives/124#comments</comments>
		<pubDate>Sat, 10 Jan 2009 11:57:21 +0000</pubDate>
		<dc:creator>oinume</dc:creator>
				<category><![CDATA[Apache]]></category>

		<guid isPermaLink="false">http://tech.lampetty.net/tech/?p=124</guid>
		<description><![CDATA[Apacheでは何も設定しないとデフォルトでレスポンスヘッダーに Server: Apache のような情報を残してしまいます。ServerTokens ProductOnly としてもApacheであることは隠せないの [...]]]></description>
			<content:encoded><![CDATA[<p>Apacheでは何も設定しないとデフォルトでレスポンスヘッダーに</p>
<pre>Server: Apache</pre>
<p>のような情報を残してしまいます。ServerTokens ProductOnly としてもApacheであることは隠せないので、mod_headersを使って</p>
<pre>Header unset Server</pre>
<p>とするのが正解なのか一応ぐぐってみたところ、<a href="http://oshiete1.watch.impress.co.jp/qa4210509.html">ここ</a>のような「ソースを修正する」という残念なアドバイスが載っていました。それmod_headersでできるよ！とつっこんであげたかったのですが、解答が締め切られていたのが残念です。</p>
<p>ちなみにPHPは何も設定しないと</p>
<pre>X-Powered-By: PHP/5.2.6-2ubuntu4
</pre>
<p>というようなヘッダーを出してしまうので、これもあわせて</p>
<pre>Header unset X-Powered-By</pre>
<p>しておきました。php.iniを修正すればいいらしいのですが、Ubuntuだとどこにあるのか調べるのが面倒だったのでApache側で対処。しかしデフォルトの設定っていうのは本当に怖いなぁと思いました。</p>
<p>2009/6/28 追記：Header unset Serverはリバースプロキシの環境でしかunsetできなさそうです。Header set Server hogeのようにして隠すことはできるはずです(未検証ですが)</p>
]]></content:encoded>
			<wfw:commentRss>http://tech.lampetty.net/tech/index.php/archives/124/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://tech.lampetty.net/tech/index.php/archives/124" />
	</item>
	</channel>
</rss>

