自己動手寫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萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章