{"id":16,"date":"2008-04-05T16:36:33","date_gmt":"2008-04-05T21:36:33","guid":{"rendered":"http:\/\/blog.monnet-usa.com\/?p=16"},"modified":"2008-04-05T16:49:56","modified_gmt":"2008-04-05T21:49:56","slug":"riding-out-of-the-rail-yard","status":"publish","type":"post","link":"https:\/\/blog.monnet-usa.com\/?p=16","title":{"rendered":"Riding out of the rail yard"},"content":{"rendered":"<p>For the last several months, I have been feeling the pain of shared hosting. My initial Ruby on Rails installation was stuck in the rail yard! Although I had cross-referenced my setup against many rails sites and blogs, I could not seem to get a ruby train out of my rail yard!!! Executing Ruby and CGI-based Ruby programs was fine. But no Rails! You see, my host &#8211; even with a business plan &#8211; does not let you touch your Apache <strong>httpd.conf<\/strong>. My only option is to perform limited configuration using <strong>.htaccess<\/strong> files. Moreover, the shared host settings are tucked away from view. Today, after weeks of various research and experiments, I discovered that handlers for CGI and FastCGI seemed to be defined at a high-level in the shared hosting setup. So overriding them seem to wreak havoc and interfere with Rails.<\/p>\n<p>Here is what I had to do:<\/p>\n<p>1. Typically a Rails rake-generated .htaccess sets the CGI and FastCGI handlers like so:<\/p>\n<p>[code]AddHandler fastcgi-script .fcgi<br \/>\nAddHandler cgi-script .cgi[\/code]<br \/>\nHowever I discovered that for my shared host setup, if I comment the handlers out (or remove them altogether), then the <strong>dispatch.fcgi<\/strong> script acting as a front-controller for Rails starts working correctly, i.e. all Apache HTTP variables get passed correctly.<\/p>\n<p>2. The .htaccess also includes Apache &#8220;voodoo&#8221; to rewrite your urls and specify that you want urls for your Rails app to be processed by the Ruby on Rails Fast CGI dispatcher. Like so:<\/p>\n<p>[code]RewriteCond %{REQUEST_FILENAME} !-F<br \/>\nRewriteRule ^(.*)$ \/dispatch.fcgi [QSA,L][\/Code]<br \/>\nIn plain English, this means: &#8220;If a url represents a resource which does not exist as a physical file, then have my Fast CGI dispatcher process the request&#8221;.<\/p>\n<p>Unlike ASP.NET, JSP, PHP or other dynamic web page based apps, a Rails application exposes its features through a multi-level URL domain. Example:<\/p>\n<p>[html]http:\/\/myapp.myhost.com\/account\/register<br \/>\nhttp:\/\/myapp.myhost.com\/account\/updateprofile<br \/>\nhttp:\/\/myapp.myhost.com\/blog\/post[\/html]<\/p>\n<p>In the classic ASP.NET world, specific .aspx pages are created to expose this functionality. Btw Microsoft is now providing a similar approach with the new MVC framework (see <a title=\"http:\/\/www.asp.net\/mvc\/\" href=\"http:\/\/www.asp.net\/mvc\/\">http:\/\/www.asp.net\/mvc\/<\/a>).<br \/>\nThe shared hosting twist was that a very precise location must be provided for the Fast CGI, i.e. for a subdomain mapped to the public folder in Rails, the root (<strong>slash<\/strong>) must be included in front of the name!!! Obvious once you discover that, but oblivious when all examples anywhere never show that!<\/p>\n<p>[code]RewriteRule ^(.*)$ \/dispatch.fcgi [QSA,L][\/code]<\/p>\n<p>3. The final step was to remove the <strong>dispatch.cgi<\/strong> also generated by rake since I am only using Fast CGI. It seems that keeping it was also interfering.<br \/>\nPhew!!! Now navigating to any Rails url works!!!<\/p>\n<p>Here are a couple useful links worth adding to your del.icio.us collection:<\/p>\n<ul>\n<li>.htaccess<\/li>\n<\/ul>\n<p><a title=\"http:\/\/httpd.apache.org\/docs\/trunk\/howto\/htaccess.html\" href=\"http:\/\/httpd.apache.org\/docs\/trunk\/howto\/htaccess.html\">http:\/\/httpd.apache.org\/docs\/trunk\/howto\/htaccess.html<\/a><br \/>\n<a title=\"http:\/\/perishablepress.com\/press\/2006\/01\/10\/stupid-htaccess-tricks\/\" href=\"http:\/\/perishablepress.com\/press\/2006\/01\/10\/stupid-htaccess-tricks\/\">http:\/\/perishablepress.com\/press\/2006\/01\/10\/stupid-htaccess-tricks\/<\/a><\/p>\n<ul>\n<li>Fast CGI<\/li>\n<\/ul>\n<p><a title=\"http:\/\/www.fastcgi.com\/devkit\/doc\/fastcgi-whitepaper\/fastcgi.htm\" href=\"http:\/\/www.fastcgi.com\/devkit\/doc\/fastcgi-whitepaper\/fastcgi.htm\">http:\/\/www.fastcgi.com\/devkit\/doc\/fastcgi-whitepaper\/fastcgi.htm<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>For the last several months, I have been feeling the pain of shared hosting. My initial Ruby on Rails installation was stuck in the rail yard! Although I had cross-referenced my setup against many rails sites and blogs, I could not seem to get a ruby train out of my rail yard!!! Executing Ruby and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-16","post","type-post","status-publish","format-standard","hentry","category-ruby"],"_links":{"self":[{"href":"https:\/\/blog.monnet-usa.com\/index.php?rest_route=\/wp\/v2\/posts\/16","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.monnet-usa.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.monnet-usa.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.monnet-usa.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.monnet-usa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=16"}],"version-history":[{"count":0,"href":"https:\/\/blog.monnet-usa.com\/index.php?rest_route=\/wp\/v2\/posts\/16\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.monnet-usa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=16"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.monnet-usa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=16"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.monnet-usa.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=16"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}