自己动手写Redmine https服务端

http://blog.csdn.net/wangjingfei/article/details/5609483


来由:

Redmine默认采用了Webrick服务器,默认启动只支持http服务,但在某些时候,项目管理有较高的保密要求(这里暂且不考虑效率问题),需要开启https服务。网络上介绍开启Redmine https的文章并不少,但是由于软件和操作系统的版本区别,修改方式各不相同,甚至需要修改的文件名称都不相同。这样,与其照网络上的文章修改,不如自己写一个服务脚本。

 

软件列表:

Ruby,Gem,rails,openssl等依赖软件。

 

Ruby快速入门:

http://tech.ddvip.com/2008-01/120059715340597.html

 

(假设redmine放在/home/fify/redmine目录下)

0. 首先将pwd定位到/home/fify/redmine/config/certs目录下

[c-sharp] view plaincopy
  1. mkdir /home/fify/redmine/config/certs  
  2. cd /home/fify/redmine/config/certs  

1. 创建RSA私钥

[c-sharp] view plaincopy
  1. openssl genrsa -des3 -out server.key 1024  

2. 创建CSR(Certificate signing request)

[c-sharp] view plaincopy
  1. openssl req -new -key server.key -out server.csr  

3. 去掉私钥中的passphrase

[c-sharp] view plaincopy
  1. cp server.key server.key.org  
  2. openssl rsa -in server.key.org -out server.key  

4. 创建自签名认证证书

[c-sharp] view plaincopy
  1. openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt  

此时,改目录下的工作已经完成,转移到redmine目录:

[c-sharp] view plaincopy
  1. cd /home/fify/redmine  

5. 创建Webrick启动脚本

[c-sharp] view plaincopy
  1. vi script/server_ssl  

以下是ruby脚本代码:

[ruby] view plaincopy
  1. #!/usr/bin/env ruby    
  2. require File.dirname(__FILE__) + '/../config/boot'    
  3. require 'webrick'    
  4. # 包含必须的库  
  5. require 'webrick/https'    
  6. require 'optparse'    
  7. puts "=> Booting WEBrick..."    
  8. OPTIONS = {    
  9. # 端口号  
  10.   :port         => 3001,    
  11. # 监听主机地址  
  12.   :Host         => "0.0.0.0",    
  13.   :environment  => (ENV['RAILS_ENV'] || "development").dup,    
  14. # 存放redmine中public的路径,这里采用相对路径,保证可移植性  
  15.   :server_root  => File.expand_path(File.dirname(__FILE__) + "/../public/"),    
  16. # 存放私钥的地址  
  17.   :pkey         => OpenSSL::PKey::RSA.new(    
  18.                       File.open(File.dirname(__FILE__) + "/../config/certs/server.key").read),    
  19. # 存放签名证书的地址  
  20.   :cert         => OpenSSL::X509::Certificate.new(    
  21.                       File.open(File.dirname(__FILE__) + "/../config/certs/server.crt").read),    
  22.   :server_type  => WEBrick::SimpleServer,    
  23.   :charset      => "UTF-8",    
  24.   :mime_types   => WEBrick::HTTPUtils::DefaultMimeTypes,    
  25.   :config       => RAILS_ROOT + "/config.ru",    
  26.   :detach       => false,    
  27.   :debugger     => false,    
  28.   :path         => nil    
  29. }    
  30. # 以下读入命令行参数  
  31. ARGV.clone.options do |opts|    
  32.   opts.on("-p""--port=port"Integer,    
  33.           "Runs Rails on the specified port.""Default: 3001") { |v| OPTIONS[:Port] = v }    
  34.   opts.on("-b""--binding=ip"String,    
  35.           "Binds Rails to the specified ip.""Default: 0.0.0.0") { |v| OPTIONS[:Host] = v }    
  36.   opts.on("-d""--daemon""Make server run as a Daemon.") { OPTIONS[:detach] = true }    
  37.   opts.on("-u""--debugger""Enable ruby-debugging for the server.") { OPTIONS[:debugger] = true }    
  38.   opts.on("-e""--environment=name"String,    
  39.           "Specifies the environment to run this server under (test/development/production).",    
  40.           "Default: development") { |v| OPTIONS[:environment] = v }    
  41.   opts.separator ""    
  42.   opts.on("-h""--help""Show this help message.") { puts opts; exit }    
  43.   opts.parse!    
  44. end    
  45. # 设置启动环境,production或development等  
  46. ENV["RAILS_ENV"] = OPTIONS[:environment]    
  47. RAILS_ENV.replace(OPTIONS[:environment]) if defined?(RAILS_ENV)    
  48. # 读取redmine配置文件  
  49. require File.dirname(__FILE__) + "/../config/environment"    
  50. require 'webrick_server'    
  51. require 'webrick/https'    
  52. OPTIONS['working_directory'] = File.expand_path(File.dirname(__FILE__))    
  53. # 初始化带SSL的webrick服务器  
  54. class SSLDispatchServlet < DispatchServlet    
  55.   def self.dispatch(options)    
  56.     Socket.do_not_reverse_lookup = true    
  57.     server = WEBrick::HTTPServer.new(    
  58.       :Port             => options[:port].to_i,    
  59.       :ServerType       => options[:server_type],    
  60.       :BindAddress      => options[:Host],    
  61.       :SSLEnable        => true,    
  62.       :SSLVerifyClient  => OpenSSL::SSL::VERIFY_NONE,    
  63.       :SSLCertificate   => options[:cert],    
  64.       :SSLPrivateKey    => options[:pkey],    
  65.       :SSLCertName      => [ [ "CN", WEBrick::Utils::getservername ] ]    
  66.     )    
  67.     server.mount('/', DispatchServlet, options)    
  68.     trap("INT") { server.shutdown }    
  69.     server.start    
  70.   end    
  71. end    
  72. # 输出启动提示  
  73. puts "=> Rails #{Rails.version} application starting on https://#{OPTIONS[:Host]}:#{OPTIONS[:port]}"    
  74. # 如果用户在命令行输入“-d”参数,则程序将在后台运行  
  75. if OPTIONS[:detach]    
  76.   Process.daemon    
  77.   pid = "#{RAILS_ROOT}/tmp/pids/server.pid"    
  78.   File.open(pid, 'w'){ |f| f.write(Process.pid) }    
  79.   at_exit { File.delete(pid) if File.exist?(pid) }    
  80. end    
  81. # 没有“-d”参数时在终端输出提示,此时可以通过“ctrl+c”关闭服务器  
  82. puts "=> Call with -d to detach"    
  83. trap(:INT) { exit }    
  84. puts "=> Ctrl-C to shutdown"    
  85. # 启动webrick服务器  
  86. SSLDispatchServlet.dispatch(OPTIONS)   

6. 将脚本参数设置为可执行

[c-sharp] view plaincopy
  1. chmod +x script/server_ssl  

7. 启动Ruby脚本

[c-sharp] view plaincopy
  1. ruby script/server_ssl -e production // 在终端运行  
  2. 或  

参考:

1. http://www.zunisoft.com/?p=740&cpage=1

2. (CentOS5)/usr/lib/ruby/gems/1.8/gems/rails-2.3.5/lib/commands/server.rb

发布了38 篇原创文章 · 获赞 6 · 访问量 20万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章