前言
通过 Apache、Subversion 搭建 SVN 服务器,实现使用 HTTP、SVN 协议访问 SVN 仓库,并进行细粒度的权限控制,本教程适用于 Debian/Ubuntu 系统。
系统环境
1 2 3 4 5 6 7 8 # uname -a # Linux debian 3.10.0-957.1.3.el7.x86_64 # cat /etc/os-releasePRETTY_NAME="Debian GNU/Linux 9 (stretch)" NAME="Debian GNU/Linux" VERSION_ID="9" VERSION="9 (stretch)"
安装软件
1 2 3 4 5 6 7 8 9 10 11 # apt -get update# apt -get install -y apache2 apache2-utils# apt -get install -y subversion subversion-tools libapache2-mod-svn# apt -get install -y subversion subversion-tools libapache2-mod-svn libapache2-svn
验证 Apache 是否安装成功
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 # service apache2 start# netstat -anp |grep 80# ufw status# ufw allow 80/tcp# ufw reloadhttp://127.0.0.1
创建 SVN 仓库,并配置 Apache 的访问权限
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 # mkdir /var/lib/svn# svnadmin create /var/lib/svn/jenkins-repo# chown -R www-data:www-data /var/lib/svn/jenkins-repo# vim /etc/apache2/mods-enabled/dav_svn.conf<Location /svn> DAV svn SVNParentPath /var/lib/svn AuthType Basic AuthName "Subversion Repository" AuthUserFile /etc/apache2/dav_svn.passwd Require valid-user </Location> # htpasswd -cm /etc/apache2/dav_svn.passwd jenkins# chgrp www-data /etc/apache2/dav_svn.passwd# chmod 660 /etc/apache2/dav_svn.passwd# service apache2 restart
mod_dav_svn 模块的其他配置介绍(如果嫌麻烦可忽略)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 # 配置文件路径:/etc/apache2/mods-enabled/dav_svn.conf # 下面配置中的"SVNParentPath /var/lib/svn"表示/var/lib/svn目录下的每个子目录都是一个仓库(即所有仓库的根目录),同时表示<Location>标签内的任何权限配置都被所有仓库共用;<Location>标签内的/svn,指通过HTTP协议访问时的URL路由 <Location /svn> DAV svn SVNParentPath /var/lib/svn AuthType Basic AuthName "Subversion Repository" AuthUserFile /etc/apache2/dav_svn.passwd Require valid-user </Location> # 如果想要指定多个仓库,让每个仓库都拥有不同的访问权限配置,那么可以使用多个Location标签,同时需要将标签内的“SVNParentPath”替换为“SVNPath”,并指向具体仓库的目录 <Location /svn/jenkins-repo> DAV svn SVNPath /var/lib/svn/jenkins-repo AuthType Basic AuthName "Subversion Repository" AuthUserFile /etc/apache2/jenkins_repo.passwd Require valid-user </Location> <Location /svn/python-repo> DAV svn SVNPath /var/lib/svn/python-repo AuthType Basic AuthName "Subversion Repository" AuthUserFile /etc/apache2/python-repo.passwd Require valid-user </Location> # 如果替换上述"Require valid-user"为"Require user tony robert",那么只有tony和robert用户可以访问所有仓库 <Location /svn> DAV svn SVNParentPath /var/lib/svn AuthType Basic AuthName "Subversion Repository" AuthUserFile /etc/apache2/dav_svn.passwd Require user tony robert </Location> # 通过使用<LimitExcept>标签,允许匿名读取所有仓库,而只有认证用户才能对所有仓库进行写操作 <Location /svn> DAV svn SVNParentPath /var/lib/svn AuthType Basic AuthName "Subversion Repository" AuthUserFile /etc/apache2/dav_svn.passwd <LimitExcept GET PROPFIND OPTIONS REPORT> Require valid-user </LimitExcept> </Location>
验证 HTTP SVN 是否可用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 http://127.0.0.1/svn/jenkins-repo/ $ vim ~/.subversion/servers[global] store-plaintext-passwords = yes $ svn checkout --username jenkins http://127.0.0.1/svn/jenkins-repo/$ cd jenkins-repo$ touch api.version$ svn add api.version$ svn commit -m "add file" api.version$ svn status$ svn --help $ svn commit --help
通过 mod_authz_svn 模块对仓库目录的访问进行细粒度权限控制
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 # cd /var/lib/svn/jenkins-repo# tree confconf |-- authz |-- hooks-env.tmpl |-- passwd `-- svnserve.conf # vim conf/passwd[users] jenkins = 123456 # vim conf/authz[groups] team = jenkins [/] @team = rw # vim conf/svnserve.conf[general] anon-access = none auth-access = write password-db = passwd authz-db = authz # vim /etc/apache2/mods-enabled/dav_svn.conf<Location /svn/jenkins-repo> DAV svn SVNPath /var/lib/svn/jenkins-repo AuthType Basic AuthName "Subversion Repository" AuthUserFile /etc/apache2/jenkins_repo.passwd AuthzSVNAccessFile /var/lib/svn/jenkins-repo/conf/authz Require valid-user </Location> <Location /svn> DAV svn SVNParentPath /var/lib/svn AuthType Basic AuthName "Subversion Repository" AuthUserFile /etc/apache2/dav_svn.passwd AuthzSVNAccessFile /etc/svn/svn_authz Require valid-user </Location> # vim /etc/svn/svn_authz[groups] project1_committers = paulex richard project2_committers = jimmy michel [repos1:/] @ project1_committer = rw [repos2:/] @ project2_committer = rw # service apache2 restart
启动 Subversion 服务
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 # svnserve -d -r /var/lib/svn# svn checkout --username jenkins svn://127.0.0.1:3690/jenkins-repo# ufw status# ufw allow 3690/tcp# ufw reload
Dockerfile 里配置容器启动时默认启动 Apache 与 Subversion
1 2 3 4 5 6 7 RUN echo "svnserve -d -r /var/lib/svn" > /usr/local /start-servers.sh RUN echo "apachectl -D FOREGROUND" >> /usr/local /start-servers.sh RUN chmod +x /usr/local /start-servers.sh CMD /bin/bash /usr/local /start-servers.sh # docker run -d -p 9126:80 -p 4690:3690 ...
Centos7 安装 Apache&Subversion
本文引用