* ApacheにFastCGIでデプロイ [#e651a145]

** 前提 [#ce9856e7]
- FastCGIの実装としてmod_fcgidを利用する。
- その他の前提は、[[ApacheにCGIでデプロイ>http://yanor.net/wiki/?Perl%2FMojo%2FApache%E3%81%ABCGI%E3%81%A7%E3%83%87%E3%83%97%E3%83%AD%E3%82%A4]]と同様。

** Apacheの設定 [#t616754e]
 <IfModule !mod_fcgid.c>
   LoadModule fcgid_module modules/mod_fcgid.so
 </IfModule>
 SocketPath /tmp/fcgid_socket
 
 <Directory /var/www/my_app/public>
   Options All
   AllowOverride All
 </Director>

** フロントコントローラ [#xcbda632]
/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;
 use Data::Dumper;
 
  
 $ENV{MOJO_APP} = 'MyApp';
 $ENV{BASE_URL} = '/myapp/';
 
 Mojo::Server::FastCGI->new->run;
- $ENV{BASE_URL}は本ウェブアプリ独自で使用する。
- $ENV{BASE_URL}はこの例でのウェブアプリ独自で使用する。

** mod_rewriteでフロントコントローラにリクエストを集める [#u5d3c7ea]
/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]


** アプリケーションクラスでのルーティング設定 [#n9797ad4]
 __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;
        }
    );
 }

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

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