Ubuntu 14.04 下搭建SVN服务器 svn://

安装软件包:

sudo apt-get install subversion

之后选择SVN服务文件及配置文件的放置位置。我放在了/srv下的svn目录。

cd /srv

sudo mkdir svn

我的svn版本仓库叫tone_src

cd /srv/svn

sudo mkdir tone_src

目录建好后 创建版本仓库

sudo svnadmin create /srv/svn/tone_src

执行之后 tone_src下文件结构如下:  

tone@ubuntu:/srv/svn/tone_src$ ls -l
总用量 24
drwxr-xr-x 2 root root 4096  1月 15 10:52 conf
drwxr-sr-x 6 root root 4096  1月 15 14:52 db
-r--r--r-- 1 root root    2  1月 15 10:50 format
drwxr-xr-x 2 root root 4096  1月 15 10:50 hooks
drwxr-xr-x 2 root root 4096  1月 15 10:50 locks
-rw-r--r-- 1 root root  246  1月 15 10:50 README.txt

下面进行配置:

我们需要修改conf目录下的三个文件,authz;passwd;svnserve.conf

编辑svnserve.conf

[general]
#匿名用户不可读
anon-access = none     [配置的格式:顶头不能留空格,否则无法解析]
#权限用户可写
auth-access = write
#密码文件为passwd
password-db = passwd
#权限文件为authz
authz-db = authz

编辑authz 制定管理员组 即admin组的用户为tone admin组有rw(读写权限) 所有人有r(读权限)

[groups]
admin= tone

[/]
@admin =rw
*=r

这里组的名字 不一定叫admin 你的管理员组名 可以叫做任意的名字,另外比如admin组还有其他用户,可以这样制定 admin=tone,tone1,tone2 类似这样的写法

编制passwd 文件 设定用户密码

[users]
# harry = harryssecret
# sally = sallyssecret
tone=www

tone的密码为www 对 没看错 明文的。

以上都做完之后,就可以开启你的svn服务器了。

sudo svnserve -d -r /srv/svn/

-d 已守护模式启动

-r 制定svn版本库根目录 这样是便于客户端不用输入全路径 就可以访问版本库了


添加到自动启动

(1)脚本

#!/bin/bash
svnserve -d -r /home/svn



 将脚本放置到/etc/init.d/


修改权限: chmod +x startsvn.sh


使用ubuntu工具update-rc.d


update-rc.d starsvn.sh defaults(这个命令等效于update-rc.d startsvn.sh start 20 2 3 4 5 . stop 20 0 1 6 .)


20是默认的启动顺序号,可以手动修改,当然也可以在defaults后面加一个启动顺序号。0\1\2\3\4\5\6是ubuntu的运行级别。


备注


/etc/rc runlevel .d/目录下生成/etc/init.d/目录下的启动脚本的链接。



svn地址:

例如:svn://127.0.0.1/tone_src

值得注意的是 我这里是用sudo 启动的  因为之前的svn目录 及tone_src目录 我都是在sudo下创建的。目录的所属权限都是root

如果我以 下面的方式启动 是可以的 但是当客户段提交文件的时候 会出问题,因为此时的svnserve 服务对svn版本库目录没有写的权限
svnserve -d -r /srv/svn/

推荐一个14.04 下的svn 客户端

$ sudo add-apt-repository ppa:rabbitvcs/ppa
$ sudo apt-get update
$ sudo apt-get install rabbitvcs-nautilus3

# Optional
$ sudo apt-get install rabbitvcs-gedit
$ sudo apt-get install rabbitvcs-cli

Ubuntu 14.04 下搭建SVN服务器 svn://_linux

Ubuntu 14.04 下搭建SVN服务器 svn://_linux_02

对于eclipse 客户端的用户,需要安装 subclipse 插件。安装插件时注意客户端的版本和服务端的版本一定要对应好,最好不要考虑兼容,最好就是准确对应,

Ubuntu 14.04 下搭建SVN服务器 svn://_linux_03

截至到目前Ubuntu 14.04 下Subversion 的版本是1.8.8 与他对应的subclipse版本是1.10.8

另外还要用到javahl的版本为1.8.8

具体安装配置如下:

下安装 配置subclipse1.10.8本版 ​​ http://subclipse.tigris.org/files/documents/906/49397/site-1.10.8.zip​

下载后解压 得到一个名为site-1.10.8 的文件夹  请将site-1.10.8放到eclipse 目录下的dropins目录内,重启eclipse

重启之后subclipse1.10.8 就应该可以安装完成了,如果没有成功,检查看文件夹的权限。或者以clean 模式再次启动eclipse

subclipse 安装完成后 就会出现 org.apache.subversion.javahl.clientexception:svn..... 这样的错误

这是因为没有安装javahl  请执行 如下命令进行安装。

sudo apt-get install libsvn-java

安装完毕之后进行一个小的配置,

先查找 javahl所在路径

