Mount rails webrick to serve .php files as well - ruby-on-rails

I would like to serve php files on my webrick server, as well as load the default rails application. My best option seems to be using php-cgi for that.
I found an implementation for webrick php handler. It works by itself, but I didn't managed to make it work with Rails environment. Apart from the configuration, it seems that I should call at least this line when starting the Webrick:
# mount document root again to set new options (add PHPHandler for .php files)
server.mount("/", HTTPServlet::FileHandler, dir,
{:FancyIndexing => true, :HandlerTable => {"php" => HTTPServlet::PHPHandler}})
Is there a way I can add this as a hook or monkey patch to my Rails application? Am I doing it right, or have a super-simple way to achieve that?

Actually, the best way to run PHP and other CGI scripts on Rails seems to be the rack-legacy gem. The configuration is simple, and you can set paths to be served as PHP and other CGIs.

Related

Rails application issue after deployment - 404 error

I'm new to RoR.
I was able to install Rails and host it in Webrick (Sample App with "Welcome" controller) in my windows.
Now i have a Unix Weblogic Server along with a dedicated domian.
After exporting the .WAR file using Warbler, i accessed the Oracle Admin Console from where i deployed the .WAR file in the dedicated domain. I did all this for the Sample app with only the Welcome controller in it.
But even after deploying the WAR file, on accessing the Domain along with the Port Number (:9002) i ended up with 404 file not found error On looking at the server logs,there wasn't any records relating to any error. The Application must have been deployed properly. I assume that i must have missed out on some basic configurations in the routes.rb or similar files before deploying. Can anyone Guess what are all the possibilities and if possible can anyone help me by pointing to any tuts that cover the Steps to be carried out for configuration before deployment. do i need to install both JRuby and Rails inside the server before depolyment?
I can't really guess with Eror 404 only.
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.
One more thing you also add this to your config/environments/production.rb and config/environments/development.rb (if on production mode):
config.action_controller.asset_path = proc { |path| "/abc#{path}" }
otherwise when you call your helpers such as stylesheet_link_tag in your views, they will generate links without the "/abc".
Also, find some guides you may refer for good support.
JRubyOnRailsOnBEAWeblogic.
Use JRuby with JMX for Oracle WebLogic Server 11g
Let me know if it is not resolved.

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.

Rails sends 0 byte files using send_file

I can't get send_file(Model.attachment.path) to work.
It doesn't fail, instead, it sends a 0 byte size file to the client, the file names are correct though.
This problem started happening after I did a big migration from Rails 2.3.8 to 3.
There were a lot of other things that took place in this migration and I will try my best to detail all of them.
Distrubution change/Server Change. Rackspace RHEL5 to Linode Ubuntu 10.04LTS
Ruby version change, 1.8.6 -> 1.9.2
Rails version change, 2.3.8 -> 3.0.0
httpd platform change, apache2 -> nginx (However I tried on apache2 as well and it did not work).
I moved the attachments via ftp as they were not part of my git repositories so they were published via cap deploy, instead manual ftp remote(RHEL5) to local(Win7) then local(Win7) to remote(Ubuntu10).
I do know that FTPing does not retain the file permissions through the transfers, so what I've also done is mimicked the chmods that were seen on my previous servers so they are almost identical. (users/groups are different, set to root:root instead of olduser:olduser).
A snippet of the request to download a attachment from my production log.
Started GET "/attachments/replies/1410?1277105698" for 218.102.140.205 at 2010-09-16 09:44:31 +0000
Processing by AttachmentsController#replies as HTML
Parameters: {"1277105698"=>nil, "id"=>"1410"}
Sent file /srv/app/releases/20100916094249/attachments/replies/UE0003-Requisition_For_Compensation_Leave.doc (0.2ms)
Completed 200 OK in 78ms
Everything's okay. Let me also rule out local issues, I've tried downloading via Chrome on both Win7 and Ubuntu (on Vbox).
Let me also assure you that the path is indeed correct.
root#li162-41:/srv/app/current# tail /srv/app/releases/20100916094249/attachments/replies/UE0003-Requisition_For_Compensation_Leave.doc
#
#
%17nw
HQ��+1ae����
%33333333333(��QR���HX�"%%��#9
��#�p4��#P#��Unknown������������G��z �Times New Roman5��Symbol3&�
�z �Arial5&�
So to sum up the question, how do I get send_file to actually send files instead of fake 0 byte junk.
send_file has :x_sendfile param which defaults to true in Rails 3.
This feature offloads streaming download to front server - Apache (with mod_xsendfile) or lighttpd, by returning empty response with X-Sendfile header with path.
Nginx uses X-Accel-Redirect header for same functionality but you have to
configure Rails properly in proper environment file:
config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect'
Rails 3 update: this line already exists in production.rb, just uncomment it.
Add sendfile on; to your nginx config to utilize header sent by Rails.
Remember the absolute path must be used and nginx must have read access to file.
Another way for aliased files:
For better security I use aliases in nginx instead of absolute paths,
however send_file method checks existence of file which fails with alias.
Thus I changed my action to:
head(
'X-Accel-Redirect'=> file_item.location,
'Content-Type' => file_item.content_type,
'Content-Disposition' => "attachment; filename=\"#{file_item.name}\"");
render :nothing => true;
In Rails 3, just uncomment the line config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' in production.rb inside environments folder.
Yes, I had the same problem with X-sendfile being enabled by default in Rails 3 too.
If you have large volume of "send_file" calls,
you can just comment-out following line in config/environments/production.rb:
#config.action_dispatch.x_sendfile_header = "X-Sendfile"
Then send_file method started working perfectly.
Because I can't install x-sendfile extension to Apache, I just searched a little and found this.
I hope it helps.
I've had similar issues with send_file() in the past, using send_data() instead saved me back then (e.g. send_data File.read(filename), :disposition => 'inline', :type => "some/mimetype")
On Rails 4, I realize my problem is that I deleted the temporary file which I've generated to send to user.
If I didn't delete the file, send_file works. I've not tested on thin but it works great on Passenger 5 as stand-alone server.

Rails on remote Apache server not displaying index.html.erb

I played around with Rails on my laptop (running Linux + Apache + MySQL) and had no trouble getting the Getting Started with Rails tutorial to work locally. Now I'm trying the same thing at work on a remote Mac OS X + Apache server, and things aren't quite so rosy.
I typed rails blog -d mysql to create a directory called blog in /Library/WebServer/Documents/mydirectory. The trouble is, if I go to server.com/mydirectory/public, I get the public/index.html in my browser. But, I don't get this file if I go to server.com/mydirectory/. Instead, I get a 403 error. Also, when I:
script/generate controller home index
to create:
app/views/home/index.html.erb
I am unable to view this file, whether I go to server.com/mydirectory/home/index, or if I add a new line (map.root :controller => "home") to config/routes.rb and go to server.com/mydirectory.
Am I missing something really obvious about Apache and Rails?
Apache does not support Rails out of the box. You have to get mod_rails aka Passenger installed. Or, you could just use the server that comes with Rails, which is much easier (but not suitable for production). To do this, go to your directory and do ./script/server.

How can I run my Rails application in a non-root context?

I'm working in a non-privileged environment, and my Rails application's root url is http://foo.com/bar.
What is the simplest way to tell Rails that, for example, my stylesheets are in /bar/stylesheets, not /stylesheets, and make model_url point to /bar/model/baz instead of /model/baz?
Regardless of the way you are running the application to that specific path (be it script/server --path=/sub-uri or have deployed passenger to a directory) you don't need to change the code as the url helpers will automatically adjust their path to the environment.

Resources