使用apache cgi运行ruby脚本

在ubuntu下

进入 /usr/lib/cgi-bin目录 新建文件 ruby.cgi

内容如下

#!/usr/bin/ruby 
print "Content-type: text/html\r\n\r\n" 
print "<html><body>Hello World! It's #{Time.now}</body></html>\r\n" 

为文件加可执行权限 sudo chmod a+x ruby.cgi

重启apache2 sudo apache2 restart

打开火狐浏览器地址:localhost/cgi-bin/ruby.cgi

在windows下

只需更改ruby的安装路径,比如我的是C:/RailsInstaller/Ruby1.9.3/bin/ruby.exe

#!C:/RailsInstaller/Ruby1.9.3/bin/ruby.exe
print "Content-type: text/html\r\n\r\n" 
print "<html><body>Hello World! It's #{Time.now}</body></html>\r\n" 

参考:

Ruby CGI编程教程 http://www.yiibai.com/ruby/ruby_web_applications.html

http://www.cppblog.com/AutomateProgram/archive/2013/01/06/197009.html