centos 6.5 轻松部署 apache+svnmanager+svn

svnmanager   svnmanager汉化   系统服务  
环境配置

系统环境: CentOS 6.5 X64
仓库路径: /data/svnroot/repos
相关软件: apache+php+mysql+svnmanager+subversion

yum 安装简单省事
cd /etc/yum.repos.d && mv CentOS-Base.repo CentOS-Base.repo.bak && wget http://mirrors.163.com/.help/CentOS6-Base-163.repo  
yum install mysql mysql-server http mod_dav_svn  php php-pear php-mysql subversion php-mbstring -y  
创建仓库
mkdir -p /data/svnroot/repos  
svnadmin create /data/svnroot/repos/test  
chown apache.apache /data/svnroot/ -R  
配置apache (/etc/httpd/conf/httpd.conf)
  • 加载svn模块
LoadModule dav_svn_module modules/mod_dav_svn.so  
LoadModule authz_svn_module modules/mod_authz_svn.so  
  • 增加虚拟主机配置
AddDefaultCharset GB2312  
AddType application/x-httpd-php .php  
DirectoryIndex index.html index.php  
<Location /svn/>  
        DAV svn
        SVNListParentPath on
        SVNParentPath /data/svnroot/repos             #仓库路径
        AuthType Basic
        AuthName "My SVN"
        AuthUserFile /data/svnroot/repos/passwdfile
        AuthzSVNAccessFile /data/svnroot/repos/accessfile
        Require valid-user
</Location>  
<Directory "/var/www/html/svnmanager/">  
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from all
</Directory>  
  • 创建认证文件
touch /data/svnroot/repos/passwdfile  
touch /data/svnroot/repos/accessfile  
chown apache.apache /data/svnroot/repos/ -R  
  • 启动apache并验证svn
service httpd configtest                                     #测试配置文件  
service httpd start                                          #启动  
访问 http://ip/svn/                                          #看是否有提示输入用户名密码
安装配置svnmanager
  • 下载安装
wget http://liquidtelecom.dl.sourceforge.net/project/svnmanager/svnmanager/1.10/svnmanager-1.10.tar.gz  
wget http://softimdst.oss-cn-beijing.aliyuncs.com/svn%2Fsvnmanager_cn.tar.gz  
#汉化
tar zxf svnmanager-1.10.tar.gz && tar zxf svn%2Fsvnmanager_cn.tar.gz  
\cp -fr svnmanager svnmanager-1.10
mv svnmanager-1.10 /var/www/html/  
  • 安装VersionControl_SVN
cd /usr/share/pear/  
rm -fr .channels/  
pear update-channels  
pear install --alldeps VersionControl_SVN-0.5.0  

Starting to download VersionControlSVN-0.5.1.tgz (31,121 bytes)
.........done: 31,121 bytes
install ok: channel://pear.php.net/VersionControl
SVN-0.5.1
出现如上提示,表示安装成功

  • 创建数据库
service mysqld start  
mysql -uroot -p  
mysql> create database svnmanager;  
mysql> grant all on svnmanager.* to svn@'localhost' identified by '123456';  
mysql> flush privileges;  
  • 更改svnmanager的配置文件
cd /var/www/html/svnmanager/  
cp config.php.linux config.php  

vim config.php

<?php  
$lang                                           = "en_US.UTF-8";
$htpassword_cmd                         =       "/usr/bin/htpasswd";
$svn_cmd                                =       "/usr/bin/svn";
$svnadmin_cmd                           =       "/usr/bin/svnadmin";
$svn_config_dir                         =       "/data/svnroot/repos";
$svn_repos_loc                          =       "/data/svnroot/repos";
$svn_passwd_file                        =       "/data/svnroot/repos/passwdfile";
$svn_access_file                        =       "/data/svnroot/repos/accessfile";
$svn_trash_loc                          =       "";
$svnserve_user_file="";
$dsn                                    =       "mysqli://svn:123456@localhost/svnmanager";
$smtp_server                    =       "smtp.imdst.com";
$admin_name                                             =       "admin";
$admin_temp_password                    =       "admin";
?>
  • 测试是否成功
    http://ip/svnmanager
    默认用户密码 admin/admin

  • 如果出现 403 Forbidden,请关闭Selinux

setenforce 0  
  • 第一次登陆看不到其他选项,添加一个新的管理员用户就可以使用了
SVN 提交强制写注释勾子
  • 项目团队开发期间,总是有人忘记写注释
    • 导致其他人员不知道提交的是什么代码,给回滚带来不必要的麻烦
    • 总是需要有人提醒提交时添加注释
  • 进入项目仓库对应目录的hooks
    cd /data/svnroot/repos/project/hooks
  • 修改 pre-commit.tmpl
    mv pre-commit.tmpl pre-commit && chmod +x pre-commit
#!/bin/sh
REPOS="$1"  
TXN="$2"  
RES="OK"  
SVNLOOK=/usr/bin/svnlook  
$SVNLOOK log -t "$TXN" "$REPOS" \
| egrep  "[^[:space:]]+" >/dev/null  || unset RES
if [ "$RES" != "OK" ] ; then  
 echo "欢迎你不写,欢迎你不填,别人吃饭你掏钱" 1>&2
 exit 1
fi  
# All checks passed, so allow the commit.
exit 0  
如果客户端出现Error: [Error output could not be translated from the native locale to UTF-8.] 无法显示中文的情况
  • Subversion 升级到 1.8 有一个新特性 SVNUseUTF8 On
  • 具体升级操作
