Resource mapping works locally but not on webserver - ruby-on-rails

Edit:
Well maybe this can give a clue. When created a Rails app in cPanel on my web host, in order to access the app you need to create a rewrite rule. Which I did, but didn't know what it did. I tracked it down to it creating the following 2 files:
home/mydomain/public_html/.htaccess:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mydomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.mydomain.com$
RewriteRule ^/?$ "http\:\/\/127\.0\.0\.1\:12030%{REQUEST_URI}" [P,QSA,L]
and
home/mydomain/.cpanel/ruby-on-rails-rewrites.db:
appname: helloworld
domain: mydomain.com
port: 12030
rewritebasepath: /home/mydomain/public_html
url:
It seems that the root URL gets sent to Rails, but any other URL does not and I get an Apache 404 (not a Rails 404). Is there something wrong in the .htaccess file?
Is the rewrite condition only for the domain root? If it is, how can I make it for any URL ?
Original post:
I'm trying to deploy a rails website that works fine locally but not on the web server.
The rails app starts fine and I can access the home page. But any other link results in a 404.
My home page is set from within routes.rb like so:
map.root :controller => "home"
I am using RESTful routes and it works fine locally. In routes.rb all I have is a bunch of
map.resources :posts
map.resources :images
...
If I change the map.root to another controller (i.e map.root :controller => "posts", then that single page will display fine as well, and all other links will return 404s, including the one that previously worked when it was the one referenced in map.root.
This is my first rails deployment so it might be me doing something wrong. My web host tech support told me it's a coding issue and that they can't help me.
My website directory structure online is like so:
/home/mydomain/etc/rails_apps/mysitedir/ <- rails folders (app, config, db, ...)
/home/mydomain/public_html/ <- nothing here
Also, the files that are in my rails public folder, do they need to stay in that folder or should they be put in the public_html folder on the web? Because right now they're not found either. If I put them in the public_html folder though, they are found from the home page (javascript and css and images).
In my error log in cPanel, it shows errors like:
[Mon Oct 05 08:19:41 2009] [error] [client XX.XX.XX.XX]
File does not exist: /home/mydomain/public_html/market,
referer: http://www.mydomain.com/
Where market is one of the links I'm trying to access from my home page, for example, and it's one of my controllers.
In short, the only location I can access is the root.
What am I doing wrong that I am getting all these 404s?
I do not have shell access, all I have is cPanel. This is with Ruby 1.8.5 and Rails 2.3.4, the (shared) web server is Linux + Apache 2.2.11 (Unix).

You need to get your web root to point to /home/mydomain/etc/rails_apps/mysitedir/public/ Maybe this can be configured in cpanel?
If you can get access the shell you would do something like(syntax may vary slightly):
ln -s /home/mydomain/etc/rails_apps/mysitedir/public/ /home/mydomain/public_html
Note that you need to remove public_html first, you should verify that there are no negative consequences of this.

The problem is with my web host. Still hasn't been resolved by them.

I came across the exact same problem and found a fix. In the .htaccess file, remove that first '$' so you have:
RewriteRule ^/? "http\:\/\/127\.0\.0\.1\:12030%{REQUEST_URI}" [P,QSA,L]
That first bit is a Regex, so the $ matches the end of the URL.
As a side-rant, this is the umpteenth problem I've had to solve today because cPanel sucks.

Related

Ruby On Rails on eHost

Is it possible to just upload my RoR files to my server (eHost) and have my website up and running?
If not, what should I do in order to have it working on any Host with support for RoR? I know of heroku but it is quite expansive for me right now. eHost charges only $3.27 per month and they claim to have support for RoR. I even called Tech Support and asked them to update the vertion to 2.3.0(Ruby) and 4.2.4(Rails).
I am new on RoR and have no idea of how to host my application on the ordinary hosts out there. So any help will be very welcome.
I've not tried this, I only hope this gives you an idea,
They say they support Ruby and RoR, So taking a look https://support.ehost.com/articles/ruby-on-rails-and-gems they say it's on Apache CGI server.
So - this is my suggestion - you upload your project, and either you contact support or you might want to try the suggestion here:
http://blog.hulihanapplications.com/browse/view/52-how-to-install-a-ruby-on-rails-app-part-2 I'll quote the section (in case it ever becomes unavailable). It's under the section FastCGI/CGI:
Configure .htaccess
Apache usually looks in any web directory for an Override file(which lets you set your own Apache configuration settings for the directory) called .htaccess. For ruby on rails, we need a special .htaccess file that tells the server to set up the directory as a ruby on rails application. This file should be created in the public directory if it's not already there. Here's what the contents of the file should look like:
#AddHandler fastcgi-script .fcgi
#AddHandler fcgid-script .fcgi
AddHandler cgi-script .cgi
Options +FollowSymLinks +ExecCGI
RewriteEngine On
RewriteRule ^$ index.html [QSA]<br>RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ dispatch.cgi [QSA,L]
ErrorDocument 500 "<h2>Application error</h2>Rails application failed to start properly"
Linking to public
The public directory inside your rails application contains all your public resources(images, css, javascript, etc.), and well as the .htaccess file needed to run your application. Until now, your public directory hasn't been visible to the outside world. Now we'll link your application to a directory inside your web root.
For example, if I was planning on running my rails application in a subdirectory of my website called www.mywebsite.com/myrailsapp, you'll need to create a symbolic link(it's kind of like a shortcut) in your web root directory(remember, this is where your regular website stuff goes, usually named public_html or www). Let's also pretend that on the server, the path to your web root is /home/myaccount/public_html and the path to your rails application is /home/myaccount/myrailsapp. Therefore, the command to create a symbolic link will be:
ln -s /home/myaccount/myrailsapp/public /home/myaccount/public_html/myrailsapp

