关闭

Centos7上搭建SVN服务器并实现自动同步至web目录

947人阅读 评论(1) 收藏 举报

前言

由于最近跟学长一起合作完成一个小项目,然后我俩比较熟悉的版本控制是SVN,就考虑着在服务器上搭建一个SVN服务器。现在在这里给出简单的搭建过程。 
(其实吧,能用Git就尽量用git吧,好处自己百度)

预期目的:

1、仓库放在 /var/svn/ 目录下,并且仓库名为 project

2、创建用户组lsgogroup,该组下添加两个成员lsgoweb1、lsgoweb2,密码直接用用户名,两用户可以checkout代码和提交代码

3、利用SVN的钩子实现当仓库的代码更新的时候自动同步至我们的web目录,在这里,我的web目录在 /home/www/ 下

一、搭建svn环境并创建仓库:

1、安装Subversion:

<code class="hljs cmake has-numbering" style="padding: 0px; margin: 0px; display: block; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">yum <span class="hljs-keyword" style="padding: 0px; margin: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">install</span> -y subversion</code>

2、检测是否安装成功(查看svn版本号)

<code class="hljs brainfuck has-numbering" style="padding: 0px; margin: 0px; display: block; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-color: transparent; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-comment" style="padding: 0px; margin: 0px; color: rgb(136, 0, 0); box-sizing: border-box;">svnserve</span> <span class="hljs-literal" style="padding: 0px; margin: 0px; color: rgb(0, 102, 102); box-sizing: border-box;">-</span><span class="hljs-literal" style="padding: 0px; margin: 0px; color: rgb(0, 102, 102); box-sizing: border-box;">-</span><span class="hljs-comment" style="padding: 0px; margin: 0px; color: rgb(136, 0, 0); box-sizing: border-box;">version</span></code>

3、创建版本库

<code class="hljs oxygene has-numbering" style="padding: 0px; margin: 0px; display: block; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-color: transparent; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-comment" style="padding: 0px; margin: 0px; color: rgb(136, 0, 0); box-sizing: border-box;">//先建目录</span>
mkdir /<span class="hljs-keyword" style="padding: 0px; margin: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">var</span>/svn
cd /<span class="hljs-keyword" style="padding: 0px; margin: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">var</span>/svn
<span class="hljs-comment" style="padding: 0px; margin: 0px; color: rgb(136, 0, 0); box-sizing: border-box;">//创建版本库</span>
svnadmin <span class="hljs-keyword" style="padding: 0px; margin: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">create</span> /<span class="hljs-keyword" style="padding: 0px; margin: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">var</span>/svn/project
cd project
<span class="hljs-comment" style="padding: 0px; margin: 0px; color: rgb(136, 0, 0); box-sizing: border-box;">//会看到自动生成的版本库文件</span>
conf  db  format  hooks  locks  README.txt</code>

至此,svn环境搭建成功。

二、创建用户组及用户:

1、 进入版本库中的配置目录conf,此目录有三个文件: svn服务综合配置文件(svnserve.conf)、 用户名口令文件(passwd)、权限配置文件(authz)。

2、修改权限配置文件:vim authz

3、配置用户名命令文件:vim passwd

4、配置SVN服务综合配置文件svnserve.conf

