Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Ruby a good choice for CGI?

Reply
Thread Tools

Ruby a good choice for CGI?

 
 
Nick Dr
Guest
Posts: n/a
 
      11-25-2009
Ive been messing around with Ruby for a few weeks now, and I'm fairly
comfortable with the language itself, but now I want to host a *very*
tiny app on the net, and I'm getting very overwhelmed with the massive
array of frameworks and technologies available.

I'll describe what I'm trying to, and maybe someone here can just point
me in the right direction and spare me weeks of wading through doc to
find out all about each of these things.

I've written a small HTML/JQuery page that displays a simple UI on a
canvas element. The user clicks on pictures of buttons and the JQuery
posts a string to the server. I need some server side code - hopefully
ruby b/c i like the syntax, and i've heard bad things about PHP - that
will take the command from the POST, hit a MySQL db, and return a string
response. The JQuery takes the string response and updates the canvas.

Really simple right? I already have the HTML/JQuery/Canvas-draw. I've
already got the Ruby code itself, including the MySQL part.

I went to host it, and figured I'd try it at home before I spent any
money. I installed Apache on my Ubuntu box, dropped the Ruby files into
the cgi-bin folder, used the ruby cgi lib in my script and it all
worked. Then the plane crashed in the mountain.

I started reading online and people said pure CGI was way too slow b/c
I'll be loading and disposing of all my code and db connections each
request. That sounds bad. So what do I do? I've be told things about
FastCGI, Passenger, mod_ruby, Rack, Sinatra, Camping, etc. and I can't
even figure out which one I need!

Can someone point me in the right direction here? My app is VERY small.
Practically no code, no HTML output. All I need is to execute the 2 or 3
Ruby functions I already have, on the net, with some basic
cookie/session support. What's the least headache(and not terribly slow)
way to do this?
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Walton Hoops
Guest
Posts: n/a
 
      11-25-2009
> -----Original Message-----
> From: http://www.velocityreviews.com/forums/(E-Mail Removed) [(E-Mail Removed)]
>
> Ive been messing around with Ruby for a few weeks now, and I'm fairly
> comfortable with the language itself, but now I want to host a *very*
> tiny app on the net, and I'm getting very overwhelmed with the massive
> array of frameworks and technologies available.
>
> I'll describe what I'm trying to, and maybe someone here can just point
> me in the right direction and spare me weeks of wading through doc to
> find out all about each of these things.
>
> I've written a small HTML/JQuery page that displays a simple UI on a
> canvas element. The user clicks on pictures of buttons and the JQuery
> posts a string to the server. I need some server side code - hopefully
> ruby b/c i like the syntax, and i've heard bad things about PHP - that
> will take the command from the POST, hit a MySQL db, and return a
> string
> response. The JQuery takes the string response and updates the canvas.
>
> Really simple right? I already have the HTML/JQuery/Canvas-draw. I've
> already got the Ruby code itself, including the MySQL part.
>
> I went to host it, and figured I'd try it at home before I spent any
> money. I installed Apache on my Ubuntu box, dropped the Ruby files into
> the cgi-bin folder, used the ruby cgi lib in my script and it all
> worked. Then the plane crashed in the mountain.
>
> I started reading online and people said pure CGI was way too slow b/c
> I'll be loading and disposing of all my code and db connections each
> request. That sounds bad. So what do I do? I've be told things about
> FastCGI, Passenger, mod_ruby, Rack, Sinatra, Camping, etc. and I can't
> even figure out which one I need!
>
> Can someone point me in the right direction here? My app is VERY small.
> Practically no code, no HTML output. All I need is to execute the 2 or
> 3
> Ruby functions I already have, on the net, with some basic
> cookie/session support. What's the least headache(and not terribly
> slow)
> way to do this?



DISCLAIMER: I don't actually use Ruby for webapps

CGI is ok, if and only if your web traffic is very low. If you
Expect a lot of traffic, or your traffic will be coming in bursts, you
definitely want to figure something else out. For something as simple
as you seem to be describing, I would recommend either mod_ruby, or
fastcgi. Mod_ruby is akin to mod_php5 or mod_python where the Ruby
interpreter is actually imbedded in the Apache server. FastCGI on
the other hand is similar to CGI, except that the process is reused
for requests rather than starting a new one for each request.

I'm not sure which will be easier, but either should be able
to handle what you need, and I think both should be fairly simple setups.

Both are available in Ubuntu via:
sudo apt-get install libapache2-mod-fastcgi
sudo apt-get install libapache2-mod-ruby