sudo find /usr -name libsvnjavahl-1.so

结果显示路径在/usr/lib/x86_64-linux-gnu/jni

修改eclipse.ini 增加 一行-Djava.library.path=/usr/lib/x86_64-linux-gnu/jni
Ubuntu升级到12.04以后,JavaHL Library的位置由原来/usr/lib/jni改为了/usr/lib/x86_64-linux-gnu/jni
14.04 也是在/usr/lib/x86_64-linux-gnu/jni

-startup
plugins/org.eclipse.equinox.launcher_1.2.0.v20110502.jar
--launcher.library
plugins/org.eclipse.equinox.launcher.gtk.linux.x86_64_1.1.100.v20110505
-product
org.eclipse.epp.package.jee.product
--launcher.defaultAction
openFile
-showsplash
org.eclipse.platform
--launcher.XXMaxPermSize
256m
--launcher.defaultAction
openFile
-vmargs
-Djava.library.path=/usr/lib/x86_64-linux-gnu/jni
-Dorg.eclipse.swt.browser.XULRunnerPath=/opt/commonTools/xulrunner
-Dosgi.requiredJavaVersion=1.5
-XX:MaxPermSize=256m
-Xms40m
-Xmx512m

这样启动eclipse 后 就能使用svn了

Ubuntu 14.04 下搭建SVN服务器 svn://_服务器_04


二、HTTP:// [apache]

1、安装包 [已安装subversion]

$ sudo apt-get install libapache2-svn

2、创建版本仓库:
sudo svnadmin create /目录地址
目录地址必须存在,这个就是保存版本仓库的地方,不同的版本仓库创建不同的文件夹即可,比如:

sudo svnadmin create /home/svn/tang

3、然后把这个版本仓库目录授权给apache读写:

sudo chown -R www-data:www-data  /home/svn/tang

4、然后来到打开apache配置文件:

sudo vim /etc/apache2/mods-available/dav_svn.conf

加入如下内容:

1. # dav_svn.conf - Example Subversion/Apache configuration
2. #
3. # For details and further options see the Apache user manual and
4. # the Subversion book.
5. #
6. # NOTE: for a setup with multiple vhosts, you will want to do this
7. # configuration in /etc/apache2/sites-available/*, not here.
8. # <Location URL> ... </Location>
9. # URL controls how the repository appears to the outside world.
10. # In this example clients access the repository as <a href="http://hostname/svn/">http://hostname/svn/</a>
11. # Note, a literal /svn should NOT exist in your document root.
12. <Location /svn>
13. # Uncomment this to enable the repository
14. DAV svn
15. # Set this to the path to your repository
16. SVNPath /home/svn/tang
17. # Alternatively, use SVNParentPath if you have multiple repositories under
18. # under a single directory (/var/lib/svn/repo1, /var/lib/svn/repo2, ...).
19. # You need either SVNPath and SVNParentPath, but not both.
20. #SVNParentPath /home/svn/tang
21. # Access control is done at 3 levels: (1) Apache authentication, via
22. # any of several methods. A "Basic Auth" section is commented out
23. # below. (2) Apache <Limit> and <LimitExcept>, also commented out
24. # below. (3) mod_authz_svn is a svn-specific authorization module
25. # which offers fine-grained read/write access control for paths
26. # within a repository. (The first two layers are coarse-grained; you
27. # can only enable/disable access to an entire repository.) Note that
28. # mod_authz_svn is noticeably slower than the other two layers, so if
29. # you don't need the fine-grained control, don't configure it.
30. # Basic Authentication is repository-wide. It is not secure unless
31. # you are using https. See the 'htpasswd' command to create and
32. # manage the password file - and the documentation for the
33. # 'auth_basic' and 'authn_file' modules, which you will need for this
34. # (enable them with 'a2enmod').
35. AuthType Basic
36. "Subversion Repository"
37. AuthUserFile /etc/apache2/dav_svn.passwd
38. # To enable authorization via mod_authz_svn
39. #AuthzSVNAccessFile /etc/apache2/dav_svn.authz
40. # The following three lines allow anonymous read, but make
41. # committers authenticate themselves. It requires the 'authz_user'
42. # module (enable it with 'a2enmod').
43. #<LimitExcept GET PROPFIND OPTIONS REPORT>
44. Require valid-user
45. #</LimitExcept>
46. </Location>


location说的是访问地址,比如上述地址,访问的时候就是

​http://192.168.1.104/tang​

其中有两行被注释掉了,以保证每次都需要用户名密码。
最后一步就是创建访问用户了,建议将用户名密码文件存放在当前版本仓库下conf文件夹下,这样版本仓库多的时候无至于太乱。
因为conf文件夹下已经存在passwd文件了,所以直接添加用户:

sudo htpasswd -c /home/svn/project/conf/passwd tang

然后输入两遍密码,tang 这个用户就创建好了。

