Ruby on Rails

From SilverRack - Affordable VPS Hosting - Wiki

Jump to: navigation, search


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>

<VirtualHost *:80>
  DocumentRoot /var/www/
  <Directory "/var/www/">
    Options FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow from all
  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]

  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

Add PHP Support to Apache and Mongrel

Add these lines to your vhost's conf file:

 RewriteRule ^(.*)$ $1 [QSA,L]
 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/
PassengerRoot /opt/ruby/enterprise/lib/ruby/gems/1.8/gems/passenger-x.x.x
# btw, your can put your optimizations here!!!

NameVirtualHost *:80
<VirtualHost *:80>
  DocumentRoot /path/to/myrailsapp/public

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>
  DocumentRoot /var/www/
  RailsBaseURI /myrailsapp

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

ln -s /path/to/myrailsapp/public /var/www/