I hope this helps!


 
Reply With Quote
 
 
 
 
Jesús Gabriel y Galán
Guest
Posts: n/a
 
      11-25-2009
On Wed, Nov 25, 2009 at 8:12 PM, Nick Dr <(E-Mail Removed)> wrote:
> I started reading online and people said pure CGI was way too slow b/c
> I'll be loading and disposing of all my code and db connections each
> request. That sounds bad. So what do I do? I've be told things about
> FastCGI, Passenger, mod_ruby, Rack, Sinatra, Camping, etc. and I can't
> even figure out which one I need!
>
> Can someone point me in the right direction here? My app is VERY small.
> Practically no code, no HTML output. All I need is to execute the 2 or 3
> Ruby functions I already have, on the net, with some basic
> cookie/session support. What's the least headache(and not terribly slow)
> way to do this?


I've been doing little apps with Sinatra and I'm very happy with it.
Pretty simple if you just need to implement 2 or 3 get requests:

require 'sinatra/base'
class MyClass < Sinatra::Base
get '/' do
"hello world"
end
end
MyClass.run!

Any rack-based framework (as Sinatra) can be deployed in a number of
ways including Phussion Passenger (apache) or most ruby webservers,
like Thin.

Hope this helps,

Jesus.

 
Reply With Quote
 
David Masover
Guest
Posts: n/a
 
      11-25-2009
On Wednesday 25 November 2009 01:12:19 pm Nick Dr wrote:
> I've written a small HTML/JQuery page that displays a simple UI on a
> canvas element. The user clicks on pictures of buttons and the JQuery
> posts a string to the server. I need some server side code - hopefully
> ruby b/c i like the syntax, and i've heard bad things about PHP - that
> will take the command from the POST, hit a MySQL db, and return a string
> response. The JQuery takes the string response and updates the canvas.


That sounds tailor-made for Sinatra. However, you should learn something about
REST, first -- are you sure POST is appropriate?

> I've
> already got the Ruby code itself, including the MySQL part.


Why MySQL specifically? Why not go with something database-agnostic, like
Datamapper, Sequel, or ActiveRecord?

But hey, if you've already got it, it works.

> I've be told things about
> FastCGI, Passenger, mod_ruby, Rack, Sinatra, Camping, etc. and I can't
> even figure out which one I need!


FastCGI is obsolete, so scratch that.

Passenger and mod_ruby are essentially the same thing, IIRC. But my
understanding is that Passenger works with Rack, which doesn't feel at all
like CGI. So these might be deployment options, but they have nothing to do
with how you write and develop your app.

So among your options are Sinatra and Camping. Camping is by _why, who seems
to have disappeared, but I'm sure someone's maintaining it... What I like
about Sinatra is the sheer simplicity of:

get '/' do
'Hello, world!'
end

In your case, assuming you still want a POST, it'll probably be something
like:

post '/wherever' do
# build your string from the database and return it
end

Once you've got it working on localhost, if you can find a managed host
that'll just let you upload a Rack app (especially if they know about
Sinatra), do that. Or you could do it yourself -- right now, I'm liking nginx
and Unicorn, which is what Github uses:

http://github.com/blog/517-unicorn

On the other hand, you could technically deploy this with Passenger. Some
people find it easier, but I like deploying with Capistrano anyway.

> some basic
> cookie/session support.


http://www.sinatrarb.com/faq.html#sessions

> What's the least headache(and not terribly slow)
> way to do this?


My vote is Sinatra.

 
Reply With Quote
 
Marnen Laibow-Koser
Guest
Posts: n/a
 
      11-25-2009
David Masover wrote:
[...]

>> I've be told things about
>> FastCGI, Passenger, mod_ruby, Rack, Sinatra, Camping, etc. and I can't
>> even figure out which one I need!

>
> FastCGI is obsolete, so scratch that.
>
> Passenger and mod_ruby are essentially the same thing, IIRC.


