Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 303 Vote(s) - 3.57 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to run a simple ruby script in any web server (Apache or Mongrel or any thing else)

#1
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?
Reply

#2
#!/usr/bin/env ruby //shebang line to indicate path to ruby.
require 'cgi' //cgi file to create a simple cgi object.
cgi = CGI.new //instantiating a cgi object.
puts cgi.header //thats telling the server about the type(html).
puts "hello" // thats the output on the browser.
Reply

#3
Ruby 1.9.2+ simple command.

ruby -run -e httpd . -p 5000

from this article

[To see links please register here]

other article
Reply

#4
Run this from your app root.

ruby -r webrick -e "s = WEBrick::HTTPServer.new(:Port => 8000, :DocumentRoot => Dir.pwd); trap('INT') { s.shutdown }; s.start"

Reply

#5
I've heard mod_ruby is good. Unlike, `#!/path/to/your/ruby`, mod_ruby won't spawn a new ruby interpreter.

[To see links please register here]

Reply

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

Take a look here:

[To see links please register here]


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

[To see links please register here]

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:

[To see links please register here]

and

[To see links please register here]


Nginx:

[To see links please register here]


Note both tutorials cover running Sinatra via Passenger (

[To see links please register here]

-- don't be put off by the "modrails" name :) ), which I have had good luck with in deploying apps under Apache and Nginx.
Reply

#7
The more commonly used way of running a ruby website is passenger:

[To see links please register here]

It is not really hard to install and you use, here is he doc for apache:

[To see links please register here]


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).
Reply

#8
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.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through