How does Pow/Phusion Passenger/Webrick work? - ruby

I come from a PHP and .NET world where I understand the environment fairly well. However I can't find a newbie explanation of how the Ruby / Ruby on Rails stack actually work with these web server.
Are they more close to the PHP model, where all classes of an application are loader for each request and there is no default shared memory, or is it like an application server where an active app sits in the memory and handles requests?
How is it with reloading when a file changes? Does the app in an application server has to be restarted? How does it know? Does it monitor file system?
I have seen that both the Ruby Version Manager (rvm) and the newer rbenv from 37signals shomewhat shuffles with the ruby command on OS X / Linux. This seems like a total magic to me. Does a webserver just runs ruby command and does not care where the interpreter is resolved in the $PATH?

Webrick is the default server of Rails and is usually used for developing and testing.
Rails is session based like PHP is. If you want to run in production you will generally use Phusion Passenger on Apache or Nginx, dont worry about that for now.
If you run in Development or Test environment you can edit your application files(views, controllers and models) and they will be reloaded on each request (even if they are not edited).
Have a look at generating a project and scafolds with Rails to get you started.
http://guides.rubyonrails.org/getting_started.html

Related

How to deploy a Ruby app - not ROR?

I am very new to ruby and I wonder, Is it possible to have my ruby script deployed on a server?
Or I should have to use Rails?
As I can understood that Rails is not part of the core Ruby lang, and Ruby have server functionality even without Rails. (as in Java, PHP, etc..)
EDIT:
I have a Ruby script - acts as a cmd-line passed program - and I want to deploy it to an external (or even internal) server the way CGI scripts/programs used to do.
Yes, you can deploy any Ruby application, not just Rails apps obviously. Take a look at Capistrano.
Deployment and serving are two different things however. If you're looking for Ruby HTTP servers look at Unicorn, Thin, WEBrick, Puma.
If you want a fully-fledged solution try Heroku which handles both the deployment and web serving parts.
There are many tools to deploy Ruby projects, but you can do it pretty much manually.
I also found it very hard to find an easy-to-go solution and I think this is a very annoying gap in RoR framework.
I've been working in a solution to deploy a project to a server using Git, like the Heroku toolbelt (google it, is a really nice tool). The main concept is: you use Git to push your project and the server does everything else! Here you can see my project: https://github.com/sentient06/RDH/.
But please, don't focus on that. Instead, read the way I came to all information in the wiki: https://github.com/sentient06/RDH/wiki.
It is a bit outdated, but I can summarize here to you:
First, setup your server. This is the most boring part, you must setup all configuration, security measures, remote access, etc, etc.
If you don't have a server, you can hire one specially for RoR applications. There are a few good out there and each has a different deployment workflow. But supposing you decide o setup yourself:
I suggest you have any Linux or Unix system, server version. Then install Ruby Version Manager, then Ruby and then Rails. Then install a server application. I suggest Thin, but lots of people use Unicorn or Apache or other servers. Dig a little bit on the internet, find an easy to use solution. If you do not use Apache, though, you will need a "reverse proxy" too, so you can redirect all requests on ports 80, 8080, etc, to your applications. I suggest Nginx (I don't like Apache, I think is too overkill).
Now, everything done, the deploy process can be done more or less like this:
1 - Commit everything in a way your files are updated in the server;
2 - In the server, cd to the directory of your application and execute these commands:
$ bundle package
$ bundle install --deployment
$ RAILS_ENV=production rake db:migrate
$ rake assets:precompile
3 - Restart the server and, if necessary, the reverse proxy.
Dig on the internet to understand each command. These will pretty much force your application into production mode, reduce the space used by your javascript and CSS, migrate your production database and install the bundles. Production RoR is not so different from development RoR, it is just more compact and faster.
I do hope these informations are useful.
Good luck!
Update:
I forgot to mention, check ruby-toolbox, it has some really useful statistics and information on how often Rails technologies are being updated. They have many categories, this one is on deployment automation, give it a look: https://www.ruby-toolbox.com/categories/deployment_automation.
Cheers!

How do I run a Sinatra application on G-WAN?

I'm trying to write a Ruby web application and I want to use the functionality offered by Sinatra.
I used this code:
require 'rubygems'
require 'sinatra'
get '/' do
'Hello, world!'
end
I typed localhost:8080/?hello.rb in my browser and I received an error message that the script is unable to find sinatra.
I also tried to run the sinatra app using this command:
ruby hello.rb
and this time it worked!
So I suspect the problem is not in my code, but in the way I'm using G-WAN to serve the Ruby script.
Sinatra expects to be run by a rack-compatible server, which G-Wan is apparently not. When you run script manually it is being run by a WEBrick server that comes bundled with Ruby. Examples of compatible servers are thin, unicorn or apache/nginx with phusion passenger
Ruby (like Java or PHP) probably uses configuration files and/or environment variables to specify where to find packages, modules and libraries.
While G-WAN received similar requests for asm, C, C++, D, Objective-C, Java and C# support, Ruby users did not manifest interest in this area - so far.
If you point to us the relevant Ruby documentation, we will try to add this Ruby feature (just like it was done for the other languages).

How to run core ruby web application in web server?

I had installed ruby/gem and the server is apache2 running fine.But I try to run a simple ruby project which is built by using Netbeans IDE. How can I run this web project in web browser?
While you sometimes still read that, you should not run ruby applications under CGI. The specification is getting old and support for it (especially in the rails world) is ceasing.
Most web frameworks in ruby use Rack which is a generic interface between a webserver and a ruby application. Thus if you write a rack-compliant application (which you do if you use e.g. Rails, Sinatra or Padrino) you can start it in any Webserver offering a Rack-interface.
The probbly most popular one is Phusion Passenger (modrails) which is a module for either Apache or nginx and integrates into the webserver. It should be noted though that it only runs on Linux/Unix systems, not Windows.
Popular alternatives are Thin and Unicorn. These servers are typically run behind a front-end webserver like Apache or nginx. The frontend-webserver delivers static files (CSS, images, ...) and forwards dynamic requests to the backend-webserver (thin or unicorn). You can find many guides on google if you search for "Apache thin ubuntu" (or similar).
And as a side-note, Mongrel shouldn't be used anymore for new deployments as it isn't really maintained anymore and causes many issues with newer framework versions.

Why is my sinatra website so slow?

After asking this question, I started using Sinatra as a way to serve web pages.
This evening, a friend of mine and I started to test the speed of the server.
The file to log in looks like:
require 'rubygems'
require 'sinatra'
require 'haml'
enable :sessions #for cookies!
get '/' do
haml :index
end
And the index.haml looks like:
%title
First Page
%header
%h2 First Page
He's sitting on a recent laptop, as am I, with an Apple 802.11n router between the two of us. We're both running Windows 7. I've also tried these same files on a laptop running Ubuntu 9.10 x64 with Sinatra and all relevant files installed from apt-get.
Sinatra is taking 7 seconds to serve up a single page request, no matter the server OS, Windows or Linux. I see that here the author managed to get over 400 requests/second processed. What gives? (or should this be on SuperUser or the like?)
I'll set aside any opinions on when you should optimize your web application.
Set up different configurations in your Sinatra app for development and production because some of these suggestions, you won't always want to use. In fact, you should probably go ahead and setup and environment similar to how you would deploy in production. You would not deploy by simply running ruby app.rb. You'd want to put apache or nginx in front of your Mongrel. Mongrel will serve up your static files, but that's really only advisable for development mode. In deployment, a web server is going to do a lot better job for that. In short, your deployed environment will be faster than your standalone development environment.
At this point, I wouldn't worry about Mongrel vs. Thin. If Thin is twice as fast - it isn't - then your 7 seconds becomes 3.5. Will that be good enough?
Some things to try ...
I know I just told you to set up a deployment environment, but maybe it's not the server side. Have you tried running YSlow or PageSpeed on your pages? I/O is going to take up more of those 7 seconds (Disclaimer: I'm assuming that there's nothing wrong with your network set up) than the server. YSlow - Firebug actually - will tell you how long each part of your page takes to get to the browser.
One of the things that YSlow told me to do was to put a far forward Expires header on my static assets, which I knew but I was leaving optimization until the end. That's when I realized that there were at least 3 different places that I could specify that header. I'm convincing myself that doing it in nginx is the right place to put it.
If you're happy with those results, then you can look at the server. Off the top of my head, so not exhaustive
Turn on gzip responses.
Combine your stylesheets so there's only one per page request. There may be some Rack Middleware for this, if you don't do it manually.
Cache. I'm trying Rack::Cache.
Use sprites to decrease the number of image downloads you use.
Minify your Javascript. Again, maybe via Rack Middleware.
Rack Middleware is neat, but it uses CPU. So, manually minifying your Javascript adds a new step to your workflow, but on the server, it's faster than Middleware. It's a tradeoff.
Sorry if this was rambly.
I had this problem when running Sinatra with shotgun but not when running my app directly (i.e., ruby -rubygems app.rb). This is because shotgun forks and reloads the application for each request.
I found a thread in Sinatra's mailing list which discussed this issue and people there advised using rerun instead of shotgun. I'm happy to say it solved this issue for me.
Try using Thin as the server. I noticed an increase in performance compared with WEBrick and Mongrel.
gem install thin
When you run your app using ruby TestServer.rb you'll see the following:
Sinatra/0.10.1 has taken the stage on 4567 for development with backup from Thin
I'm running Sinatra inside VMWare Fusion with Vagrant. My app was running slowly (about ten seconds to service a request). Then I found this gem:
Webrick is very slow to respond. How to speed it up?
It seems that WEBrick was (by default) configured to reverse dns lookup on every request, and that was slowing it down.

JRuby / Rack deployment

I know this is pretty exotic, but I want to deploy a Ruby web application (not Rails, but Rack based, so it plugs into most Ruby servers just fine) using JRuby. Google and friends give me a few success stories, but mostly rails related and if not, no details on the deployment are provided. The framework I'm using is Ramaze, if it matters.
Any help would be greatly appreciated.
In my opinion, running a Rack based application with a rackup script is the real Ruby way. And I wanted to apply the same for JRuby too. That is why I've written jetty-rackup http://github.com/geekq/jetty-rackup
We are using it for deploying a Sinatra web application. No Java specific configuration needed. A typical, small config.ru is enough. Embedded jetty web server is used in place of Webrick then.
This is the "just works" gem for me: https://github.com/matadon/mizuno

Resources