<code class="hljs lasso has-numbering" style="padding: 0px; margin: 0px; display: block; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-color: transparent; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-comment" style="padding: 0px; margin: 0px; color: rgb(136, 0, 0); box-sizing: border-box;">//找到以下配置项,将前面的#号去掉,然后做相应的配置</span>
anon<span class="hljs-attribute" style="padding: 0px; margin: 0px; box-sizing: border-box;">-access</span> <span class="hljs-subst" style="padding: 0px; margin: 0px; color: rgb(0, 0, 0); box-sizing: border-box;">=</span> <span class="hljs-literal" style="padding: 0px; margin: 0px; color: rgb(0, 102, 102); box-sizing: border-box;">none</span>  <span class="hljs-comment" style="padding: 0px; margin: 0px; color: rgb(136, 0, 0); box-sizing: border-box;">//匿名用户访问权限:无</span>
auth<span class="hljs-attribute" style="padding: 0px; margin: 0px; box-sizing: border-box;">-access</span> <span class="hljs-subst" style="padding: 0px; margin: 0px; color: rgb(0, 0, 0); box-sizing: border-box;">=</span> write     <span class="hljs-comment" style="padding: 0px; margin: 0px; color: rgb(136, 0, 0); box-sizing: border-box;">//普通用户访问权限:读、写</span>
password<span class="hljs-attribute" style="padding: 0px; margin: 0px; box-sizing: border-box;">-db</span> <span class="hljs-subst" style="padding: 0px; margin: 0px; color: rgb(0, 0, 0); box-sizing: border-box;">=</span> passwd        <span class="hljs-comment" style="padding: 0px; margin: 0px; color: rgb(136, 0, 0); box-sizing: border-box;">//密码文件</span>
authz<span class="hljs-attribute" style="padding: 0px; margin: 0px; box-sizing: border-box;">-db</span> <span class="hljs-subst" style="padding: 0px; margin: 0px; color: rgb(0, 0, 0); box-sizing: border-box;">=</span> authz        <span class="hljs-comment" style="padding: 0px; margin: 0px; color: rgb(136, 0, 0); box-sizing: border-box;">//权限配置文件</span>
realm <span class="hljs-subst" style="padding: 0px; margin: 0px; color: rgb(0, 0, 0); box-sizing: border-box;">=</span> /<span class="hljs-built_in" style="padding: 0px; margin: 0px; color: rgb(102, 0, 102); box-sizing: border-box;">var</span>/svn/project    <span class="hljs-comment" style="padding: 0px; margin: 0px; color: rgb(136, 0, 0); box-sizing: border-box;">//版本库所在</span><span style="padding: 0px; margin: 0px; text-align: right; color: rgb(34, 34, 34); font-family: Arial, Verdana, sans-serif; font-size: 12px; background-color: rgb(238, 238, 238);">1</span></code>

注意:所有以上的配置项都需要顶格,即前面不能预留空格,否则报错

5、启动svn服务:

<code class="hljs lasso has-numbering" style="padding: 0px; margin: 0px; display: block; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">svnserve <span class="hljs-attribute" style="padding: 0px; margin: 0px; box-sizing: border-box;">-d</span> <span class="hljs-attribute" style="padding: 0px; margin: 0px; box-sizing: border-box;">-r</span> /<span class="hljs-built_in" style="padding: 0px; margin: 0px; color: rgb(102, 0, 102); box-sizing: border-box;">var</span>/svn</code>

如果提示:

<code class="hljs vhdl has-numbering" style="padding: 0px; margin: 0px; display: block; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">svnserve: E000098: Can<span class="hljs-attribute" style="padding: 0px; margin: 0px; box-sizing: border-box;">'t</span> bind server socket: Address already <span class="hljs-keyword" style="padding: 0px; margin: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">in</span> <span class="hljs-keyword" style="padding: 0px; margin: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">use</span></code>

证明现在svn已经被启动了,由于我们修改了配置文件,因此要重启svn服务

<code class="hljs 1c has-numbering" style="padding: 0px; margin: 0px; display: block; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-color: transparent; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-comment" style="padding: 0px; margin: 0px; color: rgb(136, 0, 0); box-sizing: border-box;">//查看svn服务详情</span>
ps aux <span class="hljs-string" style="padding: 0px; margin: 0px; color: rgb(0, 136, 0); box-sizing: border-box;">| grep svn</span>
<span class="hljs-comment" style="padding: 0px; margin: 0px; color: rgb(136, 0, 0); box-sizing: border-box;">//将svn服务强制停止  其中790为svn服务的ID号,-9是kill的参数</span>
kill -<span class="hljs-number" style="padding: 0px; margin: 0px; color: rgb(0, 102, 102); box-sizing: border-box;">9</span> <span class="hljs-number" style="padding: 0px; margin: 0px; color: rgb(0, 102, 102); box-sizing: border-box;">790</span></code>

或者用

