ApacheにFastCGIでデプロイ

前提

Apacheの設定

 <IfModule !mod_fcgid.c>
   LoadModule fcgid_module modules/mod_fcgid.so
 </IfModule>
 
 <Directory /var/www/my_app/public>
   Options All
   AllowOverride All
 </Director>

フロントコントローラ

/var/www/my_app/public/index.fcgiに設置する。

 #!/usr/bin/perl
 use strict;
 use warnings;
 use lib qw(
   /var/www/my_app/lib
 );
 use Mojo::Server::FastCGI;
  
 $ENV{MOJO_APP} = 'MyApp';
 $ENV{BASE_URL} = '/myapp/';
 
 Mojo::Server::FastCGI->new->run;
  • $ENV{BASE_URL}はこの例でのウェブアプリ独自で使用する。

mod_rewriteでフロントコントローラにリクエストを集める

/var/www/my_app/public/.htaccessに設置する。

 AddHandler fcgid-script .fcgi
 
 RewriteEngine On
 RewriteBase /myapp
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteRule ^(.*)$ index.fcgi [QSA,L]

アプリケーションクラスでのルーティング設定

 __PACKAGE__->attr( base_url => $ENV{BASE_URL} );
 
 sub startup {
    my $self = shift;
    my $base = $self->base_url;
    my $r = $self->routes;
    $r->route("${base}index")->to(controller => 'top', action => 'index')->name('top');
 
    # (注)
    $self->renderer->add_helper(
        url_for => sub {
            my $script_path = $self->base_url . $0;
            $script_path =~ s/\./\\./g;
            ( my $url = shift->url_for(@_) ) =~ s#$script_path##;
            return $url;
        }
    );
 }

  • このままだとurl_for()が"/my_app/index.fcgi/my_app/index"のようなURLを返すので、不要な"/my_app/index.fcgi"部分を削除している。
  • 同様の処理はMojolicious::Controllerクラスでも行う必要がある。

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS

Last-modified: 2010-11-23 (火) 19:37:02