打开 /etc/apache2/dav_svn.passwd   这个文件,会开到形如如下形式的文本:

tang:WEd.83H.gealA  //后面是加密后的密码。

创建以后,再次需要往别的版本仓库添加这个用户,直接把这一行复制过去就可以了。
重启apache就可以了。

sudo /etc/init.d/apache2 restart

开机自动启动svn服务

在init.d目录建立一个脚本文件svnd.sh
# cd /etc/init.d
# vim svnd.sh
输入svnd.sh内容如下(/kaifa/svn 为svn仓库目录):

#!/bin/bash
#svnserve startup
svnserve -d -r /kaifa/svn

保存退出。
更新,修改权限:
# update-rc.d svnd.sh defaults
# chmod 777 svnd.sh

三、同步更新 [勾子]

同步程序思路:用户提交程序到SVN,SVN触发hooks,按不同的hooks进行处理,这里用到的是post-commit,利用post-commit到代码检出到SVN服务器的本地硬盘目录,再通过rsync同步到远程的WEB服务器上。

知识点:

1、SVN的hooks
# start-commit 提交前触发事务
# pre-commit 提交完成前触发事务
# post-commit 提交完成时触发事务
# pre-revprop-change 版本属性修改前触发事务
# post-revprop-change 版本属性修改后触发事务
通过上面这些名称编写的脚本就就可以实现多种功能了,相当强大。
2、同步命令rsync的具体参数使用
3、具有基个语言的编程能力bash python perl都可以实现

post-commit具体实现细节
post-commit脚本

编辑文件:sudo vim /home/svn/fitness/hooks/post-commit

注意:编辑完成post-commit后,执行:sudo chmod 755 post-commit

内容:


1. #!/bin/sh 
2. export LANG=zh_CN.UTF-8
3. sudo /usr/bin/svn update /var/www/www --username mirze --password 123456
4.
5. 或
6. #Set variable
7. SVN=/usr/bin/svn
8. WEB=/home/test_nokia/
9. RSYNC=/usr/bin/rsync
10. LOG=/tmp/rsync_test_nokia.log
11. WEBIP="192.168.0.23"
12. export LANG=en_US.UTF-8
13.
14. #update the code from the SVN
15. $SVN update $WEB --username user --password password
16. #If the previous command completed successfully, to continue the following
17. if [ $? == 0
18. then
19. echo "" >> $LOG
20. echo `date` >> $LOG
21. "##############################"
22. chown -R nobody:nobody /home/test_nokia/
23. #Synchronization code from the SVN server to the WEB server, notes:by the key
24. 90 --exclude-from=/home/svn/exclude.list $WEB root@$WEBIP:/www/ >> $LOG
25. fi


以上是具体的post-commit程序

注意事项:


1、一定要定义变量,主要是用过的命令的路径。因为SVN的考虑的安全问题,没有调用系统变量,如果手动执行是没有问题,但SVN自动执行就会无法执行了。


2、SVN update 之前一定要先手动checkout一份出来,还有这里一定要添加用户和密码如果只是手动一样会更新,但自动一样的不行。


3、加上了对前一个命令的判断,如果update的时候出了问题,程序没有退出的话还会继续同步代码到WEB服务器上,这样会造成代码有问题


4、记得要设置所属用户,因为rsync可以同步文件属性,而且我们的WEB服务器一般都不是root用户,用户不正确会造成WEB程序无法正常工作。


5、建议最好记录日志,出错的时候可以很快的排错


6、最后最关键的数据同步,rsync的相关参数一定要清楚,这个就不说了。注意几个场景:


这里的环境是SVN服务器与WEB服务器是开的


把SVN服务器定义为源服务器 WEB服务器为目的服务器


场景一、如果目的WEB服务器为综合的混杂的,像只有一个WEB静态资源,用户提交的,自动生成的都在WEB的一个目录下,建议不要用–delete这个参数


上面这个程序就是这样,实现的是源服务器到目的服务器的更新和添加,而没有删除操作,WEB服务器的内容会多于源SVN的服务器的


场景二、实现镜像,即目的WEB服务器与源SVN服务器一样的数据,SVN上任何变化WEB上一样的变化,就需要–delete参数


场景三、不需要同步某些子目录,可能有些目录是缓存的临时垃圾目录,或者是专用的图片目录(而不是样式或者排版的)要用exclude这个参数


注意:这个参数的使用不用写绝对路径,只要目录名称就行 aa代表文件 aa/ 代表目录 ,缺点就是如果有多个子目录都是一样的名称那么这些名称就都不会被同步


建议用–exclude-from=/home/svn/exclude.list 用文件的形式可以方便的添加和删除


exclude.list

  1. .svn/   
  2. .DS_Store   
  3. images/   


利用SVN的钩子还可以写出很多的程序来控制SVN 如代码提交前查看是否有写日志,是否有tab,有将换成空格,是否有不允许上传的文件,是否有超过限制大小的文件等等。