[ruby]
[rails]
[config]
[fastcgi]
最近は lighttpd + fastcgi が流行のようですが、やっぱり apache + fastcgi を使わざるを得ない人向け。
fastcgiを導入するとき、毎回同じ事に引っかかってる気がするのでまとめておきます。
1. mod_fastcgiと、The Development Kit のインストール
公式サイトから入手できます。2. apache の設定を編集
参考例。バーチャルホストの設定は各自の環境に合わせて行う。
LoadModule fastcgi_module libexec/httpd/mod_fastcgi.so
<IfModule mod_fastcgi.c>
FastCgiIpcDir /tmp/fcgi_ipc/
AddHandler fastcgi-script .fcgi
</IfModule>
<VirtualHost *:80>
ServerName rails
DocumentRoot /path/to/application/public/
ErrorLog /path/to/application/log/apache.log
<Directory /path/to/application/public/>
Options ExecCGI FollowSymLinks
# Change this line for the one under
# AddHandler cgi-script .cgi
AddHandler fastcgi-script .fcgi
AllowOverride all
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
ここでapacheを再起動してみると、「/tmp/fcgi_ipc/ が読めないよ」というエラーが発生。
$ sudo /etc/init.d/httpd restart
httpd を停止中: [ OK ]
httpd を起動中: Syntax error on line 3 of /etc/httpd/conf.d/fastcgi.conf:
FastCgiIpcDir /tmp/fcgi_ipc: access for server (uid -1, gid -1) failed: read not allowed
[失敗]
確認してみると、所有者がこんな感じに・・・こりゃ当然無理だ。
$ ll /tmp/
合計 8
drwx------ 3 root root 4096 1月 21 23:27 fcgi_ipc
で、パーミッションを変更して再起動。
/tmp/fcgi_ipc/dynamicでも同様のエラーが発生したので修正。
最初からディレクトリは用意しておくのがベターなのかな。めんどくさい。
3. gemを使ってRuby用のFCGIバインディングをインストール
sudo gem install fcgi
4. RAILS_ROOT/public/.htaccess を編集
#RewriteRule ^(.*)$ dispatch.cgi [QSA,L]
RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
5. apacheを再起動して完了
アクセスしてみて、Application error が表示される場合は、RAILS_ROOT/tmp/ 以下のディレクトリのパーミッションを確認しましょう。 session 情報が書き込めないとfastcgiが、こんな感じのエラーを吐きます。FastCGI: incomplete headers (0 bytes) received from server "/opt/rails/app/current/public/dispatch.fcgi"