Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a simple ruby script in any web server (Apache or Mongrel or any thing else)

Tags:

It seems very funny to me that when I search something related ruby, all ruby on rails related results popped up. So nobody using raw ruby anymore?

However, I am new to ruby. This morning I was only trying to run a simple hello world ruby script in web server, firstly apache 2 and then tried the mongrel. But unfortunately I failed. I googled every way I can, but result only shows regarding ruby on rails. So really is there any way to run a ruby script in any web server, or I have to use ror even if I just want to do a hello world application?

like image 968
devzee Avatar asked Jun 18 '11 16:06

devzee


People also ask

How do I run a Web server in ruby?

You could try with ruby -run -e httpd . -p 8000 , which will start a WEBrick server on your current directory. Show activity on this post. Just type rails s or rails server into the terminal.

Where do I run ruby code?

rb ("runner bee") allows you to run Ruby code in the browser. To get started, click the play button in the bottom of the editor.

Which server is used for ruby?

The Ruby standard library comes with a default web server named WEBrick. As this library is installed on every machine that has Ruby, most frameworks such as Rails and Rack use WEBrick as a default development web server.


1 Answers

Sinatra is probably your best bet for getting a Ruby script running from a web server without Rails.

Take a look here: http://www.sinatrarb.com

From the Sinatra docs:

require 'sinatra'

get '/hi' do
  "Hello World!"
end

Then, just run:

$ gem install sinatra
$ ruby -rubygems hi.rb
== Sinatra has taken the stage ...
>> Listening on 0.0.0.0:4567

Just go to http://0.0.0.0:4567 in your browser and you should find your "Hello World"

...

To add on to this, since you also ask about running in Apache or other web servers, you may want to check out these tutorials about deploying your new Sinatra-based application to Apache or Nginx:

Apache: http://www.pastbedti.me/2009/11/deploying-a-sinatra-app-with-apache-and-phusion-passenger-a-k-a-mod_rack/ and http://www.giantflyingsaucer.com/blog/?p=1716

Nginx: http://tommy.chheng.com/2009/06/09/deploying-a-sinatra-app-on-nginx-passenger-with-capistrano-and-git/

Note both tutorials cover running Sinatra via Passenger (http://www.modrails.com/ -- don't be put off by the "modrails" name :) ), which I have had good luck with in deploying apps under Apache and Nginx.

like image 65
shedd Avatar answered Sep 30 '22 07:09

shedd