Ruby on Rails

From SilverRack - Affordable VPS Hosting - Wiki

Jump to: navigation, search

Contents

Apache / Mongrel Cluster

Sample Apache Config

Here's a sample Apache vhost entry for using Ruby on Rails and Mongrel Cluster:

NameVirtualHost *:80

<Proxy balancer://mongrel_cluster>
  BalancerMember http://127.0.0.1:5000
  BalancerMember http://127.0.0.1:5001
</Proxy>

<VirtualHost *:80>
  ServerName myserver.com
  DocumentRoot /var/www/myserver.com/current/public
  <Directory "/var/www/myserver.com/current/public">
    Options FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow from all
  </Directory>
  RewriteEngine On
  RewriteCond %{DOCUMENT_ROOT}/maintenance.html -f
  RewriteCond %{SCRIPT_FILENAME} !maintenance.html
  RewriteRule ^.*$ /maintenance.html [L]
  RewriteRule ^/$ /index.html [QSA]
  RewriteRule ^([^.]+)$ $1.html [QSA]

  RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
  RewriteRule ^/(.*)$ balancer://mongrel_cluster%{REQUEST_URI} [P,QSA,L]
  AddOutputFilterByType DEFLATE text/html text/plain text/xml
  BrowserMatch ^Mozilla/4 gzip-only-text/html
  BrowserMatch ^Mozilla/4\.0[678] no-gzip
  BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
  ErrorLog logs/myserver_error_log
  CustomLog logs/myserver_access_log combined
  ProxyTimeout 600
</VirtualHost>

Add PHP Support to Apache and Mongrel

Add these lines to your vhost's conf file:

 RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} \.php
 RewriteRule ^(.*)$ $1 [QSA,L]
 RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
 RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME}/index.php -f
 RewriteRule ^(.*)$ $1/index.php [QSA,L]

Apache / Passenger (mod_rails) on Ruby Enterprise Edition

This is a great way to improve the performance your your Rails applications. Installation and setup is pretty straightforward (assuming you have Apache installed already):

  1. Install Ruby Enterprise Edition
  2. Install Passenger
  3. Configure Apache

Phusion already provides great documention on installing REE and Passenger, so I'll skip those, and go right to configuration. Now I'm going do this in a virtual host, though if your Rails app is the only thing running, you could just do the same stuff in the general Apache config.

LoadModule passenger_module /opt/ruby-enterprise/lib/ruby/gems/1.8/gems/passenger-x.x.x/ext/apache2/mod_passenger.so
PassengerRoot /opt/ruby/enterprise/lib/ruby/gems/1.8/gems/passenger-x.x.x
PassengerRuby/opt/ruby-enterprise/bin/ruby
# btw, your can put your optimizations here!!!

NameVirtualHost *:80
<VirtualHost *:80>
  ServerName myserver.com
  DocumentRoot /path/to/myrailsapp/public
</VirtualHost>

There you go. Pretty much kills the Mongrel config above, right? Now go to the Reducing Memory Usage page to learn a little about optimizing Passenger for your VPS. Now what if you want to have your rails app at a sub-uri, e.g. /myrailsapp? This is how you do that:

NameVirtualHost *:80
<VirtualHost *:80>
  ServerName myserver.com
  DocumentRoot /var/www/myserver.com
  RailsBaseURI /myrailsapp
</VirtualHost>

Then you just need to create a symlink to your apps public directory:

ln -s /path/to/myrailsapp/public /var/www/myserver.com

Baaam!