You do not RC. Passenger is the same as mod_rails. mod_ruby is an
older module, suitable for CGI scripts but not Rails (and therefore I
would assume it's not suitable for Sinatra).

> But my
> understanding is that Passenger works with Rack, which doesn't feel at
> all
> like CGI. So these might be deployment options, but they have nothing to
> do
> with how you write and develop your app.
>
> So among your options are Sinatra and Camping. Camping is by _why, who
> seems
> to have disappeared, but I'm sure someone's maintaining it...


There's also Merb, which is used by SproutCore sort of in the way the OP
is describing.

> What I
> like
> about Sinatra is the sheer simplicity of:
>
> get '/' do
> 'Hello, world!'
> end
>


That looks nice! I plan to try Sinatra if I run into a project for
which Rails is excessive.


> In your case, assuming you still want a POST, it'll probably be
> something
> like:
>
> post '/wherever' do
> # build your string from the database and return it
> end
>
> Once you've got it working on localhost, if you can find a managed host
> that'll just let you upload a Rack app (especially if they know about
> Sinatra), do that. Or you could do it yourself -- right now, I'm liking
> nginx
> and Unicorn, which is what Github uses:
>
> http://github.com/blog/517-unicorn
>
> On the other hand, you could technically deploy this with Passenger.
> Some
> people find it easier, but I like deploying with Capistrano anyway.
>


They're not mutually exclusive. Cap + Passenger is a great combination.

>> some basic
>> cookie/session support.

>
> http://www.sinatrarb.com/faq.html#sessions
>
>> What's the least headache(and not terribly slow)
>> way to do this?

>
> My vote is Sinatra.


Best,
--
Marnen Laibow-Koser
http://www.marnen.org
(E-Mail Removed)
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Bill Kelly
Guest
Posts: n/a
 
      11-25-2009

From: "Nick Dr" <(E-Mail Removed)>
>
> Can someone point me in the right direction here? My app is VERY small.
> Practically no code, no HTML output. All I need is to execute the 2 or 3
> Ruby functions I already have, on the net, with some basic
> cookie/session support. What's the least headache(and not terribly slow)
> way to do this?


Others have already mentioned Sinatra, which looks nice. Another
framework which also allows starting very simple is Ramaze:
[ http://ramaze.net/ ]


require 'ramaze'

class MainController < Ramaze::Controller
def index
"Hello, World! Server clock reads: #{Time.now}"
end
end

Ramaze.start ort => 9001, :adapter => :webrick


At this point the HTTP server is running on a custom port. However,
it's not difficult to integrate that with the webserver, as described
here: http://wiki.ramaze.net/Deployment


Regards,

Bill



 
Reply With Quote
 
David Masover
Guest
Posts: n/a
 
      11-25-2009
On Wednesday 25 November 2009 03:00:49 pm Judson Lester wrote:
> On Wed, Nov 25, 2009 at 12:52 PM, David Masover <(E-Mail Removed)> wrote:


> > Granted, but when I talk to people using Passenger, the #1 reason they
> > tell me
> > is ease of deployment -- the idea that you'd just FTP up a file into a
> > certain
> > directory to restart your app.
> >
> > To my mind, Passenger's appeal is that it embeds Rack in Apache - so you

>
> get the benefits of mod_ruby (not restarting the interpreter with each
> request)


I'm confused. Wasn't mod_ruby the one which restarts the interpreter with each
request? I also don't know of _any_ Rack handler that restarts the interpreter
with each request.

The supposed technical advantages of Passenger are that you're building on
Apache, that it's actually somewhat production-ready but doesn't require
complex reverse proxies, and it has something to do with Ruby Enterprise
Edition.

I'm curious, actually... I get why people might choose thin (really quick
responses, so no real multitasking required -- keep it simple), mongrel (old-
ish and proven), unicorn (simple, reliable, prefork is proven), ebb (C for
speed, and threads may be more efficient than forking), etc etc.

I don't really see the case for Passenger over these, other than simplicity of
deployment. Am I missing anything?

 
Reply With Quote
 
Marnen Laibow-Koser
Guest
Posts: n/a
 
      11-25-2009
David Masover wrote:
[...]
> The supposed technical advantages of Passenger are that you're building
> on
> Apache,


Sort of, although I think you can use it with Nginx and Thin.

> that it's actually somewhat production-ready


Strike the "somewhat".

> but doesn't require
> complex reverse proxies,


Right. It also doesn't require clustered Mongrels. It's about as easy
to set up as mod_php.

> and it has something to do with Ruby Enterprise
> Edition.


It's written by the same people, and I believe it can take advantage of
some Ruby EE optimizations. But you can use it just as well with MRI
(although I'm not sure why you would -- EE is much faster).

>
> I'm curious, actually... I get why people might choose thin (really
> quick
> responses, so no real multitasking required -- keep it simple), mongrel
> (old-
> ish and proven), unicorn (simple, reliable, prefork is proven), ebb (C
> for
> speed, and threads may be more efficient than forking), etc etc.
>
> I don't really see the case for Passenger


Incredible ease of configuration, EE optimizations. AFAIK, Passenger
has no serious competition on either of these points.

> over these, other than
> simplicity of
> deployment.


