Ruby on rails

Allikas: Kuutõrvaja
Redaktsioon seisuga 31. jaanuar 2007, kell 17:53 kasutajalt Triinu (arutelu | kaastöö)

Apache22 ruby on rails and mongrel

Süsteem toimib nii, et Apache suunab vhostide info mongrel serverile, mis siis railsi skripte esile manab.

Installime vajalikud päkitsad

cd /usr/ports/apache22
make install with_proxy_modules=yes
cd /usr/ports/lang/ruby18
make install && make clean
cd /usr/ports/devel/ruby-gems
make install && make clean
gem install mongrel
gem install rails


Loome vajalikud skriptid

pane rcrails /etc/init.d vms kausta ja start_mongrel iga railsi script kausta. start_mongreli sees määra USER ja GROUP, kellena see server jooksma peaks.

Meil on kaks eraldi railsis loodud programmi, mis asuvad

/home/rails/progeadmin/
/home/rails/veebiadmin/

/usr/local/etc/rails.conf või muusse faili (muuda vastavalt rcrails skripti) kirjuta näiteks:

RAILS_SERVERS="
/home/rails/progeadmin/
/home/rails/veebiadmin/
"

Ehitame ruudu alla näiteks skripti, mis stardib mongreleid nimega rcrails

#!/usr/local/bin/bash
#
# This is proxy skript to start-stop mongrel_rails servers
# This script can be placed in /etc/init.d or other rc startup directory
# and it will pass its first argument to each rails start script. 

conf="/usr/local/etc/mongrel.conf"

if [ -f $conf ]
then
  source $conf
fi

# function to do actual calling
start_stop_rails(){
  echo ""
  echo "Rails directory: $1"
  cd $1
  ./script/start_mongrel $2
}

# set CMD to first argument
CMD=$1

for i in $RAILS_SERVERS
do
  start_stop_rails $i $CMD
done

Loome skripti start_mongrel ja kopeerime kausta /home/rails/progeadmin/

#!/usr/local/bin/bash

USER="root"
GROUP="wheel"

case $1 in
  start)
    /usr/local/bin/mongrel_rails start -d --user $USER --group $GROUP -p 8001 -a 127.0.0.1 -P rails-8001.pid
    /usr/local/bin/mongrel_rails start -d --user $USER --group $GROUP -p 8002 -a 127.0.0.1 -P rails-8002.pid
    /usr/local/bin/mongrel_rails start -d --user $USER --group $GROUP -p 8003 -a 127.0.0.1 -P rails-8003.pid
    ;;
  restart)
    /usr/local/bin/mongrel_rails restart -P rails-8001.pid
    /usr/local/bin/mongrel_rails restart -P rails-8002.pid
    /usr/local/bin/mongrel_rails restart -P rails-8003.pid
  ;;
  stop)
    /usr/local/bin/mongrel_rails stop -P rails-8001.pid
    /usr/local/bin/mongrel_rails stop -P rails-8002.pid
    /usr/local/bin/mongrel_rails stop -P rails-8003.pid
  ;;
  *)
    cat <<EOF

USAGE: $0 <start|stop|restart>

Mongrel places it's pid files in the RAILS_ROOT directory, which must be writable by the calling user.

EOF
esac

Teeme teise skripti start_mongrel ning kopeerime /home/rails/veebiadmin/

#!/usr/local/bin/bash

USER="root"
GROUP="wheel"
 

case $1 in
  start)
    /usr/local/bin/mongrel_rails start -d --user $USER --group $GROUP -p 8101 -a 127.0.0.1 -P rails-8101.pid
    /usr/local/bin/mongrel_rails start -d --user $USER --group $GROUP -p 8102 -a 127.0.0.1 -P rails-8102.pid
    /usr/local/bin/mongrel_rails start -d --user $USER --group $GROUP -p 8103 -a 127.0.0.1 -P rails-8103.pid
    ;;
  restart)
    /usr/local/bin/mongrel_rails restart -P rails-8101.pid
    /usr/local/bin/mongrel_rails restart -P rails-8102.pid
    /usr/local/bin/mongrel_rails restart -P rails-8103.pid
  ;;
  stop)
    /usr/local/bin/mongrel_rails stop -P rails-8101.pid
    /usr/local/bin/mongrel_rails stop -P rails-8102.pid
    /usr/local/bin/mongrel_rails stop -P rails-8103.pid
  ;;
  *)
    cat <<EOF

USAGE: $0 <start|stop|restart>

Mongrel places it's pid files in the RAILS_ROOT directory, which must be writable by the calling user.

EOF
esac

httpd.conf

Kontrollime, et read oleksid

LoadModule proxy_module libexec/apache22/mod_proxy.so
LoadModule proxy_connect_module libexec/apache22/mod_proxy_connect.so
LoadModule proxy_ftp_module libexec/apache22/mod_proxy_ftp.so
LoadModule proxy_http_module libexec/apache22/mod_proxy_http.so
LoadModule proxy_ajp_module libexec/apache22/mod_proxy_ajp.so
LoadModule proxy_balancer_module libexec/apache22/mod_proxy_balancer.so

Nüüd loome mõlemale progele nende vhostid

<VirtualHost 193.40.0.10:80>
  ServerName progeadmin.katse.ee
  DocumentRoot /home/rails/progeadmin/

  <Directory "/home/rails/progeadmin/">
    Options FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow from all
  </Directory>

  RewriteEngine On


 # Redirect all non-static requests to cluster
 #RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
 RewriteRule ^/(.*)$ balancer://mongrel_cluster%{REQUEST_URI} [P,QSA,L]

</VirtualHost>
<VirtualHost 193.40.0.10:80>
 ServerName veebiadmin
 DocumentRoot /home/rails/veebiadmin/

 <Directory "/home/rails/veebiadmin/">
   Options FollowSymLinks
   AllowOverride None
   Order allow,deny
   Allow from all
 </Directory>


 RewriteEngine On

 # Rewrite to trailing slash
 RewriteRule ^/admin$ /admin/ [R,L]

 # test if this is /admin request
 RewriteCond %{REQUEST_URI} ^/admin(/.*)
 # strip off /admin and put result in ENV
 RewriteRule ^/admin(.*) /admin$1 [E=AdminReq:$1]

 RewriteCond /home/rails/admin/public/%{ENV:AdminReq} -f
 # serve static files
 RewriteRule ^/admin/(.*) /home/rails/admin/public/$1 [L]
 # If request is not static, proxy to rails
 RewriteRule ^/(admin/.*)$ balancer://mongrel_cluster2/$1 [P,QSA,L]


</VirtualHost>

Ehitame proxyd

<Proxy balancer://mongrel_cluster2>
   BalancerMember http://127.0.0.1:8001
   BalancerMember http://127.0.0.1:8002
   BalancerMember http://127.0.0.1:8003
</Proxy>
<Proxy balancer://mongrel_cluster>
   BalancerMember http://127.0.0.1:8101
   BalancerMember http://127.0.0.1:8102
   BalancerMember http://127.0.0.1:8103
</Proxy>

ja balaceri

Listen 8000
<VirtualHost 193.40.0.10:8000>
 <Location />
   SetHandler balancer-manager
   Deny from all
   Allow from localhost
 </Location>
</VirtualHost>

Lingid


http://blog.innerewut.de/articles/2006/04/21/scaling-rails-with-apache-2-2-mod_proxy_balancer-and-mongrel