How to serve an api subdomain using apache, passenger, and rails?

I created a subdomain like the following myapp.example.com and it works great ...has been. Now I want to build an API using rails for the app (rails itself) so I created the subdomain api.myapp.example.com. Currently the problem I'm getting is the api subdomain isn't getting routed to the rails app.
I tried adding the following to the apache conf file in /etc/apache2/sites-enabled/example.com.conf
# example.com.conf
ServerAlias *.myapp.example.com
I then restarted Apache, but still api.myapp.example.com wasn't being routed to rails application.
Ahhh, it ended being an easy fix
# example.com.conf
ServerAlias myapp.example.com api.myapp.example.com

recently switched from PHP to Rails Passenger. Can't make index.php redirect to /

Rails 3 on phusion passenger with apache
on development server in routes.rb
match "/index.php" => redirect("/")
works fine. In production it throws a 404 and its an Apache 404 page not a Rails 404 page
Took out DirectoryIndex from httpd.conf and .htaccess.
If you make a redirect like /sub/index.php it works fine just not in the root.
Anyone know what the problem could be or how to work around? I am afraid people have bookmarked index.php
Instead of redirecting, does it work if you just point index.php straight at the root controller/action?
match "/index.php" => "welcome#index"

Root of rails application serves wrong kind of file

I am using Ruby on Rails on a CentOS-server with apache 2.0. But somehow the root of the website does not load. It comes up as a download, with the right contents. But it needs to be server as html. Other links within the site are working fine (by adding a text/html type to the .htaccess). Also, when accessing the mongrel directly root does come up. Or when i put a index.html in the public folder of the rails app, it gets loaded as well.
My .htaccess:
DefaultType text/html # added for the other pages to get them working, like /products
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule .* "http://127.0.0.1:12001%{REQUEST_URI}" [P,QSA,L]
Rails version: 2.3.8
Mongrel version: 1.1.5
Apache version: 2.0.63
Due to a bug in mongrel_start, it wasn't working properly. This fixed the problem. Hope it can be a help for someone else in the future as well.

Redmine doesn't work properly in Apache

I'm trying to get Redmine (a Ruby on Rails app) working. It works fine when started with ruby script/server webrick -e production, however, I'm having trouble getting it working in Apache with Passenger.
Accessing http://example.com/redmine returns the Redmine home page, but clicking any link (or even adding a / to the URL) results in a 404. According to the Rails log, a RoutingError occurs. For example, when opening the projects page: ActionController::RoutingError (No route matches "/projects.html" with {:method=>:get})
The Redmine directory is /var/www/localhost/htapps/redmine. I followed the documentation at http://www.modrails.org/documentation/Users%20guide.html#_deploying_a_ruby_on_rails_application (section 3.2), so there's a symlink at /var/www/localhost/htdocs/redmine pointing to ../htapps/redmine/public, and the Apache configuration contains DocumentRoot /var/www/localhost/htdocs and RailsBaseURI /redmine.
What is causing it to raise these RoutingErrors?
It looks like this issue was actually caused by the default .htaccess included with Redmine.
Redmine's .htaccess rewrites every request to end with .html. Redmine's routes expect .html-less requests.
Setting RewriteEngine to Off solved the issue for me.
http://ptspts.blogspot.com/2009/05/how-to-fix-railsbaseuri-sub-uri-with.html
The manual workaround (according to what is suggested on the pages above) is adding the line below to config/environments/production.rb:
config.action_controller.relative_url_root = '/redmine'
If not deploying to a Sub-URI and deploying using passenger, adding the rule RewriteRule ^(.*)$ dispatch.fcgi [QSA,L] to your public/.htaccess also solves the issue. As that rule is IfModule-ed out in the default .htaccess.
Another option is to delete .htaccess if you're not using it (As an example you may be using it for additional layer authentication with AuthType Digest etc). It is not required when deploying with passenger.
Even if you will manage to run Redmine in suburi, redmine still will have issues.
Some pages won't be parsed and displayed correctly, if displayed at all.
This issue is almost one year old and indicated for next minor release. Btw dozen minor releases came out, but it's not yet fixed. FCGI mode does not support sub-URI.

Resources