如何設定 Redmine 以進行進階 git 整合¶
範圍¶
安裝於 Centos 6.x
本操作指南說明如何透過 git 1.6.6 中引入的基於 http 的 git-smart-http 協定 在 apache 上提供 git 儲存庫服務。
與 ssh 或基於 git 的存取相比,git-smart-http 提供了多項優點:您可以按原樣使用 redmine 存取控制,不需要額外的 ssh 金鑰或其他東西,您可以根據需要透過 SSL 保護它,而且與 ssh 和 git 埠相比,防火牆和 https/https 埠通常遇到的問題較少。 git-smart-http 也沒有其「笨拙」前身的一些缺點,因為它不需要任何複雜的 DAV 設定。
本操作指南主要憑記憶撰寫,並在已經提供 與 redmine 整合的 svn 儲存庫 的設定上執行,因此我可能會忘記一些事情或將其視為理所當然。
這是一個 wiki 頁面,歡迎您更正或修改您發現的任何不足之處 :-) 您也可以 與我聯繫。
將 grack 與 redmine 整合的另一個選擇是 修改過的 grack+redmine 外掛 或 任何其他為 redmine 修改的 grack,儘管這些文件缺乏文件,而且我沒有嘗試過,所以我無法對這些發表太多意見。
先決條件¶
- 具有 mod_perl 的 Apache(存取控制)
- git(版本至少為 1.6.6)
- 一種提供 git-smart-http 服務的方式
- 如果您想使用預設的 git-http-backend,則需要 mod_cgi(或 mod_cgid)
- 如果您想使用 grack(基本上是正確 git 命令周圍的 rack 包裝器)或
git-webby(另一個基於 grack 但以 Sinatra 編寫的實現)則需要 rack 伺服器。
您應該已經有一個 rack 伺服器來執行 redmine,這就是我選擇 grack 作為後端並將在本教學課程中說明的原因。
使用內建的 git-http-backend 應該相當簡單(跳過 grack 安裝 部分並使用 git-http-backend 進行安裝(git-http-backend 線上手冊 中有一些範例),完成後繼續進行 存取控制 部分)。
安裝 Git¶
yum install git
安裝 grack¶
取得原始碼¶
從 github 儲存庫 取得 grack,我將我的儲存庫 checkout 到 /var/www/grack
cd /var/www
git clone http://github.com/schacon/grack.git
並為儲存庫建立一個目錄
mkdir /opt/repositories
mkdir /opt/repositories/git
chown -R apache:apache /opt/repositories/git
設定¶
編輯 config.ru
檔案並根據您的本機設定進行調整。 project_root
必須包含 git 儲存庫所在目錄的路徑,git_path
顯然必須包含 git 的路徑,我的看起來像這樣(在 gentoo 上)
vi /var/www/grack/config.ru
並編輯檔案
$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/lib')
use Rack::ShowExceptions
require 'grack'
require 'git_adapter'
config = {
:project_root => "/opt/repositories/git",
:git_path => '/usr/bin/git',
:upload_pack => true,
:receive_pack => true,
}
run GitHttp::App.new(config)
如果您使用的是最新版本的 grack,那麼這個 config.ru 檔案可能可以使用
$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/lib')
use Rack::ShowExceptions
require 'grack'
require 'git_adapter'
config = {
:project_root => "/opt/repositories/git",
:git_path => '/usr/bin/git',
:upload_pack => true,
:receive_pack => true,
:adapter => Grack::GitAdapter,
}
run Grack::App.new(config)
不要忘記安裝 grack 的相依套件
$ cd /var/www/grack
$ bundle install
與 Apache 整合¶
您顯然可以在此使用任何您喜歡的 rack 伺服器,但存取控制機制 Redmine.pm
是使用 mod_perl 為 apache 編寫的,因此您至少需要透過 apache 反向代理您的 rack 伺服器。
我選擇的 rack 伺服器是 passenger(效能穩定、apache 模組、設定大多很簡單),而且它已經在我的系統上設定好了。
由於 passenger 的安裝和設定不在本操作指南的範圍內,請參閱 passenger 文件 或您發行版的 passenger 安裝指南。
還需要做一點工作才能讓 passenger 與其配合使用,您需要在 grack 目錄中建立 public
和 tmp
目錄。
另請注意,在標準設定中,passenger 將使用擁有 config.ru
檔案的相同使用者和群組執行 grack 應用程式。 這個使用者必須對 git 儲存庫具有讀取和寫入權限!
在 /var/www/grack 中為 apache 建立目錄 'public' 和 'tmp'
cd /var/www/grack
mkdir public
mkdir tmp
chown -R apache:apache /var/www/grack
編輯設定檔 "/etc/httpd/conf/httpd.conf",透過移除註解來支援多虛擬主機
NameVirtualHost *:80
建立虛擬主機檔案
vi /etc/httpd/conf.d/git.conf
內容為
<VirtualHost *:80>
ServerName git.yourdomain.com
DocumentRoot "/var/www/grack/public"
<Directory "/var/www/grack/public">
Options None
AllowOverride None
<IfVersion < 2.3 >
Order allow,deny
Allow from all
</IfVersion>
<IfVersion >= 2.3>
Require all granted
</IfVersion>
</Directory>
</VirtualHost>
在網域名稱頁面的控制台 DNS 記錄中,建立一個名稱為「git.yourdomain.com」的子網域,並指向您的 IP 伺服器。
此時,如果您在 "/opt/repositories/git/myrepo" 中有一個儲存庫,您應該能夠透過 "http://git.yourdomain.com/myrepo" 存取它,例如
使用 Gitbash 或 TortoiseGit 等 git 用戶端來複製儲存庫
git clone http://git.yourdomain.com/myrepo
如果成功,伺服器上的 git 和連線都非常好!
存取控制¶
您現在有一個可以運作的 git 伺服器,但沒有存取控制。 目前,用於存取控制的附帶 perl 模組 Redmine.pm
(在您的 redmine 目錄中的 extra/svn/
中)不支援 git-smart-http 協定的存取控制,#4905 中的修補程式旨在實作該功能。
套用修補程式¶
如果您使用的是 Redmine >= 2.1.0,請跳至設定 Apache
從 #4905 下載最新(或更好:正確)版本的修補程式到您的 Redmine 目錄。在 Redmine 目錄中,應用修補程式:patch -p1 < the-patch-file.patch
應該可以運作(如果它告訴您無法應用區塊,則表示修補程式與您的 Redmine.pm
版本不相容,如果它顯示其他內容,請嘗試 patch -p0 < the-patch-file.patch
或 patch Redmine.pm < the-patch-file.patch
,如果仍然無法運作,請在 #4905 上尋求建議)。
您可能仍然需要從這裡編輯檔案,因為目前版本的修補程式僅適用於從 此步驟已不再需要,修補程式已更新為會將 Apache 的 http://git.myhost.com/git/myrepo
提供的儲存庫,但上面的範例使用的是 http://git.myhost.com/myrepo
。Location
區塊中的資訊納入考量。
設定 Apache¶
您現在必須讓 Apache 知道您的新驗證模組(如果您已經為 Subversion 整合完成了此步驟,則可以直接跳到 Location
指令)。
將 Redmine.pm
(從您的 extra/svn/
目錄)複製或連結到 /usr/lib/perl5/Apache/Redmine.pm
(Ubuntu)或您的發行版放置 Apache Perl 模組的任何位置(例如 Gentoo 將它們放在 /usr/lib64/perl5/vendor_perl/5.8.8/Apache/
,Fedora 將它們放在 /usr/lib64/perl5/vendor_perl/Apache/
)。
完成後,重新載入 Apache 以確保修補階段的一切順利(如果沒有,請移除連結或在上一步中建立的檔案,然後重新啟動 Apache 以恢復 Apache,嘗試在您的 Redmine.pm 檔案中找出錯誤)。現在編輯您的虛擬主機設定,使其看起來像這樣(與上面相同,但包含更多內容)
vi /etc/httpd/conf.d/git.conf
其中
<VirtualHost *:80>
ServerName git.yourdomain.com
DocumentRoot "/var/www/grack/public"
<Directory "/var/www/grack/public">
Options None
AllowOverride None
<IfVersion < 2.3 >
Order allow,deny
Allow from all
</IfVersion>
<IfVersion >= 2.3>
Require all granted
</IfVersion>
</Directory>
PerlLoadModule Apache::Redmine
<Directory "/var/www/grack/public">
Options None
AllowOverride None
<IfVersion < 2.3 >
Order allow,deny
Allow from all
</IfVersion>
<IfVersion >= 2.3>
Require all granted
</IfVersion>
</Directory>
<Location "/">
AuthType Basic
AuthName "Redmine git repositories"
Require valid-user
PerlAccessHandler Apache::Authn::Redmine::access_handler
PerlAuthenHandler Apache::Authn::Redmine::authen_handler
RedmineDSN "DBI:mysql:database=your_database;host=localhost:3306"
RedmineDbUser "user_database"
RedmineDbPass "password_database"
RedmineGitSmartHttp yes
</Location>
</VirtualHost>
重新啟動您的 Apache,一切應該就會正常運作 :-)
已知問題¶
如果您在 Apache 下直接使用預設的 git-http-backend,並且在 Apache 錯誤日誌中發現「不支援的請求:'/git/your-git-repo'」之類的錯誤,您可能需要在 Apache 設定中設定的環境變數列表中新增「SetEnv REMOTE_USER=$REDIRECT_REMOTE_USER」。
不幸的是,此設定可能會導致 Redmine 故障。如果是這樣,您將只需要為透過 git-http-backend 傳遞的請求設定變數。一種方法是使用 mod_rewrite。以下是 Fedora 17 系統的範例 Apache 設定,該系統使用 git-http-backend 和 mod_rewrite。
在 httpd.conf 中
Listen xxx.xxx.xxx.xxx:80
<VirtualHost xxx.xxx.xxx.xxx:80>
DocumentRoot /var/www/redmine/public
ServerName servername.domain:80
Include conf/servername.conf
</VirtualHost>
Listen xxx.xxx.xxx.xxx:443
<VirtualHost xxx.xxx.xxx.xxx:443>
DocumentRoot /var/www/redmine/public
ServerName servername.domain:443
Include conf/servername.conf
Include conf/ssl.conf
</VirtualHost>
在 servername.conf 中
PerlLoadModule Apache::Authn::Redmine
SetEnv GIT_PROJECT_ROOT /git-1/repositories
SetEnv GIT_HTTP_EXPORT_ALL
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} ^off$
RewriteCond %{REQUEST_URI} !^/git-private/
RewriteRule ^.*$ https://servername.domain$0 [R=301,L]
RewriteRule ^/git/(.*/objects/[0-9a-f]{2}/[0-9a-f]{38})$ /git-1/repositories/$1 [L]
RewriteRule ^/git/(.*/objects/pack/pack-[0-9a-f]{40}.(pack|idx))$ /git-1/repositories/$1 [L]
RewriteRule ^/git/(.*)$ /usr/libexec/git-core/git-http-backend/$1 [E=REMOTE_USER:$REDIRECT_REMOTE_USER,H=cgi-script,L]
</IfModule>
<Directory /usr/libexec/git-core>
<Files "git-http-backend">
Options +ExecCGI
</Files>
</Directory>
<Location /git>
AuthType Basic
AuthName "CAMPUS"
AuthBasicProvider external
AuthExternal pwauth
Require valid-user
PerlAccessHandler Apache::Authn::Redmine::access_handler
PerlAuthenHandler Apache::Authn::Redmine::authen_handler
RedmineDSN "DBI:mysql:database=redmine;host=localhost"
RedmineDbUser "redmine"
# RedmineDbPass "password"
RedmineGitSmartHttp yes
</Location>
Alias /git-private /git-1/repositories
<Location /git-private>
Order deny,allow
Deny from all
<Limit GET PROPFIND OPTIONS REPORT>
Options Indexes FollowSymLinks MultiViews
Allow from 127.0.0.1
Allow from localhost
</Limit>
</Location>
<Directory "/var/www/redmine/public">
RailsEnv production
RailsBaseURI /
Options -MultiViews
AllowOverride All
</Directory>
在 conf/ssl.conf 中
LogLevel warn
SSLEngine on
SSLProtocol all -SSLv2
SSLCipherSuite RC4-SHA:AES128-SHA:ALL:!ADH:!EXP:!LOW:!MD5:!SSLV2:!NULL
SSLCertificateFile /etc/pki/tls/certs/your-server.crt
SSLCertificateKeyFile /etc/pki/tls/private/your-server.key
SSLCertificateChainFile /etc/pki/tls/certs/server-chain.crt
SSLCACertificateFile /etc/pki/tls/certs/ca-bundle.crt
<Files ~ "\.(cgi|shtml|phtml|php3?)$">
SSLOptions +StdEnvVars
</Files>
<Directory "/var/www/cgi-bin">
SSLOptions +StdEnvVars
</Directory>
SetEnvIf User-Agent ".*MSIE.*" \
nokeepalive ssl-unclean-shutdown \
downgrade-1.0 force-response-1.0
在 conf.d/ssl.conf 中
LoadModule ssl_module modules/mod_ssl.so
SSLPassPhraseDialog exec:/usr/libexec/httpd-ssl-pass-dialog
SSLSessionCache shmcb:/var/cache/mod_ssl/scache(512000)
SSLSessionCacheTimeout 300
SSLMutex default
SSLRandomSeed startup file:/dev/urandom 256
SSLRandomSeed connect builtin
SSLCryptoDevice builtin
您還需要安裝 Perl 模組 Net::LDAP、Authen::Simple 和 Authen::Simple::LDAP。前兩個模組可在 Fedora 的預設軟體套件庫中找到。
第三個模組必須在前兩個模組安裝之後安裝,並且必須直接從 CPAN 取得。以下是我用於在 Fedora 17 上安裝這些套件的指令。
yum -y install gcc make perl-LDAP perl-Authen-Simple
cpan
cpan> install Authen::Simple::LDAP
由 Vladimir Skubriev 於 將近 11 年前 更新 · 25 個修訂