问题描述
||
对我来说似乎很有趣,当我搜索与红宝石有关的东西时,所有与红宝石相关的结果都会弹出。因此,没有人再使用原始红宝石了吗?
但是,我是红宝石的新手。今天早上,我只想在Web服务器上运行一个简单的hello world ruby脚本,首先是Apache 2,然后尝试了杂种。但不幸的是我失败了。我已尽一切可能在Google上进行了搜索,但结果仅显示了关于红宝石的信息。那么,是否真的有任何方法可以在任何Web服务器上运行ruby脚本,或者即使我只想做一个hello world应用程序,也必须使用ror?
解决方法
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下。
, 您可以将Apache配置为例如将.rb文件作为CGI脚本运行,然后在脚本顶部添加一个shebang行(#!/path/to/your/ruby
或maybe3ѭ)。不过,这并不是最佳选择,因为它会为每个请求启动一个新的解释器。
, 运营红宝石网站最常用的方式是乘客:http://www.modrails.com/
安装起来并不难,您可以使用,这是他的Apache文档:http://www.modrails.com/documentation/Users%20guide%20Apache.html#_deploying_a_ruby_on_rails_application
您的应用程序必须是有效的机架应用程序,这是最小的问候世界(假设/ 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服务器并运行您的应用程序。
在大多数情况下,您永远不会直接使用机架,而是在轨道,sinatra或其他框架上使用ruby为您生成html(它们现在都在下面的机架中提供与Web服务器一起使用的通用api)。
, 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
, 我听说mod_ruby很好。与#!/path/to/your/ruby
不同,mod_ruby不会产生新的ruby解释器。
https://github.com/shugo/mod_ruby
, 从您的应用程序根目录运行。
ruby -r webrick -e \"s = WEBrick::HTTPServer.new(:Port => 8000,:DocumentRoot => Dir.pwd); trap(\'INT\') { s.shutdown }; s.start\"
, #!/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.