Not app deployment so much as configuration. It really is about as easy
to set up as mod_php. I don't know of any other Rails/Rack server setup
for which that can be said.


> Am I missing anything?


You're missing just about everything. Personally, I'm not sure what
the case *against* Passenger is, unless you're using Windows or JRuby.

Didn't 37signals switch to Passenger some time ago?

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
(E-Mail Removed)
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Brian Candler
Guest
Posts: n/a
 
      11-25-2009
David Masover wrote:
> I'm confused. Wasn't mod_ruby the one which restarts the interpreter
> with each
> request?


That would be CGI. Each incoming request forks off a new CGI handler,
which processes one request then terminates.

With the old mod_ruby, each Apache HTTP worker has an interpreter. If
Apache decides to fire up 20 workers because it's handling 20 concurrent
connections, that's 20 ruby interpreters. That may be wasteful if there
are mixed ruby and regular HTTP requests.

Phusion Passenger keeps a pool of Ruby processes, independent of the
httpd workers/threads, and picks a free worker to dispatch each ruby
request into. This makes it similar in some ways to fastcgi, but it's
much easier to set up.

> I also don't know of _any_ Rack handler that restarts the
> interpreter
> with each request.


You can run rack as a CGI, I belive. You would not be advised to.

> something to do with Ruby Enterprise
> Edition.


That's the fork-friendly garbage collection. Passenger also prepares a
preloaded ruby interpreter with your app and forks it when it needs to
increase the size of the pool, rather than starting with a fresh ruby
instance which would have to read all the libraries in. Advantages: much
faster startup, and much more RAM sharing between ruby processes, so
less total RAM used.

> I'm curious, actually... I get why people might choose thin (really
> quick
> responses, so no real multitasking required -- keep it simple), mongrel
> (old-
> ish and proven), unicorn (simple, reliable, prefork is proven), ebb (C
> for
> speed, and threads may be more efficient than forking), etc etc.
>
> I don't really see the case for Passenger over these, other than
> simplicity of
> deployment. Am I missing anything?


Well for one thing, many sites are a mixture of static assets and
dynamic content. It's much more efficient to serve the static assets
from a webserver tuned for serving this sort of content, rather than
serving your entire site from a ruby server.

For another thing, Rails is often still best run non-threaded. This
means you need multiple worker processes. With mongrel/thin/etc this
means running multiple servers bound to different ports, and that in
turn means sticking a proxy of some sort in front, and having the bits
and pieces necessary to start and monitor the required number of
workers, and sorting out things like proxying the SSL variables.

I bought Ezra's Deploying Rails Apps book and built a system by
combining Apache, pen, mongrels, monit and capistrano. It works, but is
complex and a pain to add new apps. With Apache+Passenger it just works
out of the box, is easy to tweak, is far simpler, and you get the REE
memory sharing advantages too.

I don't really see the case for deploying Rack or Rails apps using any
other approach, other than familiarity. Am I missing anything?
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Marnen Laibow-Koser
Guest
Posts: n/a
 
      11-25-2009
Judson Lester wrote:
> On Wed, Nov 25, 2009 at 2:17 PM, Brian Candler <(E-Mail Removed)>
> wrote:
>
>> Well for one thing, many sites are a mixture of static assets and
>> dynamic content. It's much more efficient to serve the static assets
>> from a webserver tuned for serving this sort of content, rather than
>> serving your entire site from a ruby server.
>>

>
> I wanted to amplify with use cases that run more than one app on the
> same
> server. My experience is that getting two *Rails* apps to play nicely
> in
> the same environment is a pain. But certainly easier than getting
> Mongrel
> to run PHP. With Passenger I can run Rails alongside PHP, and serve
> static
> content far more efficiently.


And running multiple Rails apps with one Passenger installation is
likwise a breeze.


> (nginx is on my saw-sharpening list...)
>
> Judson


Best,
--
Marnen Laibow-Koser
http://www.marnen.org
(E-Mail Removed)
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Opinions wanted! Is Ruby a good choice for... Kioko -- Ruby 2 10-06-2009 09:53 AM
c++ as choice for long term application choice. miles.jg C++ 16 11-14-2007 03:43 PM
A good Ruby book and a good Ruby blog vasudevram Ruby 0 04-15-2007 06:27 PM
Can Choice components respond to keyboard input like HTML Choice components? Mickey Segal Java 0 02-02-2004 10:59 PM
Choice of DHCP-server? Is the "IOS-one" a good choice? Fred Cisco 1 12-11-2003 06:25 AM



Advertisments