如何在任何 Web 服务器(Apache 或 Mongrel 或任何其他服务器)中运行简单的 ruby​​ 脚本

发布于 2024-11-16 05:38:33 字数 293 浏览 1 评论 0 原文

我觉得很有趣的是,当我搜索与 Ruby 相关的内容时,所有与 Ruby on Rails 相关的结果都会弹出。那么没人再使用原始红宝石了吗?

然而,我是红宝石新手。今天早上我只是想在网络服务器中运行一个简单的 hello world ruby​​ 脚本,首先是 apache 2,然后尝试了 mongrel。但不幸的是我失败了。我尽我所能用谷歌搜索,但结果只显示有关 ruby​​ on Rails 的信息。那么真的有没有办法在任何 Web 服务器中运行 ruby​​ 脚本,或者即使我只想做一个 hello world 应用程序,我也必须使用 ror ?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

和我恋爱吧 2024-11-23 05:38:33

Sinatra 可能是在没有 Rails 的情况下从 Web 服务器运行 Ruby 脚本的最佳选择。

看一下这里: ​​http://www.sinatrarb.com

从 Sinatra 文档:

require 'sinatra'

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

然后,只需运行

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

:在浏览器中访问 http://0.0.0.0:4567,你应该会找到你的“Hello World”

.. 。

到除此之外,由于您还询问有关在 Apache 或其他 Web 服务器中运行的问题,因此您可能需要查看这些有关将新的基于 Sinatra 的应用程序部署到 Apache 或 Nginx 的教程:

Apache: http://www.pastbedti.me/2009/11/deploying-a-sinatra-app-with-apache-and-phusion-passenger-aka-mod_rack/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/

请注意教程涵盖通过 Passenger 运行 Sinatra(http://www.modrails.com/ - 不要被“modrails”这个名字推迟了:)),我在 Apache 和 Nginx 下部署应用程序时很幸运。

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.

月牙弯弯 2024-11-23 05:38:33

例如,您可以将 Apache 配置为将 .rb 文件作为 CGI 脚本运行,然后添加 shebang 行(#!/path/to/your/ruby#!/usr /bin/env ruby​​)位于脚本顶部。但这并不是最佳选择,因为它会为每个请求启动一个新的解释器。

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.

-黛色若梦 2024-11-23 05:38:33

运行 ruby​​ 网站的更常用方式是乘客:http://www.modrails.com/
安装和使用并不难,这是 apache 的文档: http://www.modrails.com/documentation/Users%20guide%20Apache.html#_deploying_a_ruby_on_rails_application

您的应用程序必须是有效的机架应用程序,这是一个最小的 hello world(比方说 / app 是您应用程序的根文件夹):

/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

保存上面的文件并创建一个子文件夹 /app/public (乘客需要检测 ruby​​/rails/sinatra 应用程序)并在 apache 配置中使用 /app/public 作为 DocumentRoot。

这可能看起来很可怕,但这适用于生产部署,在开发中您真的不想弄乱真正的服务器。

运行我上面给出的 config.ru 文件所需的只是:

$ gem install rack
$ rackup config.ru

或者如果您想更接近您的生产系统:

$ gem install passenger
$ cd /app
$ passenger start

它将为您安装一个带有乘客的 nginx 服务器并运行您的应用程序。

在大多数情况下,你永远不会直接使用rack,而是使用ruby on Rails、sinatra或其他框架来为你生成html(它们现在都使用下面的rack来为网络服务器提供通用的api)。

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

等待圉鍢 2024-11-23 05:38:33

Ruby 1.9.2+ 简单命令。

ruby -run -e httpd . -p 5000

从这篇文章 http://til.justincampbell.me/start -an-http-server-with-ruby-run/
其他文章https://gist.github.com/willurd/5720255

Ruby 1.9.2+ simple command.

ruby -run -e httpd . -p 5000

from this article http://til.justincampbell.me/start-an-http-server-with-ruby-run/
other article https://gist.github.com/willurd/5720255

橪书 2024-11-23 05:38:33

我听说 mod_ruby 很好。与 #!/path/to/your/ruby 不同,mod_ruby 不会生成新的 ruby​​ 解释器。

https://github.com/shugo/mod_ruby

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

https://github.com/shugo/mod_ruby

影子的影子 2024-11-23 05:38:33

从您的应用程序根目录运行它。

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

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"
岁月打碎记忆 2024-11-23 05:38:33
#!/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.
#!/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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文