用Nginx代理hg仓库

********************
用Nginx代理hg仓库
********************


准备工作
============
#. 安装配置nginx
– Ubuntu下源中有nginx安装方便
– CentOS可以用epel源、第三方源或者编译安装
– 具体细节另论
#. 安装mercurial
– Ubuntu下源中有 sudo apt-get install mercurial
– CentOS下源中也有 yum install mercurial

建立仓库
============

假设仓库位置为/var/repo

#. 初始化
$cd /var/repo
$hg init

#. 配置仓库本地用户
$vim /var/reop/.hg/hgrc

::
[ui]
username = yourname

#. 添加文件
$hg add .
$hg ci
`输入提交信息`

#. 建立hg serve配置文件
$vim /var/repo/hgweb-conf

::
[web]
push_ssl = false
allow_push = *
encoding = “UTF-8″
[paths]
/hg = /var/repo

[hooks]
changegroup = hg update

#配置了一个hook,用于每次客户端push时,都自动update,方便。

#. 启动hg serve

$hg --cwd /var/repo serve -d -a localhost --webdir-conf hgweb-config

– 其中–cwd 为仓库目录,serve为启动hg http服务器,-d为以守护进程启动,-a 为使用域,配置为localhost可以禁止直接访问,–webdir-conf为指定hg serve 配置文件
– 可以将此命令写入/etc/rc.local (或者是/etc/rc.d/rc.local)中,使其开机自动启动

配置nginx反向代理
================

#. 修改nginx主配置,添加如下配置

::
location /hg {
proxy_pass http://localhost:8000;
auth_basic “Restricted”;
auth_basic_user_file htpasswd;
}

– auth_basic_user_file htpasswd; 为指定验证配置文件,用于仓库权限管理。

#. 配置访问验证
– 下载http://trac.edgewall.org/browser/trunk/contrib/htpasswd.py 脚本到/etc/nginx
– 执行 $python htpasswd.py -c -b yourname yourpasswd
– 其中,-c为创建文件,所以再添加用户则不需要-c参数,-b为直接添加用户和密码,不必等待提示再输入。

#. 重起Nginx
– $service nginx restart
– 应该也可以reload, $ /usr/sbin/nginx -s reload

完成
=============
这样就可以通过 http://你机子的地址/hg 来访问仓库了。

另外
============
使用TortoiseHG连接词ci co都需要密码不方便。

可以这样设置。
在TortoiseHG 配置path时,配置default地址为 http://yourname:yourpasswd@你机子的地址/hg 保存且不移除头部信息。

这样就可以不用每次都输入用户名密码了,十分方便。

Leave a Reply