[Rails] Multiple application deployments on a single Apache host

Han Holl han.holl at informationslogik.nl
Thu Dec 2 10:03:56 GMT 2004


On Wednesday 01 December 2004 20:03, Damon Clinkscales wrote:
> Hi Everyone,
>
> I am setting up Rails for a team of developers to be able to set up
> their own apps easily to play with Rails.  Ideally this would look
> something like:
>
> http://servername/app1/list
> http://servername/app2/edit/0
> http://servername/app3/show/1
> etc...
>

Hello,

I posted a page with a apache configuration to do that on the rails wiki, but 
unfortunately it got lost.
I'm attaching it now, because the lines are a bit uncomfortably long for 
email.
With this method you can do what you want just fine, and avoid per directory 
rewriting which is a hack, really.
Of course you have to take care with relative URLs in the application, but 
resource sharing is much easier.

Cheers,

Han Holl
-------------- next part --------------
# Directory based Rails apache configuration without per directory mod_rewrite
# Two global replaces (in the right order) should suffice to relocate
# This file can be dropped in /etc/httpd/conf.d as rails.conf -- no need to touch httpd.conf
# Can co-exist with other parts of a website and other rails applications
alias /rails /var/www/rails/public

<IfModule !mod_fastcgi.c>
  LoadModule fastcgi_module modules/mod_fastcgi.so
</IfModule>
FastCgiServer /var/www/rails/public/dispatch.fcgi -socket /var/tmp/rails.socket
RewriteLog /var/log/rewrite.log
RewriteLogLevel 5
RewriteEngine On

# Skip rest of rewrite if url doesn't start with /rails
# Yes, S=14 is rather fragile, but it's the best we can do at this time
RewriteCond %{REQUEST_URI} !^/rails/?
RewriteRule .* - [S=15]
# Skip rest of rewrite if there is no controller
# Use PT to let mod_alias do the remapping
RewriteCond %{REQUEST_URI} !^/rails(/fcgi/|/mruby/|/)([-_a-zA-Z0-9]+)/
RewriteRule .* - [S=14,PT]
RewriteCond %{REQUEST_URI} ^/rails(/fcgi/|/mruby/|/)([-_a-zA-Z0-9]+)/
RewriteCond /var/www/rails/public/../app/controllers/%2_controller.rb !-f 
RewriteRule .* - [S=13,PT]
# Remove /rails from URL
RewriteRule ^/rails(.*) $1
# Force fcgi
RewriteRule ^/fcgi/([-_a-zA-Z0-9]+)/([-_a-zA-Z0-9]+)/([-_a-zA-Z0-9.]+)$ /var/www/rails/public/dispatch.fcgi?controller=$1&action=$2&id=$3 [L,QSA]
RewriteRule ^/fcgi/([-_a-zA-Z0-9]+)/([-_a-zA-Z0-9]+)$ /var/www/rails/public/dispatch.fcgi?controller=$1&action=$2 [L,QSA]
RewriteRule ^/fcgi/([-_a-zA-Z0-9]+)/?$ /var/www/rails/public/dispatch.fcgi?controller=$1&action=sql [L,QSA]
RewriteRule ^/fcgi.* /var/www/rails/public/dispatch.fcgi?controller=dps&action=sql [L,QSA]

# Force mod_ruby
RewriteRule ^/mruby/([-_a-zA-Z0-9]+)/([-_a-zA-Z0-9]+)/([-_a-zA-Z0-9.]+)$ /var/www/rails/public/dispatch.rb?controller=$1&action=$2&id=$3 [L,QSA]
RewriteRule ^/mruby/([-_a-zA-Z0-9]+)/([-_a-zA-Z0-9]+)$ /var/www/rails/public/dispatch.rb?controller=$1&action=$2 [L,QSA]
RewriteRule ^/mruby/([-_a-zA-Z0-9]+)/?$ /var/www/rails/public/dispatch.rb?controller=$1&action=sql [L,QSA]
RewriteRule ^/mruby.* /var/www/rails/public/dispatch.rb?controller=dps&action=sql [L,QSA]

# Default rewriting rules. Change extension from .cgi to .fcgi to switch to FCGI and to .rb to switch to mod_ruby
RewriteRule ^/([-_a-zA-Z0-9]+)/([-_a-zA-Z0-9]+)/([-_a-zA-Z0-9.]+)$ /var/www/rails/public/dispatch.cgi?controller=$1&action=$2&id=$3 [L,QSA]
RewriteRule ^/([-_a-zA-Z0-9]+)/([-_a-zA-Z0-9]+)$ /var/www/rails/public/dispatch.cgi?controller=$1&action=$2 [L,QSA]
RewriteRule ^/([-_a-zA-Z0-9]+)/$ /var/www/rails/public/dispatch.cgi?controller=$1&action=sql [L,QSA]
RewriteRule .* /var/www/rails/public/dispatch.cgi?controller=sql&action=show [L,QSA]


<Directory /var/www/rails/public>
  AllowOverride none
  Order allow,deny
  Allow from all
  
  # General Apache options
  AddHandler fastcgi-script .fcgi
  AddHandler cgi-script .cgi
  Options +FollowSymLinks +ExecCGI

  # Remember to set RubySafeLevel 0 in httpd.conf
  <IfModule mod_ruby.c>
    RubySafeLevel 0
    RubyRequire apache/ruby-run
    <Files dispatch.rb>
      SetHandler ruby-object
      RubyHandler Apache::RubyRun.instance
    </Files>
  </IfModule>

  # You can also point these error messages to a controller/action
  ErrorDocument 500 /rails/500.html
  ErrorDocument 404 /rails/404.html
</Directory>


More information about the Rails mailing list