cat > /etc/yum.repos.d/wandisco-svn.repo <<EOF  
[WandiscoSVN]
name=Wandisco SVN Repo  
baseurl=http://opensource.wandisco.com/centos/6/svn-1.8/RPMS/\$basearch/  
enabled=1  
gpgcheck=0  
EOF  
# 移除旧的版本
yum remove subversion*  
# 清缓存
yum clean all  
yum install subversion  
  • 修改httpd.conf并重启httpd
<IfModule mod_dav_fs.c>  
    # Location of the WebDAV lock database.
    DAVLockDB /var/lib/dav/lockdb
    SVNUseUTF8 On
</IfModule>

/etc/init.d/httpd graceful

解决中文目录问题

  • 选择中文目录授权时会出现如下报错
VersionControl_SVN_Exception: Execution of command failed returning: 1  
svn: E000022: Can't convert string from native encoding to 'UTF-8':  
svn: E000022: file:///data/svn/repositories/developer///doc/?\228?\184?\147?\229?\136?\169  
  • 解决办法,修改/usr/share/pear/VersionControl/SVN/Command.php文件,在run()方法中找到如下代码
if (!$this->passthru) {  
  exec("{$this->prependCmd}$cmd 2>&1", $out, $returnVar);
} else {
  passthru("{$this->prependCmd}$cmd 2>&1", $returnVar);
}
###改为
if (!$this->passthru) {  
 exec("export LC_CTYPE=en_US.UTF-8 && {$this->prependCmd}$cmd 2>&1", $out, $returnVar);
} else {
 passthru("export LC_CTYPE=en_US.UTF-8 && {$this->prependCmd}$cmd 2>&1", $returnVar);
}
  • 修改svnmanger表字段如果是latin1的话
CREATE TABLE `groupprivileges` (  
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `groupid` int(11) NOT NULL DEFAULT '0',
  `repositoryid` int(11) NOT NULL DEFAULT '0',
  `access` tinyint(4) NOT NULL DEFAULT '0',
  `path` varchar(255) CHARACTER SET utf8 NOT NULL DEFAULT '',
  `canlock` tinyint(1) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  UNIQUE KEY `groupid` (`groupid`,`repositoryid`,`path`)
) ENGINE=InnoDB AUTO_INCREMENT=1788 DEFAULT CHARSET=utf8;

解决Warning: Cannot modify header information - headers already sent by问题.

  • output_buffering
vim /etc/php/php.ini  
output_buffering  
   Default Value: Off
   Development Value: 4096
   Production Value: 4096
  • 重启apache

Nginx 反向代理apache

  • apache User和Group要和nginx一致
  • 如果为本机Listen地址改成内网如10.104.235.118:8080
  • mysql.sock和php session目录权限

svnmanager nginx +php

server {  
    listen 443 ssl;
    server_name svn-office.imdst.com;

    access_log  /data/logs/nginx/svnmanager.access.log;
    error_log  /data/logs/nginx/svnmanager.error.log;

    ssl_certificate       cert/imdst.com.pem;
    ssl_certificate_key   cert/imdst.com.key;
    ssl_prefer_server_ciphers       on;
    ssl_session_timeout     5m;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES256-GCM-SHA384:AES128-GCM-SHA256:AES256-SHA256:AES128-SHA256:AES256-SHA:AES128-SHA:DES-CBC3-SHA:HIGH:!aNULL:!eNULL:!EXPORT:!DES:!MD5:!PSK:!RC4:!DH";

    client_max_body_size             1024m;
    root /data/www;
    index index.php;

    location /svn/ {
        access_log  /data/logs/nginx/svn.access.log;
        error_log  /data/logs/nginx/svn.error.log;

        proxy_redirect    off;
        proxy_read_timeout 3600;
        proxy_send_timeout 3600;
        proxy_buffering    on;

        set $fixed_destination $http_destination;
        if ( $http_destination ~* ^https(.*)$ ) {
            set $fixed_destination http$1;
        }
        proxy_set_header Destination $fixed_destination;

        proxy_pass http://127.0.0.1:80/svn/;
    }

   location /admin/ {
    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/dev/shm/php-fpm.sock;
        #fastcgi_pass 127.0.0.1:9001;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param  QUERY_STRING     $query_string;
        include fastcgi_params;
        }

    }
}

httpd+svn

LoadModule dav_svn_module     modules/mod_dav_svn.so  
LoadModule authz_svn_module   modules/mod_authz_svn.so  
TimeOut 3600  
<Location /svn>  
  DAV svn
  SVNParentPath /data/svn
  #SVNAllowBulkUpdates Prefer
  AuthType Basic
  AuthName "Subversion Repository"
  AuthUserFile /data/svn/passwdfile
  AuthzSVNAccessFile /data/svn/accessfile
    Require valid-user
</Location>

<Directory "/data/svn">  
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

ProxyPass /admin/ http://127.0.0.1:81/  
ProxyPassReverse /admin/ http://127.0.0.1:81/  
ProxyPreserveHost On  

SVN备份和还原

SVN常用指令

补充

  • 如果出现 403 Forbidden
  • 检查 selinux /etc/sysconfig/selinux
    SELINUX=disabled
  • 不重启生效
    setenforce 0

  • 出现require_once(VersionControl/SVN.php): failed to open stream: No such file or directory错误

  • 请确定VersionControl_SVN是否安装成功