在《[Publishing Mercurial Repositories](http://mercurial.selenic.com/wiki/PublishingRepositories)》這篇文章中介紹了很多種將我們自己的hg代碼庫發布/公開的辦法。其中最輕量型的辦法是使用hg自帶的web server發布,只需要在代碼庫目錄下執行命令hg serve就搞定。但是這只是一個臨時的方案,如果想要更健壯更安全,官網還是建議使用hgweb腳本+Web server(apache,IIS等)的方式。
查看資料后,我權衡利弊,準備以《[Serving Mercurial repositories with Apache and mod_wsgi](http://mercurial.selenic.com/wiki/modwsgi)》為藍本再結合其他教程和實踐,來搭建自己的hg server供局域網中的小伙伴使用。
#### 1.工具
mod_wsgi:apache和python溝通的橋梁。
apache2:這個家伙仍然在web server領域引領風騷。
python:2.7版本足以。
mercurial:這是我們的主角。
hgweb.wsgi:mercurial給我們提供的。
linc注:我是這WEB方面的門外漢,對cgi和wsgi等概念是一無所知,之前搭建Scrum環境是在Windows下使用傻瓜似工具,根本沒機會配置apache。因此,接下來的工作令我苦不堪言。有不足之處也在所難免。更請高手指正。
#### 2.安裝
我在Ubuntu下使用apt-get install安裝這些工具,十分順利,閑言少敘。
*sudo apt-get install apache2*
完成后即使不做任何配置,在瀏覽器中輸入localhost,也會出現“It works!”。那么apache就算安裝成功。
*sudo apt-get install libapache2-mod-wsgi*
安裝后在/usr/lib/apache2/modules/目錄下會發現多了mod_wsgi.so
~~~
lrwxrwxrwx?1?root?root?????15?Nov?19?00:50?mod_wsgi.so?->?mod_wsgi.so-2.7??
-rw-r--r--?1?root?root?152064?Nov?19?00:50?mod_wsgi.so-2.7??
~~~
#### 3.關于apache目錄這件小事
1)/var/www
這個目錄是用來放置我們的網站的入口,默認這里會有個index.html,剛才我們打開localhost出現的界面就是這個index.html了。后續我們會在這個目錄下做文章。
2)/etc/apache2
我們要在這里做apache的配置,重點文件有apache2.conf,httpd.conf,以及sites-available下的default。
#### 4.配置
按照教程的例子,我也以虛擬主機 “hg.example.net"來完成整個配置。
**第一步,加載mod_wsgi**
我們的配置(user configurations)在/etc/apache2/httpd.conf中進行,
加載mod_wsgi只需添加LoadModule wsgi_module modules/mod_wsgi.so 即可。
linc注:后來我又將此句注釋掉,發現依然運行正常。
**第二步,配置apache**
依例子的要求,我們要做以下工作。
1)在www下創建新文件夾
/var/www/vhosts/hg.example.net/cgi-bin
/var/www/vhosts/hg.example.net/htdocs
2)修改/etc/apache2/sites-avaiable/default
變成如下:
~~~
??
????ServerName?hg.example.net??
????DocumentRoot?/var/www/vhosts/hg.example.net/htdocs??
??
????WSGIScriptAliasMatch?^(.*)$?/var/www/vhosts/hg.example.net/cgi-bin/hgweb.wsgi$1??
??
??????
????????Options?FollowSymlinks??
????????DirectoryIndex?index.html??
????????AllowOverride?None??
????????Order?allow,deny??
????????Allow?from?all??
??????
??
??????
????????Options?ExecCGI?FollowSymlinks??
??
????????AddHandler?wsgi-script?.wsgi??
????????AllowOverride?None??
????????Order?allow,deny??
????????Allow?from?all??
??????
??
~~~
**第三步,配置Mercurial**
1)將hgweb.wsgi文件復制到/var/www/vhosts/hg.example.net/cgi-bin/
sudo cp /usr/share/doc/mercurial-common/examples/hgweb.wsgi /var/www/vhosts/hg.example.net/cgi-bin/
修改成如下:
~~~
#?Path?to?repo?or?hgweb?config?to?serve?(see?'hg?help?hgweb')??
config?=?"/var/www/vhosts/hg.example.net/cgi-bin/hgweb.config"??
??
#?enable?demandloading?to?reduce?startup?time??
from?mercurial?import?demandimport;?demandimport.enable()??
??
from?mercurial.hgweb?import?hgweb??
application?=?hgweb(config)??
~~~
2)hgweb.config
在/var/www/vhosts/hg.example.net/cgi-bin/新加文件hgweb.config
內如如下:
~~~
[web]??
style?=?coal??
??
[paths]??
/?=?/var/www/vhosts/hg.example.net/htdocs/**??
~~~
重啟apache:
*sodu /etc/init.d/apache2 restart*
到此,《Serving Mercurial repositories with Apache and mod_wsgi》便結束了。
**第四步,創建hg代碼倉庫**
將代碼放在哪里?一直沒有好主意,看到有人將其放在/var下了,姑且我也這樣吧。
新建目錄/var/hg/repos/test,
*mkdir /var/hg/repos/test; cd /var/hg/repos/test; hg init*
一個臨時的代碼倉庫就創建完了,下面如何跟server聯系在一起呢?
回過頭來,我們還要修改一下hgweb.config,加入下面兩句:
~~~
[collections]??
/var/hg?=?/var/hg??
~~~
重啟apache后,在瀏覽器中輸入:localhost/repos/test
你的代碼庫就出現了呢。這樣就說明我們的工作完成了。你可能要問,直接在瀏覽器中輸入hg.example.net不也可以嗎?是這樣的,但是我們還要做一項工作,在/etc/hosts文件中加入127.0.0.1?? hg.example.net 才行.
好吧,先慶祝一下,下文再說代碼提交遇到的問題以及身份認證問題。
參考:
http://mercurial.selenic.com/wiki/PublishingRepositories
http://mercurial.selenic.com/wiki/modwsgi
http://stackoverflow.com/questions/12347373/how-to-setup-mercurial-server-in-ubuntu-to-serve-60-repositories
http://thepanz.netsons.org/post/ubuntu-10-4-and-mercurial-server-apache2-mod_wsgi/comment-page-1
http://blog.sina.com.cn/s/blog_4567bb800100whho.html
http://blog.csdn.net/tony1130/article/details/5326015
http://blog.csdn.net/kongdaoxian/article/details/7944872