tag:blogger.com,1999:blog-74620878369912259862024-03-12T18:47:19.259-07:00tonyrose023Tony Rosehttp://www.blogger.com/profile/05343435312378065687noreply@blogger.comBlogger1125tag:blogger.com,1999:blog-7462087836991225986.post-90111832588520235492007-01-29T17:23:00.000-08:002007-01-31T14:24:59.109-08:00Multiple Ruby on Rails apps with Mongrel and ApacheThis post will show how to deploy multiple Ruby on Rails applications using Apache2.2 and Mongrel Clusters. The goal is to serve many apps under the same domain, for example:<br /><br /><span style="font-weight: bold;">http://www.domain.com/app1<br />http://www.domain.com/app2</span><br /><br />The topic was discussed on the Mongrel mailing list (<a href="http://rubyforge.org/pipermail/mongrel-users/2006-September/001498.html">http://rubyforge.org/pipermail/mongrel-users/2006-September/001498.html</a>), but even after reading it over and over for a few days, I could not make any of the suggestions there work, hence this post of what I did to make it work.<br /><br /><span style="font-size:130%;"><span style="font-weight: bold;">Preparation: </span></span>Get 1 Ruby on Rails app deployed first! For the instructions on getting a single Ruby on Rails app running using Apache2.2, Capistrano and Mongrel, check out these excellent tutorials. I use the configuration file layout suggested by the codahale blog.<br /><br /><a href="http://mongrel.rubyforge.org/docs/apache.html">http://mongrel.rubyforge.org/docs/apache.html</a><br /><a href="http://blog.codahale.com/2006/06/19/time-for-a-grown-up-server-rails-mongrel-apache-capistrano-and-you/">http://blog.codahale.com/2006/06/19/time-for-a-grown-up-server-rails-mongrel-apache-capistrano-and-you/</a><br /><br />Once you have a single app deployed and working properly, it is relatively easy to get the second one running under your domain - if you know what to do. You will configure and deploy your second app exactly the same way you did the first, with one exception. For both apps, you need to change the mongrel configuration to use the --prefix argument, besides that it is only 2 changes to your apache configuration. Here are the steps in detail:<br /><br /><span style="font-size:130%;"><span style="font-weight: bold;">Step 1) </span></span>Use the --prefix flag of the mongrel configuration, here are 2 example mongrel_cluster.yml files.<br /><br /><span style="font-family:arial;">---</span><br /><span style="font-family:arial;">prefix: /app1</span><br /><span style="font-family:arial;">cwd: /var/www/rails/app1/current</span><br /><span style="font-family:arial;">port: "8000"</span><br /><span style="font-family:arial;">environment: production</span><br /><span style="font-family:arial;">address: 127.0.0.1</span><br /><span style="font-family:arial;">pid_file: log/mongrel.pid</span><br /><span style="font-family:arial;">servers: 2</span><br /><br />----------------------------------------------------------------------------------<br /><span style="font-family:arial;">---</span><br /><span style="font-family:arial;">prefix: /app2</span><br /><span style="font-family:arial;">cwd: /var/www/rails/app2/current</span><br /><span style="font-family:arial;">port: "8010"</span><br /><span style="font-family:arial;">environment: production</span><br /><span style="font-family:arial;">address: 127.0.0.1</span><br /><span style="font-family:arial;">pid_file: log/mongrel.pid</span><br /><span style="font-family:arial;">servers: 2</span><br /><br /><br /><span style="font-size:130%;"><span style="font-weight: bold;">Step 2) </span></span>Modify your RewriteRules in ds.common to redirect to the appropriate app. Here is what I have in my ds.common for serving dynamic content.<br /><br /><span style="font-family:arial;"> # Redirect all non-static requests to the right cluster</span><br /><span style="font-family:arial;"> RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f</span><br /><span style="font-family:arial;"> RewriteRule ^/app1(.*)$ balancer://app1%{REQUEST_URI} [P,QSA,L]</span><br /><span style="font-family:arial;"> RewriteRule ^/app2(.*)$ balancer://app2%{REQUEST_URI} [P,QSA,L]</span><br /><br /><br /><span style="font-size:130%;"><span style="font-weight: bold;">Step 3)</span></span> Add a new cluster to proxy_cluster.com.<br /><blockquote><Proxy balancer://app1 ><br />BalancerMember http://127.0.0.1:8000<br />BalancerMember http://127.0.0.1:8001<br /></Proxy ><br /><br /><Proxy balancer://app2 ><br />BalancerMember http://127.0.0.1:8010<br />BalancerMember http://127.0.0.1:8011<br /></Proxy ><br /></blockquote>Tony Rosehttp://www.blogger.com/profile/05343435312378065687noreply@blogger.com9