<code class="hljs  has-numbering" style="padding: 0px; margin: 0px; display: block; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">killall svnserve</code>

再运行 svnserve -d -r /var/svn,进行启动服务

6、测试服务器:

<code class="hljs cs has-numbering" style="padding: 0px; margin: 0px; display: block; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-color: transparent; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-comment" style="padding: 0px; margin: 0px; color: rgb(136, 0, 0); box-sizing: border-box;">//我们在web目录测试(/home/www)</span>
cd /home/www
svn co svn:<span class="hljs-comment" style="padding: 0px; margin: 0px; color: rgb(136, 0, 0); box-sizing: border-box;">//localhost/project</span></code>

如果提示:Checked out revision 1.  表示checkout成功

我们添加新文件来测试是否提交成功

<code class="hljs avrasm has-numbering" style="padding: 0px; margin: 0px; display: block; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">cd /home/www
touch index<span class="hljs-preprocessor" style="padding: 0px; margin: 0px; color: rgb(68, 68, 68); box-sizing: border-box;">.php</span>
svn <span class="hljs-keyword" style="padding: 0px; margin: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">add</span> index<span class="hljs-preprocessor" style="padding: 0px; margin: 0px; color: rgb(68, 68, 68); box-sizing: border-box;">.php</span>   //成功的话会显示  A index<span class="hljs-preprocessor" style="padding: 0px; margin: 0px; color: rgb(68, 68, 68); box-sizing: border-box;">.php</span>
svn commit index<span class="hljs-preprocessor" style="padding: 0px; margin: 0px; color: rgb(68, 68, 68); box-sizing: border-box;">.php</span> -m <span class="hljs-string" style="padding: 0px; margin: 0px; color: rgb(0, 136, 0); box-sizing: border-box;">"测试提交文件"</span></code>

假如最后提示:

<code class="hljs haskell has-numbering" style="padding: 0px; margin: 0px; display: block; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-color: transparent; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-type" style="padding: 0px; margin: 0px; box-sizing: border-box; color: rgb(102, 0, 102);">Adding</span>         index.php
<span class="hljs-type" style="padding: 0px; margin: 0px; box-sizing: border-box; color: rgb(102, 0, 102);">Transmitting</span> file <span class="hljs-typedef" style="padding: 0px; margin: 0px; box-sizing: border-box;"><span class="hljs-keyword" style="padding: 0px; margin: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">data</span> .</span>
<span class="hljs-type" style="padding: 0px; margin: 0px; box-sizing: border-box; color: rgb(102, 0, 102);">Committed</span> revision <span class="hljs-number" style="padding: 0px; margin: 0px; color: rgb(0, 102, 102); box-sizing: border-box;">2.</span></code>

则表示我们svn服务器搭建成功!文件已经能够推送了。

7、本地拉取、推送

在本地首先要安装SVN吧,具体百度。。。

填写信息:

点击ok,提示输入用户名和密码

就填刚才我们创建的lsgoweb1或lsgoweb2,和对应的密码即可。

三、实现svn更新自动同步到web目录:

1、在web目录中checkout版本库

这一步算是比较关键的一步了,当时我搭建的时候试了无数次,就是缺少了这一步。把刚才checkout的版本库删掉,我们来一次比较正式的checkout:

