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

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


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.


You could configure Apache (for example) to run .rb files as CGI scripts, and then add a shebang line (#!/path/to/your/ruby or maybe #!/usr/bin/env ruby) at the top of the script. It's not optimal, though, as it'd start a new interpreter for each request.


The more commonly used way of running a ruby website is passenger: http://www.modrails.com/It is not really hard to install and you use, here is he doc for apache: http://www.modrails.com/documentation/Users%20guide%20Apache.html#_deploying_a_ruby_on_rails_application

Your application must be a valid rack application, here is a minimal hello world (let's say /app is your application's root folder):

/app/config.ru

require 'rack'require 'app'run(app)

/app/app.rb

app = proc do |env|  [    # http status code    200,    # headers    {'Content-Type' => 'text/html'},    # html body    ["<head><title>Test Page</title></head><body>Hello World !</body>"]  ]end

Save the files above and create a subfolder /app/public (required by passenger to detect a ruby/rails/sinatra application) and use /app/public as DocumentRoot in your apache config.

This may look scary but this is for production deployment, in development your really don't want to mess with a real server.

All you need to run the config.ru file I gave above is:

$ gem install rack$ rackup config.ru

Or if you want to be closer to your production system:

$ gem install passenger$ cd /app$ passenger start

which will install you an nginx server with passenger and run your application.

In most case you will never use rack directly but instead use ruby on rails, sinatra or another framework to generate the html for you (they all use rack below now to provide a common api with the webservers).