SVN(SubVersion)是新一代版本控制軟體,它在CVS的基礎上開發,修正了CVS的種種弊端,有望成為開源界CVS的替代品。
(1) 準備工作
可從svn的官方網站http://subversion.tigris.org/下載最新版本的svn。
$ cd ~/build$ wget http://subversion.tigris.org/downloads/subversion-1.3.0.tar.gz$ tar zxvf subversion-1.3.0.tar.gz$ cd subversion-1.3.0$ more INSTALL...I. BUILD REQUIREMENTS =====================...1. Apache Portable Runtime 0.9.7 (http://apr.apache.org/)## svn的發行包已經包含APR和APR-util,如果你不希望用預設的包,你可以在./configure## 中選項--with-apr=和--with-apr-util=指定## 你也可以下載最新的apr和apr-util包,然後存放為./apr和./apr-util,## 這樣它就會參與整個編譯過程...2. autoconf 2.50 or newer (Unix only)## 僅在計劃編譯最新版本的源碼才要...3. libtool 1.4 or newer (Unix only)## 僅在計劃編譯最新版本的源碼才要...4. Neon library 0.24.7 or 0.25.3 (http://www.webdav.org/neon/)The Neon library allows a Subversion client to interact with remoterepositories over the Internet via a WebDAV based protocol.## 你可以下載Neon-x.x.x,然後存放為./neon,這樣它會參與整個編譯過程## 你可以指定一個已經編譯好的neon,這需要在./configure中指定LDFLAGS變數,## 還需要指定--with-neon=選項,這個選項所指的目錄中應該有bin/neon-config程式...5. Berkeley DB 4.X## 如果不打算使用Berkeley DB做版本庫,或者僅僅安裝svn用戶端,則可不用裝## 建議 Berkeley DB 4.3或者4.2## 你可以在./configure中指定位置 --with-berkeley-db=/usr/local/BerkeleyDB.4.3...6. Apache Web Server 2.0.49 or newer(http://httpd.apache.org/download.cgi)## 用來存取你的subversion倉庫## 當然你也可以用svn自己的server:svnserver...6.1 Apache Web Server 2.0.54 or newer (Windows only) (http://httpd.apache.org/download.cgi)...7. Python 2.0 (http://www.python.org/)## 主要用作make check,應該可以不要...8. Visual C++ 6.0 or newer (Windows Only)...9. Perl 5.8 or newer (Windows only)...10. MASM 6 or newer (Windows only, optional)...11. Libraries for our libraries## 主要是一些相關庫,比如Neon需要SSL加密,則OpenSSL庫必須要有......II. INSTALLATION ===============A. Building from a Tarball or RPM...2. Building from an RPM...Unpack it, and use the standard GNU procedure to compile: $ ./configure $ make # make install You can also run the full test suite by running 'make check'....B. Building the Latest Source under Unix## 如果你已經安裝了Subversion,而又想更新到最新版,則看看這個...III. BUILDING A SUBVERSION SERVER =============================A. Setting Up Apache1. Obtaining and Installing Apache 2.0...$ svn co / http://svn.apache.org/repos/asf/httpd/httpd/branches/2.0.x httpd-2.0 Checkout the "apr" and "apr-util" modules into the srclib/ directory: $ cd httpd-2.0/srclib $ svn co / http://svn.apache.org/repos/asf/apr/apr/branches/0.9.x apr $ svn co / http://svn.apache.org/repos/asf/apr/apr-util/branches/0.9.x apr-util At the top of the httpd-2.0 tree: $ ./buildconf $ ./configure --enable-dav --enable-so --enable-maintainer-mode## 你還可以通過--with-dbm=db4和--with-berkeley-db=/usr/local/BerkeleyDB.4.2## 指定BerkeleyDB的位置## --enable-ssl啟用SSL支援## --enable-deflate啟用壓縮支援...Compile and install apache: $ make && make installB. Making and Installing the Subversion Server## 切換到svn源碼目錄,可以$ ./configure --with-apxs=...配置Subversion## 當make完後應該可以在Apache的modules目錄發現 mod_dav_svn.so...C. Configuring Apache for Subversion## 確保你的httpd.conf在載入mod_dav.so之後有這麼一行:## LoadModule dav_svn_module modules/mod_dav_svn.so#### 在httpd.conf的底部,配置如下:## ## DAV svn## SVNPath /absolute/path/to/repository## #### 為了限制訪問,你可以在Location裡加上:## AuthType Basic## AuthName "Subversion repository"## AuthUserFile /my/svn/user/passwd/file#### 如果你想讓讀和寫都要驗證,可加上 Require valid-user#### 如果僅僅讓寫要驗證,加上## ## Require valid-user## #### 如果要對讀和寫分開控制,可以加上 ## AuthGroupFile /my/svn/group/file## ## Require group svn_committers## ## ## Require group svn_committers## Require group svn_readers## #### mod_dav需要根據網域名稱判斷伺服器,因此必須配置正確的ServerName。## 如果svn庫是在基於名稱的虛擬機器主機中,需要配置ServerAlias#### 如果要指定內容壓縮,可配置## SetOutputFilter DEFLATE#### 注意,Apache使用者(通常是nobody)必須能讀Berkeley DB檔案... E. Alternative: 'svnserve' and ra_svn## 另一種可選的網路層是svn自己的通訊協定,用戶端是 libsvn_ra_svn,## 服務段是svnserver進程,這種通訊沒有加密措施。... $ svnserve -d # becomes a background daemon $ svn checkout svn://localhost/usr/local/svn/repository...'svnserve' has built-in CRAM-MD5 authentication (so you canuse non-system accounts), and can also be tunneled over SSH(so you can use existing system accounts). ...
這段文字說明了如何安裝svn以及如何佈建服務程式。根據這段文字的指示,我們開始我們的svn安裝配置之旅吧。
(2) 安裝Berkeley DB 4.3.29
$ cd ~/build$ wget http://downloads.sleepycat.com/db-4.3.29.tar.gz$ tar zxvf db-4.3.29.tar.gz$ cd db-4.3.29/build_unix$ ../dist/configure --prefix=/usr/local/BerkeleyDB-4.3.29 --enable-cxx --enable-rpc$ make$ make install$ echo /usr/local/BerkeleyDB-4.3.29/lib >> /etc/ld.so.conf$ ldconfig -v
(3) 安裝Apache 2.0.55
$ cd ~/build$ wget http://mirror.vmmatrix.net/apache/httpd/httpd-2.0.55.tar.gz$ tar zxvf httpd-2.0.55.tar.gz$ cd httpd-2.0.55$ CFLAGS="-O2" ./configure --prefix=/usr/local/apache2 /--enable-rewrite=shared --enable-dav=shared --enable-so /--with-dbm=db4 --with-berkeley-db=/usr/local/BerkeleyDB-4.3.29 /--enable-deflate=shared --enable-ssl=shared$ make$ make install
(4) 安裝Subversion 1.3.0
$ cd ~/build$ cd subversion-1.3.0$ ./configure --prefix=/usr/local/svn-1.3.0 /--with-apxs=/usr/local/apache2/bin/apxs --with-ssl /--with-berkeley-db=/usr/local/BerkeleyDB-4.3.29 /--with-zlib$ make$ make install$ ln -s /usr/local/svn-1.3.0 /usr/local/svn
為了方便我們使用,我們建立了一個軟串連/usr/local/svn。
(5) 配置svnserve
我們先配置svnserve服務方式,過後我們再考慮使用Apache方式實現服務。
首先,給我們的svn服務一個名分。IANA為Subversion協議保留3690連接埠,你可以加在/etc/services中。
$ vi /etc/services...svn 3690/tcp # Subversionsvn 3690/udp # Subversion...
其次,準備你的svn倉庫,配置你的svnserve。
# SVN版本庫將放置在/usr/local/repository/svn/test下,倉庫類型為BerkeleyDB$ svnadmin create --fs-type bdb /usr/local/repository/svn/test# 建立svn使用者運行svn服務$ useradd -s /sbin/nologin -d /dev/null svn$ chown -R svn.svn /usr/local/repository/svn# 建立svnserve的設定檔$ vi /usr/local/repository/svn/test/conf/svnserve.conf[general]password-db = passwdrealm = example realmanon-access = readauth-access = write$ vi /usr/local/repository/svn/test/conf/passwdlarry = foopasswdmarchday=barpasswd
我們編輯svnserve設定檔svnserve.conf,通過CRAM-MD5方式認證使用者。使用者名稱和口令對儲存在檔案passwd中。匿名使用者可讀,認證使用者可寫。此外還有一個設定檔authz,可微調權限控制,這裡不作改變。
svnserve也可以通過SSH進行認證,這種方式稍後再講。
svnserve的啟動方式有兩種選擇:配置xinetd(或者inited)或者以獨立的進程啟動。
對於xinetd啟動的方式,你可以建立一個檔案 /etc/xinetd.d/svn,內容如下:
service svn{ disable = no socket_type = stream wait = no user = svn server = /usr/local/svn/bin/svnserve server_args = -i -r /usr/local/repository/svn log_on_failure += USERID}
然後運行命令 service xinetd restart 就可以了。
而對於後一種方式,你可以直接通過 -d 參數啟動svnserver。
$ /usr/local/svn/bin/svnserver -d -r /usr/local/repository/svn
如果你檢查一下網路服務狀態,應該可以發現svn連接埠正被監聽。
$ netstat -la...tcp 0 0 *:svn *:* LISTEN...
我們做個測試,往版本庫裡添加項目,看看svnserver是否正常工作。
$ cd /tmp$ mkdir svntest$ cd svntest$ mkdir {trunk,branches,tags}$ svn import . file:///usr/local/repository/svn/test/svntest /--message "first test project"Adding trunkAdding branchesAdding tagsCommitted revision 1.$ svnlook tree /usr/local/repository/svn/test/ svntest/ trunk/ branches/ tags/$ cd ../$ rm -rf svntest$ svn checkout svn://localhost/test/svntestA svntest/trunkA svntest/branchesA svntest/tagsChecked out revision 1.
OK,看來我們的svn已經能正確工作了。
(6) 整合svn和SSH
基本原理:當用戶端通過svn命令串連 ssh+ssh://url時,svn用戶端開啟一個SSH客戶進程串連到遠端SSH服務端,接著由遠端SSH服務端以管道模式開啟svnserve進程。當SSH會話終止時,svnserve自動結束。
可見,負責網路監聽的是sshd,svnserve和普通的程式沒什麼兩樣。svnserve的運行身份為當前通過ssh登入的使用者身份,所以對於版本庫的存取必須要有許可權(可把使用者加入svn使用者組)。
用戶端無法緩衝口令,因此每次提交修改時都要輸入口令,不過這可以通過ssh-agent解決。此外,還有一點比較麻煩,就是控制svnserve的運行方式比較不便,比如指定-r參數說明版本庫的絕對路徑。不過這可以通過配置authorized_users檔案解決。具體實現請參照svn的文檔,這裡不作解釋了。
做個實驗。我們把xinetd中的svn服務關掉,如果有獨立啟動並執行服務svnserver,也請關掉。
$ cp /usr/local/svn/bin/svnserve /usr/bin$ usermod -G svn marchday$ chmod 02775 /usr/local/repository/svn/test/db$ su marchday$ cd /tmp$ svn checkout svn+ssh://localhost/usr/local/repository/svn/test/svntestmarchday@127.0.0.1's password:A svntest/trunkA svntest/branchesA svntest/tagsChecked out revision 1.
首先,我們需要把svnserve拷貝到一個shell能找到的地方,比如/usr/bin。然後修改許可權,使marchday對於svn db目錄可寫。最後,我們訪問版本庫的時候,要用全路徑。
(7) 整合svn和Apache
激動人心的時刻終於到了,我們在這裡將要配置一個具有安全保護且便於使用的svn版本控制系統。前提是,上述(2), (3), (4)步的工作必須要做到位了。
首先,修改httpd.conf,確保已經載入了必要的模組。
LoadModule dav_module modules/mod_dav.soLoadModule dav_svn_module modules/mod_dav_svn.so
接下來,需要暴露版本庫所在的路徑。
<Location /svn> DAV svn SVNPath /usr/local/repostiroy/svn/test</Location>
如果你有多個版本庫可供訪問,每個版本庫的訪問方式如http://my.server.com/svn/test,
http://my.server.com/svn/product1的形式,可做如下配置:
<Location /svn> DAV svn SVNParentPath /usr/local/repository/svn</Location>
修改User和Group選項,使其可訪問版本庫檔案。
User svnGroup svn
此外,或許還需要配置ServerName,如果通過NameVirtualHost指示使用Apache的虛擬機器主機,或許需要ServerAlias來指定額外的名稱。這裡設定ServerName為127.0.0.1,不通過虛擬機器主機訪問。且監聽連接埠為8080。
如下,我們的webdav已經能work了。先喝口水,高興一下。:-)
$ /usr/local/apache2/bin/apachectl start$ cd /tmp$ svn checkout http://localhost:8080/svn/test/svntestA svntest/trunkA svntest/branchesA svntest/tagsChecked out revision 1.
下面我們給它加上認證的功能。
首先,加上ssl保護,沒有保護的認證同樣是很危險的。
產生自簽署的認證:
$ cd /usr/local/apache2$ mkdir certs$ cd certs$ openssl genrsa -des3 -out ca.key 1024 # 產生根憑證的私密金鑰匙$ openssl req -new -x509 -days 365 -key ca.key -out ca.crt # 產生根憑證$ openssl genrsa -des3 -out server.key 1024 # 產生伺服器的私密金鑰匙$ openssl req -new -key server.key -out server.csr # 產生伺服器的請求籤證檔案 $ ../bin/sign.sh server.csr # 簽署伺服器憑證,結果產生server.crt檔案
其中指令碼sign.sh是從mod_ssl源碼目錄pkg.distrib下拿來的,它簡化了認證簽署步驟。如果你想建立自己的認證體系,可參考我的另一篇關於OpenSSL的文章。
配置conf/ssl.conf,啟用SSL支援。
SSLCertificateFile /sinaad/apache2/certs/server.crtSSLCertificateKeyFile /sinaad/apache2/certs/server.key
然後啟動apache。
$ /usr/local/apache2/bin/apachectl startssl
現在,應該可以通過https訪問你的網站了。
有一個比較煩的地方是每次apachectl startssl都要求輸入server.key的口令,你可以通過下面的方法去掉口令輸入:
$ cp server.key server.key.org$ openssl rsa -in server.key.org -out server.key$ chmod 400 server.key
現在,給apache加上基本認證功能。
$ cd /usr/local/apache2/conf$ htpasswd -cm svnusers harryNew password: ***** Re-type new password: *****Adding password for user harry$ htpasswd -m /etc/svn-auth-file sallyNew password: *******Re-type new password: *******Adding password for user sally
口令檔案svnusers建立後,再修改httpd.conf和ssl.conf。我們把以下段落從httpd.conf移動到ssl.conf的裡面。
<Location /svn> DAV svn SVNParentPath /usr/local/repository/svn</Location>
然後加上基本認證配置,最終結果如下:
<Location /svn> DAV svn SVNParentPath /usr/local/repository/svn AuthType Basic AuthName "Subversion repository" AuthUserFile conf/svnusers Require valid-user</Location>
我們的安全有了初步的保障,至少密碼不會以明文的形式在網路上傳輸了,很簡單吧。美中不足的是許可權控制的精度不夠,如果你想精確控制訪問,就得繼續下面的步驟了。
首先,我們需要一個許可權控制檔案,比如下面這樣子:
$ cd /usr/local/apache2/conf$ vi svnauthz[test:/svntest]harry = rw* = r
這裡我們允許harry可讀寫test版本庫的svntest項目,其他人(包括匿名使用者)只能讀。
然後修改httpd.conf,啟用authz_svn_module,這個模組在編譯subversion的時候就產生了。現在我們的httpd.conf如下面這樣:
...LoadModule dav_module modules/mod_dav.soLoadModule rewrite_module modules/mod_rewrite.soLoadModule dav_svn_module modules/mod_dav_svn.soLoadModule authz_svn_module modules/mod_authz_svn.so...
修改ssl.conf,改成下面這樣子:
<Location /svn> DAV svn SVNParentPath /usr/local/repository/svn AuthzSVNAccessFile conf/svnauthz # try anonymous access first, resort to real # authentication if necessary. Satisfy Any Require valid-user AuthType Basic AuthName "Subversion repository" AuthUserFile conf/svnusers Require valid-user</Location>
Satisfy Any配置指明任何使用者都可以訪問,Require valid-user指明在必要的情況下才需要認證使用者,其實這相當於把判斷權交給了authz_svn_module。關於Apache如何和模組進行使用者認證資訊交換,可參考O'Reilly的大作《Writing Apache Modules with Perl and C》。現在,許可權控制的功能已經非常強大了。
(8) WebDAV和自動化(auto versioning)
WebDAV的規範為RFC 2518,基本目標在於把Web變成一個讀寫的媒體。它沒有版本控制,有版本控制的規範為RFC 3253,即DeltaV,不過很少有廠商實現它。
mod_dav_svn是對DeltaV的一種類比,沒有完全符合DeltaV的規範。它的身份是充當mod_dav請求的具體檔案操作,帶來的一個令人激動的特性就是自動版本化(auto versioning):當新版本提交的時候,自動升級版本號碼。
svn 1.2的文檔說為了開啟auto versioning,必須在Apache配置項中設定 SVNAutoversioning on,同時在鉤子指令碼中要防止某些用戶端提交檔案長度為0的請求。我安裝的是svn 1.3,好像SVNAutoversioning預設就設定為on,而且能有效避免重複提交的問題(第一次PUT長度為0的檔案體,緊接著PUT是真正的檔案),已經在近端分享、Dreamweaver、Microsoft Office 2000中測試過。其間遇到過一次Berkeley DB被鎖的情況,運行下面的命令就解決了。
$ /usr/local/apache2/bin/apachectl stop$ svnadmin recover /usr/local/repository/svn/test$ chown -R svn.svn /usr/local/repository/svn/test$ /usr/local/apache2/bin/apachectl startssl
我們的svn配置旅程告一段落,如果你意猶未盡的話,就繼續鑽鑽別的資料吧。
參考資料:
Subversion official recommended book (see http://svnbook.red-bean.com/)O'Reilly, Network Security with OpenSSLApache 2.0.55 manual