<code class="hljs brainfuck has-numbering" style="padding: 0px; margin: 0px; display: block; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-color: transparent; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-comment" style="padding: 0px; margin: 0px; color: rgb(136, 0, 0); box-sizing: border-box;">svn</span> <span class="hljs-comment" style="padding: 0px; margin: 0px; color: rgb(136, 0, 0); box-sizing: border-box;">co</span> <span class="hljs-comment" style="padding: 0px; margin: 0px; color: rgb(136, 0, 0); box-sizing: border-box;">svn://localhost/project</span> <span class="hljs-comment" style="padding: 0px; margin: 0px; color: rgb(136, 0, 0); box-sizing: border-box;">/home/www</span> <span class="hljs-literal" style="padding: 0px; margin: 0px; color: rgb(0, 102, 102); box-sizing: border-box;">-</span><span class="hljs-literal" style="padding: 0px; margin: 0px; color: rgb(0, 102, 102); box-sizing: border-box;">-</span><span class="hljs-comment" style="padding: 0px; margin: 0px; color: rgb(136, 0, 0); box-sizing: border-box;">username</span> <span class="hljs-comment" style="padding: 0px; margin: 0px; color: rgb(136, 0, 0); box-sizing: border-box;">lsgoweb1(SVN账号)</span> <span class="hljs-literal" style="padding: 0px; margin: 0px; color: rgb(0, 102, 102); box-sizing: border-box;">-</span><span class="hljs-literal" style="padding: 0px; margin: 0px; color: rgb(0, 102, 102); box-sizing: border-box;">-</span><span class="hljs-comment" style="padding: 0px; margin: 0px; color: rgb(136, 0, 0); box-sizing: border-box;">lsgoweb1(SVN密码)</span></code>

进入/var/svn/project/hooks下,建立post-commit文件:

<code class="hljs bash has-numbering" style="padding: 0px; margin: 0px; display: block; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-color: transparent; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-built_in" style="padding: 0px; margin: 0px; color: rgb(102, 0, 102); box-sizing: border-box;">cd</span> /var/svn/project/hooks
vim post-commit

//在该文件里添加如下代码,保存
<span class="hljs-shebang" style="padding: 0px; margin: 0px; color: rgb(0, 102, 102); box-sizing: border-box;">#!/bin/sh</span>
<span class="hljs-comment" style="padding: 0px; margin: 0px; color: rgb(136, 0, 0); box-sizing: border-box;">#设定环境变量,如果没有设定可能会出现update报错</span>
<span class="hljs-comment" style="padding: 0px; margin: 0px; color: rgb(136, 0, 0); box-sizing: border-box;">#设定语言,根据系统语言设置,如果是GBK就设置为 LANG=zh_CN.GBK</span>
<span class="hljs-keyword" style="padding: 0px; margin: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">export</span> LANG=en_US.utf8
SVN_PATH=/usr/bin/svn   //这里不用改
WEB_PATH=/home/www  //web目录,如果你的不同,可以改
//这里的用户随便一个就好
<span class="hljs-variable" style="padding: 0px; margin: 0px; color: rgb(102, 0, 102); box-sizing: border-box;">$SVN_PATH</span> update <span class="hljs-variable" style="padding: 0px; margin: 0px; color: rgb(102, 0, 102); box-sizing: border-box;">$WEB_PATH</span> --username <span class="hljs-string" style="padding: 0px; margin: 0px; color: rgb(0, 136, 0); box-sizing: border-box;">'lsgoweb1'</span> --password <span class="hljs-string" style="padding: 0px; margin: 0px; color: rgb(0, 136, 0); box-sizing: border-box;">'lsgoweb1'</span> --no-auth-cache</code>

保存退出。

修改post-commit用户为www目录用户

<code class="hljs livecodeserver has-numbering" style="padding: 0px; margin: 0px; display: block; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">chown apache:apache <span class="hljs-built_in" style="padding: 0px; margin: 0px; color: rgb(102, 0, 102); box-sizing: border-box;">post</span>-commit    <span class="hljs-comment" style="padding: 0px; margin: 0px; color: rgb(136, 0, 0); box-sizing: border-box;"> //我的 /home/www 的用户组和所有者都是apache</span></code>

给post-commit 执行权限:

<code class="hljs sql has-numbering" style="padding: 0px; margin: 0px; display: block; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">chmod 755 post-<span class="hljs-operator" style="padding: 0px; margin: 0px; box-sizing: border-box;"><span class="hljs-keyword" style="padding: 0px; margin: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">commit</span></span></code>

麻蛋,终于写完了。。。。

本博客主要参考了:  1、centos7.0搭建SVN服务器  2、CentOS创建SVN 服务器,并且设置自动同步到WEB目录  感谢原作者。

1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1437591次
    • 积分:19397
    • 等级:
    • 排名:第463名
    • 原创:792篇
    • 转载:28篇
    • 译文:18篇
    • 评论:335条