How to deploy Rails 3.1 app in a subdirectory - ruby-on-rails

How do I configure a Rails 3.1 application to run under a specific directory such as "/r"?
I tried in config.ru:
map '/r' do
run Debtor::Application
end
but that just returned "Not Found: /r"
To get it to work I had to enclose all routes in a scope:
scope '/r' do
#routes
end
and to add the following line to config/applcation.rb
config.assets.prefix = "/r/assets"
and to move my jquery ui css files from /stylesheets to /r/stylesheets.
this seems too complicated. isn't there an easier way? and why isn't my config.ru setting working?
my use case is to have a rails powered ajax backend for a wordpress server.

are you running under passenger?
Then RailsBaseURI is probably what you want.
https://www.phusionpassenger.com/library/deploy/apache/deploy/ruby/#deploying-an-app-to-a-sub-uri
If not running under passenger, please update your question to show what you are deployed under.

What worked for me was creating the symbolic link for the sub-uri (/info) to the 'public' folder of the application (setup under another user on my server, /home/otheruser/current/public).
ln -s /home/myapp/current/public /home/mysite/public_html/info
Then I inserted this configuration inside of the VirtualHost entry for the site:
Alias /info /home/myapp/current/public
<Location /info>
PassengerAppRoot /home/myapp/current
RackEnv production
RackBaseURI /info
</Location>
No scoped routes, no asset prefix configuration.

Here’s how to deploy a Rails 3.1 app to a subdirectory in Apache, replacing config.action_controller.relative_url_root which no longer exists.
In config/routes.rb:
scope 'my_subdir' do
# all resources and routes go here
end
In your Apache configuration file:
Alias /my_subdir /var/www/my_subdir/public
<Location /my_subdir>
SetEnv RAILS_RELATIVE_URL_ROOT "/my_subdir"
PassengerAppRoot /var/www/my_subdir
</Location>
And it should work, including automatically pointing all your assets to /my_subdir.

Related

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

Allow an Apache directory listing from a subdirectory of rails "public/"?

I'd like to enable Apache directory listings on a specific subdirectory of my Rails application's "public/" directory. For the sake of this discussion, let's say it is the "public/listme"
I've set Apache options to enable directory listings, but if I request something like http://mydomain.com/listme, I get a 404 error, and if I check the Rails log, I get an error like:
ActionController::RoutingError (No route matches "/listme")
So: is there a way to disable routing for a specific folder within my Rails app and allow Apache's automatic directory listings to kick in?
It turns out that this question is answered in the modrails documentation. See http://www.modrails.com/documentation/Users%20guide%20Apache.html
In summary :
Suppose that you have a Rails application in /apps/foo. Suppose that you’ve dropped Wordpress — a blogging application written in PHP — in /apps/foo/public/wordpress. You can then configure Phusion Passenger as follows:
<VirtualHost *:80>
ServerName www.foo.com
DocumentRoot /apps/foo/public
<Directory /apps/foo/public/wordpress>
PassengerEnabled off
AllowOverride all # <-- Makes Wordpress's .htaccess file work.
</Directory>
</VirtualHost>

Changing the base URL for Rails 3 development

I know I'm going to deploy to an environment with my application running with a base URL which looks like this:
http://someserver/mydepartment/myapp
My development environment is set up to use the default Rails configuration, which looks like this:
http://localhost:3000/myapp
I'd like to model this deployment path in my development environment. That is, I'd like to develop with a base URL which looks like this:
http://localhost:3000/mydepartment/myapp
That way, I can make all my URLs relative to "/" and they will work in both environments.
How can I change it so my application will live at this path in my development environment?
Solutions I've found, but don't work for me:
Setting the scope in routes.rb doesn't seem to work for the static content in public.
Using Apache's rewriting capabilities. I don't want to install Apache on my development box. Ideally the solution would work with WEbrick, though I seem to have Mongrel mostly working as well (there are some problems with Mongrel and Ruby 1.9.2).
Setting relative_url_root and similar suggestions which don't work with Rails 3.
Dynamically generating CSS/JavaScript and adjusting the paths to compensate between development and production environments.
You can try mapping your rails app rack config to a different base_uri.
All you need to do is wrap the existing 'run' command in a map block
try doing this in your rails 'config.ru' file:
map '/mydepartment' do
run Myapp::Application
end
Now when you 'rails server' the app should be at localhost:3000/mydepartment .
Not sure if this will give you the desired outcome, but worth a try.
Here’s how you can deploy a Rails 3.1 app to a subdirectory in Apache, replacing config.action_controller.relative_url_root which no longer exists.
In config/routes.rb:
scope 'my_subdir' do
# all resources and routes go here
end
In your Apache configuration file:
Alias /my_subdir /var/www/my_subdir/public
<Location /my_subdir>
SetEnv RAILS_RELATIVE_URL_ROOT "/my_subdir"
PassengerAppRoot /var/www/my_subdir
</Location>
And it should work, including automatically pointing all your assets to /my_subdir.

apache shows the content of my public dir - rails + apache + passenger + local git + capistrano + ubuntu

I decided just recently to implement my RoR project in a production experimental environment.
The problem I'm experiencing is when trying to view my main app page, apache would show the content of public directory instead.
Important notes:
I have deleted the index.html file from public (before, apache would show the rails welcome page)
I have a map root route in my route.rb - that used to perfectly work in my dev envronment.
map.root :controller => 'home'
I'm using the virtual host file that was automatically created by capistrano deployment under
/etc/apache2/sites-available/appname
its Content:
NameVirtualHost specific.ip.address:80
ServerName specific.ip.address
DocumentRoot /var/www/appname/current/public
Dev environment (in which everything works fine):
Ruby and Rails installed on my macbook pro using the default mongrel configuration
Experimental production environment (where I'm experiencing the problem):
Ubuntu 9.04 32 bit
Rails, Ruby, Apache, git (local), passenger and capistrano were installed and configured following the instructions in :
http://hackd.thrivesmarthq.com/how-to-setup-a-linux-server-for-ruby-on-rails-with-github-and-phusion-passenger
all steps went fine including the capistrano deployment which successfully deployed my app under:
/var/www/appname/current/
as detailed above, when trying to view my main app page - apache would show the content of my public dir (even though I don't have index.html and I have a map root route that worked in dev)
Regards,
Jason
add
Options -Indexes
to a directory configuration settings in your virtualhost. Either in
<Directory />
Options -Indexes
</Directory>
or in your document root
<Directory /var/www/appname/current/public>
Options -Indexes
</Directory>

Ruby on Rails app in root directory?

Hey guys, new to rails, but I have found out how to create an app now through the shell but to create an app using rails appname would give me a url of http://url.com/appname/ but I want my app, to be within the route if you understand me, so it's just http://url.com/login/ or /signup or /play so on?
So does anyone have any ideas how to do this, or why you can't or I shouldn't? Anything really, thanks guys!
if you use passenger and apache, firstly in your apache conf file
<VirtualHost *:80>
ServerName myapp.local
DocumentRoot "/home/davit/myapp/public"
</VirtualHost>
DocumentRoot should be point to your app' s public folder.
And in public folder create a .htaccess file and write this:
PassengerEnabled on
RailsEnv development
Rails application is stored in some directory like appname but it doesn't appear in url path. In general you should configure your server (Apache, nginx, ...) to point to public folder in rails application. The only thing to do is to configure your server properly.
It depends on your hosting provider, with dreamhost, which I use, its part of the tools they provide to configure where do I want the application to be.
If you have to configure things yourself, you can still write your alias configuration or whatever your server uses to map to your rails application public/ folder.

Resources