4

Now I am pretty new to Sinatra/Ruby/Apache but have inherited a Sinatra application to deploy.

Currently Apache is set up to run from a document root (httpdocs) and I need to run a ruby application underneath a folder subdirectory such as: /httpdocs/webapp

What do I need to do to get this up and running under a subdirectory?

Donald
  • 505
  • 1
  • 7
  • 19

2 Answers2

2

This link might be helpful, it explains how to deploy a Sinatra app with Apache using Passenger (mod_rack): Deploying a Sinatra App with Apache and Phusion Passenger

The part of particular interest to you is the RackBaseURI option in the virtual host configuration. The official documentation is available here: Phusion Passenger users guide - Deploying Rack to Sub URI

brentvatne
  • 7,603
  • 4
  • 38
  • 55
  • Got it. But the apps in the subdirectory are pointing '/' to the parent application. How can I it be pointed to the root of each app? – gcstr Oct 13 '11 at 19:26
  • goo: if I understand your problem correctly, any url references generated by a sinatra app in a subdirectory are not taking the base url included the subdirectory name into account. if this is the problem, you can look at a gem like: [sinatra-url-for](https://github.com/emk/sinatra-url-for/) or create a helper as suggested in this discussion [get absolute base url in sinatra](http://stackoverflow.com/questions/2950234/get-absolute-base-url-in-sinatra) – brentvatne Oct 13 '11 at 22:28
  • goo: As @brentvatne says you should always use an URL helper - since Sinatra 1.2.0 there's a [helper included](http://www.sinatrarb.com/2011/03/03/sinatra-1.2.0.html#url_helper). Additionally I had weird results because I specified the Apache `RackBaseURI` option inside of the `Directory` directive in the Apache configuration, also it seems to be important to use the symlink method described in the article @brentvatne linked. – rkallensee Apr 13 '12 at 11:52
1

I just ran into the same issue. Since there was no answer here for how to do this without Passenger, I'm going to document the solution for Apache + CGI or FastCGI.

The trick is to rewrite PATH_INFO for Rack, and everything will fall into place:

  1. Set up .htaccess:

     RewriteEngine On
     RewriteRule ^(.*)$ sinatra_app.cgi [QSA,L,E=PATHINFO:/$1]
    
  2. In your Sinatra code, before anything else:

     ENV['PATH_INFO'] = ENV['REDIRECT_PATHINFO']
    

Now all URLs like /subfolder/resource/123 will hit the correct route in the Sinatra app.
In the above case get '/resource/:id' will work correctly, assuming the Sinatra app was put into /subfolder.

Casper
  • 33,403
  • 4
  • 84
  • 79