用 Apache 直接使用 erb
※ 引述《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
Ruby 近期熱門文章
PTT數位生活區 即時熱門文章