---
title: "征服 Apache + SVN"
author: "snowolf"
date: "2010-08-18T17:14:07Z"
tags: ["Apache", "SVN", "subversion", "配置管理", "项目管理"]
categories: ["研发管理"]
slug: "apache-svn"
draft: false
lastmod: "2012-05-24T17:49:17Z"
id: 740347
url: "https://snowolf.iteye.com/blog/740347"
---

实在是不知道这个帖子在JE上放到哪个分类里合适，跟SVN有关，那就是项目咯，姑且放到项目管理中吧！ SVN用了很久，不过一直没有机会配置。以前工作的时候都是技术总监、部门经理搞定，自己很少有机会尝试。更别说基于svn方式、http方式，亦或是https方式访问svn了，只有用的份，没有了解的份。最近，配置Apache开窍，索性再深入一步，搭建基于HTTPS平台的Apache+SVN平台。 \[b] 相关内容： \[url=http://snowolf.iteye.com/blog/739475]征服 Apache + SSL\[/url] \[url=http://snowolf.iteye.com/blog/740347]征服 Apache + SVN\[/url] \[url=http://snowolf.iteye.com/blog/892001]征服 Apache + SVN + LDAP\[/url] \[url=http://snowolf.iteye.com/blog/743611]征服 Apache + Tomcat\[/url] \[url=http://snowolf.iteye.com/blog/1497929]征服Nginx\[/url] \[url=http://snowolf.iteye.com/blog/1539932]征服 Nginx + Tomcat\[/url] \[/b] 选用Ubuntu Server 10.04，Apache 2.2.14，Subversion 1.6.6。 步骤： \[list=1] \[\*]安装SVN相关模块 \[\*]配置SVN版本库 \[\*]配置APACHE \[\*]简单测试 \[/list] \[size=large]\[b]1.安装SVN相关模块\[/b]\[/size] 这里主要用到的是Subversion 以及Apache与SVN相关的模块（DAV\_SVN）！ 执行命令，安装： \[code="shell"]sudo apt-get install subversion libapache2-svn\[/code] 注意观察安装后的结果： \[quote]Considering dependency dav for dav\_svn: Enabling module dav. Enabling module dav\_svn. Run '/etc/init.d/apache2 restart' to activate new configuration!\[/quote] 如果看到这样的提示，那说明DAV\_SVN模块已经成功安装，可以重启Apache看看是否正常启动： \[code="shell"]sudo /etc/init.d/apache2 restart\[/code] 或者，使用\[code="shell"]sudo service apache2 restart\[/code] 以我本机为例，正常启动了： \[quote]zlex@localhost:~$ sudo /etc/init.d/apache2 restart * Restarting web server apache2 apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName ... waiting .apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName\[/quote] 当然，稍后你可能需要修改\[b]/etc/apache2/mods-available/dav\_svn.conf\[/b]文件，配置SVN版本库等。 \[size=large]\[b]2.配置SVN版本库\[/b]\[/size] 完成上述操作，只是为Apache获知SVN给出了一种途径，最关键的还是要配置SVN相关部分! 首先，我们要创建subversion组并把www-data作为subversion中的一员。因为，apache是通过www-data账户启动的，我们需要让它能够访问subversion组的文件！ \[code="shell"]sudo addgroup subversion sudo usermod -G subversion -a www-data\[/code] 然后，我们要配置版本库： 我们可以在\[b]/var/lib\[/b]目录下构建一个svn目录，作为SVN版本库根目录： \[code="shell"] cd /var/lib sudo mkdir svn\[/code] 假设我们要创建版本库zlex： \[code="shell"]cd svn sudo svnadmin create zlex\[/code] 更改版本库所属用户、组： \[code="shell"]sudo chown -R root:subversion zlex\[/code] 赋予组成员对所有新加入文件仓库的文件拥有相应的权限： \[code="shell"]sudo chmod -R g+rws zlex\[/code] 试试\[code="shell"]svn co file://localhost/var/lib/svn/zlex\[/code]，这时候应该可以访问了！ \[size=large]\[b]3.配置Apache\[/b]\[/size] 接下来，我们需要修改\[b]/etc/apache2/mods-available/dav\_svn.conf\[/b]文件，配置SVN版本库： \[code="shell"]sudo vi /etc/apache2/mods-available/dav\_svn.conf\[/code] \[img]http://dl.iteye.com/upload/attachment/295388/c9c357a0-35cb-3b68-971a-5704c7fecef0.jpg\[/img] 打开红框中的注释， \[code="conf"] # dav\_svn.conf - Example Subversion/Apache configuration # # For details and further options see the Apache user manual and # the Subversion book. # # NOTE: for a setup with multiple vhosts, you will want to do this # configuration in /etc/apache2/sites-available/\*, not here. # ... # URL controls how the repository appears to the outside world. # In this example clients access the repository as http://hostname/svn/ # Note, a literal /svn should NOT exist in your document root. # Uncomment this to enable the repository DAV svn # Set this to the path to your repository #SVNPath /var/lib/svn # Alternatively, use SVNParentPath if you have multiple repositories under # under a single directory (/var/lib/svn/repo1, /var/lib/svn/repo2, ...). # You need either SVNPath and SVNParentPath, but not both. SVNParentPath /var/lib/svn # Access control is done at 3 levels: (1) Apache authentication, via # any of several methods. A "Basic Auth" section is commented out # below. (2) Apache and , also commented out # below. (3) mod\_authz\_svn is a svn-specific authorization module # which offers fine-grained read/write access control for paths # within a repository. (The first two layers are coarse-grained; you # can only enable/disable access to an entire repository.) Note that # mod\_authz\_svn is noticeably slower than the other two layers, so if # you don't need the fine-grained control, don't configure it. # Basic Authentication is repository-wide. It is not secure unless # you are using https. See the 'htpasswd' command to create and # manage the password file - and the documentation for the # 'auth\_basic' and 'authn\_file' modules, which you will need for this # (enable them with 'a2enmod'). AuthType Basic AuthName "Subversion Repository" AuthUserFile /etc/apache2/dav\_svn.passwd # To enable authorization via mod\_authz\_svn AuthzSVNAccessFile /etc/apache2/dav\_svn.authz # The following three lines allow anonymous read, but make # committers authenticate themselves. It requires the 'authz\_user' # module (enable it with 'a2enmod'). # Require valid-user # \[/code] 分述： \[b]\[/b]与\[b]\[/b]成对儿出现！ \[b]DAV svn\[/b]开启DAV模块支持！ \[b]SVNPath /var/lib/svn\[/b]与\[b]SVNParentPath /var/lib/svn\[/b]选其一，\[b]\[color=red]不可同时出现！\[/color]\[/b]建议使用\[b]SVNParentPath\[/b]，可以在SVN根目录下创建多个SVN版本库！ \[quote] AuthType Basic AuthName "Subversion Repository" AuthUserFile /etc/apache2/dav\_svn.passwd\[/quote]定义了授权类型、并指定了密码文件（\[b]/etc/apache2/dav\_svn.passwd\[/b]）。 \[b]AuthzSVNAccessFile /etc/apache2/dav\_svn.authz\[/b]授权配置文件，规定了路径访问权限！ \[quote]# Require valid-user # \[/quote]\[color=red]建议只使用\[b]Require valid-user\[/b]，打开\[b]注释，将允许匿名访问！\[/color] 现在通过命令设置SVN账户： \[code="shell"]sudo htpasswd -c /etc/apache2/dav\_svn.passwd \[/code] \[color=red]这里用到参数\[b]-c\[/b]，是因为\[b]/etc/apache2/dav\_svn.passwd\[/b]文件不存在，如果文件存在，则无需该参数！否则，将覆盖掉原有密码文件！\[/color] 形如： \[quote]sudo htpasswd -c /etc/apache2/dav\_svn.passwd snowolf New password: Re-type new password: Updating password for user snowolf\[/quote] 可以追加多个账户！ \[quote]sudo htpasswd /etc/apache2/dav\_svn.passwd zlex New password: Re-type new password: Updating password for user zlex\[/quote] 现在，需要设置路径访问权限文件\[b]AuthzSVNAccessFile /etc/apache2/dav\_svn.authz\[/b]。 我们先做一个默认的配置，当前这个文件还不存在： \[code="shell"]sudo vi /etc/apache2/dav\_svn.authz\[/code] 然后追加： \[quote] \[zlex:/] * = r \[/quote] 这样，所有授权用户就都能够看到zlex项目了！ :D 然后访问\[url]http://localhost/svn/zlex\[/url]： \[img]http://dl.iteye.com/upload/attachment/295412/a1f4b826-893d-36f6-a6a0-1754577354e0.jpg\[/img] 试试检出： \[code="shell"]svn co http://localhost/svn/zlex --username snowolf\[/code] 我们通过组方式管理项目，修改\[b]/etc/apache2/dav\_svn.authz \[/b]文件： \[code="shell"]sudo vi /etc/apache2/dav\_svn.authz \[/code] 我们定义一个超级用户组admin，组中成员为snowolf;开发组developer，组中成员为snowolf,zlex，多个用户用逗号分隔。 \[quote] \[groups] admin = snowolf developer = snowolf, zlex \[/quote] 让admin和developer组成员有创建项目版本库的权限，其余用户只有查看权限： \[quote] \[zlex:/] \*=r @admin = rw @developer = rw \[/quote] 给出一个完整配置： \[quote] \[groups] admin = snowolf developer = zlex \[zlex:/] @admin = rw @developer = rw * = \[/quote] 有关Subversion详细配置，参照\[url=http://svnbook.red-bean.com/]Subversion官方中文文档\[/url]！ 修改这个配置文件时，不需要重启apache！ \[size=large]\[b]4.简单测试\[/b]\[/size] 我们之前构建了一个项目仓库——zlex，现在项目有了，我们需要构建相应的版本库管理，及trunk、tags以及branches！ 用命令创建： \[code="shell"]svn mkdir "http://localhost/svn/zlex/branches" "http://localhost/svn/zlex/tags" "http://localhost/svn/zlex/trunk" -m "create a new project zlex" --username "snowolf" \[/code] \[img]http://dl.iteye.com/upload/attachment/295715/b8bedb8d-bec9-3296-aea5-4080f23231ca.jpg\[/img] 这时，我们用zlex账号提交一个docs目录： \[code="shell"]svn mkdir "http://192.168.49.132/svn/zlex/docs" -m "文档目录" --username zlex\[/code] 系统会提示输入密码： \[img]http://dl.iteye.com/upload/attachment/295442/887bef82-7780-35cc-b6d8-d9d8c0e9e87a.jpg\[/img] \[b]192.168.49.132\[/b]是我本机的IP地址！ 我们可以使用命令将项目签出： \[code="shell"]svn checkout "http://localhost/svn/zlex/trunk@HEAD" -r HEAD --depth infinity zlex-svn --username zlex\[/code] 我们随便修改一个文件： \[img]http://dl.iteye.com/upload/attachment/295277/ddef13c9-1040-3342-90c0-bedd76169b8a.jpg\[/img] 提交修改： \[img]http://dl.iteye.com/upload/attachment/295287/dbe7f135-febc-3c60-bd1b-7642bf673f44.jpg\[/img] 如果你参照\[b]\[url=http://snowolf.iteye.com/blog/739475]征服 Apache + SSL\[/url]\[/b]完成了HTTPS平台搭建，这时候，也可以使用HTTPS方式访问了： \[img]http://dl.iteye.com/upload/attachment/295431/87b39e32-cd15-37e8-a559-25ffd8555e1f.jpg\[/img] \[b] 相关内容： \[url=http://snowolf.iteye.com/blog/739475]征服 Apache + SSL\[/url] \[url=http://snowolf.iteye.com/blog/740347]征服 Apache + SVN\[/url] \[url=http://snowolf.iteye.com/blog/892001]征服 Apache + SVN + LDAP\[/url] \[url=http://snowolf.iteye.com/blog/743611]征服 Apache + Tomcat\[/url] \[url=http://snowolf.iteye.com/blog/1497929]征服Nginx\[/url] \[url=http://snowolf.iteye.com/blog/1539932]征服 Nginx + Tomcat\[/url] \[/b]