How to set up a Rails application with Mongrel and Apache
ApacheでMongrelのセットアップ
1)mongrel_cluster.ymlをダウンロードし、hostname特有のPIDを書き込みます。
# cd /home/YOUR/PATH/TO/RAILS/APP
# svn export http://svn.joyent.com/public/accelerators/configs/mongrel/mongrel_cluster.yml config/mongrel_cluster.yml
2)mongrel clusterを起動。
# cd /home/YOUR/PATH/TO/RAILS/APP
# mongrel_rails cluster::start
3)チェック。
# curl `myprivateip`:8000
# curl -I `myprivateip`:8000
200OKとなっていればOK
HTTP/1.1 200 OK
Connection: close
Date: Tue, 06 Mar 2007 07:09:13 GMT
Status: 200 OK
Server: Mongrel 1.0.1
Content-Type: text/html
Content-Length: 0
なっていなければ、production.logをチェック。
4)mongrelのプロキシバランサにあわせてapacheのバーチャルホストを設定
# cd /opt/csw/apache2/etc/virtualhost
# nano myrailsapp-domain.conf
中身はこんな感じ
<VirtualHost YOUR-PUBLIC-IP:80>
ServerName YOURDOMAIN.com >ServerAlias *.YOURDOMAIN.com
DocumentRoot /home/YOUR/PATH/TO/RAILS/APP/public
<Directory "/home/YOUR/PATH/TO/RAILS/APP/public/">
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^/(.*)$ balancer://app1-mongrels%{REQUEST_URI} [P,QSA,L]
<Proxy balancer://app1-mongrels>
BalancerMember http://YOUR-PRIVATE-IP:8000
BalancerMember http://YOUR-PRIVATE-IP:8001
BalancerMember http://YOUR-PRIVATE-IP:8002
BalancerMember http://YOUR-PRIVATE-IP:8003
</Proxy>
ProxyPass /images !
ProxyPass /stylesheets !
ProxyPass / balancer://app1-mongrels
ProxyPassReverse / balancer://app1-mongrels
ProxyPreserveHost On
</VirtualHost>
- ProxyPass /images ! と ProxyPass /stylesheets !を記述しないとサーバがうまく動かないらしい。
- “balancer://app1-mongrels”にはユニークな名前を。
- ポートはmongrel_cluster.ymlと同じにする。
apacheを再起動
apachectl restart
指定したポートで動作しなければ、再度設定を繰り返す事。
5)最後に、リブートの準備を行う。
# nano /HOME/YOUR/PATH/TO/RAILS/APP/mongrel-app-smf.xml
<?xml version='1.0'?>
<!DOCTYPE service_bundle SYSTEM '/usr/share/lib/xml/dtd/service_bundle.dtd.1'>
<service_bundle type='manifest' name='mongrel/YOURAPP-production'>
<service name='network/mongrel/YOURAPP->production' type='service' version='0'>
<create_default_instance enabled='true'/>
<single_instance/>
<dependency name='fs' grouping='require_all' restart_on='none' type='service'>
<service_fmri value='svc:/system/filesystem/local'/>
</dependency>
<dependency name='net' grouping='require_all' restart_on='none' type='service'>
<service_fmri value='svc:/network/loopback'/>
<!-- need nfs/>client because the mongrel stuff is on />home which is nfs mounted -->
<service_fmri value='svc:/network/nfs/client'/>
</dependency>
<dependent name='mongrel_multi->user' restart_on='none' grouping='optional_all'>
<service_fmri value='svc:/milestone/multi-user'/>
</dependent>
<exec_method name='start' type='method' exec='/>opt/csw/bin/mongrel_rails cluster::start' timeout_seconds='60'>
<method_context working_directory='/HOME/YOUR/PATH/TO/RAILS/APP'>
<method_credential user='USER' group='GROUP' />
<method_environment>
<envvar name="PATH" value="/usr/bin:/bin:/opt/csw/bin" />
</method_environment>
</method_context>
</exec_method>
<exec_method name='stop' type='method' exec=':kill' timeout_seconds='60'>
<method_context/>
</exec_method>
</service>
</service_bundle>
# cd /HOME/YOUR/PATH/TO/RAILS/APP/
# mongrel_rails cluster::stop
# rm log/*.pid
# svccfg import /HOME/YOUR/PATH/TO/RAILS/APP/mongrel-app-smf.xml
# svcadm enable mongrel/YOURAPP-production
# svcs -v | grep mongrel