作者contagious (布谷饱吃不堡)
看板Ruby
标题用 Apache 直接使用 erb
时间Fri May 19 23:12:21 2006
※ 引述《yalight (ㄚ光)》之铭言:
: 顺便问一下...
: 如何在 windows + apache 下装 Ruby??
: 不是把 ruby 当成 CGI 执行...
: 而是嵌入网页那种
: eruby, erb 等等...
: 找老半天都没找到成功的方法 @_@"
请看这两篇:
http://rubyurl.com/tBQ
http://rubyurl.com/GvR
简单讲一下,基本上就是告诉 apache 多了一种特别的 file ,副档名叫 rhtml
用 mod_action 把所有 rhtml 的档案转给一个 cgi 去处理,这个 cgi 要
做的事就是把拿到的档案丢给 erb 翻译之後再输出
下面是我的设定,我实验的机器是 Ubuntu..不过在 windows 上也应该一样
首先我先做一个 virtual host
NameVirtualHost *
<VirtualHost *>
ServerName erb.mymachine.mydomain
DocumentRoot /var/erb
<Directory />
Options ExecCGI FollowSymLinks
AddHandler cgi-script .cgi .rb
AddHandler rubypage .rhtml
Action rubypage /cgi-bin/erb.rb
</Directory>
</VirtualHost>
当然你要载入 mod_cgi, mod_action 等等..
然後在 /var/erb/cgi-bin/ 放一个档案叫 erb.rb,内容如下
#!/usr/local/bin/ruby
require 'time'
require 'erb'
time = Time.now.httpdate
LOGFILE = 'erblog.txt'
HEADERS = <<EOF
Date: #{time}
Last-Modified: #{time}
Content-Type: text/html
EOF
begin
file_path = ENV['REDIRECT_URL'].include?(File.basename(__FILE__)) ?
ENV['SCRIPT_URL'] : ENV['REDIRECT_URL']
path = File.expand_path(ENV['DOCUMENT_ROOT'] + '/' + file_path)
raise "Attempt to access invalid path: #{path}" unless
path.index(ENV['DOCUMENT_ROOT']) == 0
erb = File.open(path) { |f| ERB.new(f.read) }
print HEADERS + erb.result(binding)
rescue
begin
File.open(LOGFILE, 'a+') do |f|
f.write("An error has occurred at #{time}:
#{$!.inspect}\n#{$!.backtrace.inspect}\n\n")
end
rescue
ensure
print "Content-Type: text/html\n\n"
print "Sorry, a server error has occurred. Please try again later."
end
end
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 59.120.143.109