標籤:
在《Publishing Mercurial Repositories》這篇文章中介紹了很多種將我們自己的hg程式碼程式庫發布/公開的辦法。其中最輕量型的辦法是使用hg內建的web server發布,只需要在程式碼程式庫目錄下執行命令hg serve就搞定。但是這隻是一個臨時的方案,如果想要更健壯更安全,官網還是建議使用hgweb指令碼+Web server(apache,IIS等)的方式。
查看資料後,我權衡利弊,準備以《Serving Mercurial repositories with Apache and mod_wsgi》為藍本再結合其他教程和實踐,來搭建自己的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
變成如下:
<VirtualHost *:80> ServerName hg.example.net DocumentRoot /var/www/vhosts/hg.example.net/htdocs WSGIScriptAliasMatch ^(.*)$ /var/www/vhosts/hg.example.net/cgi-bin/hgweb.wsgi$1 <Directory /var/www/vhosts/hg.example.net/htdocs> Options FollowSymlinks DirectoryIndex index.html AllowOverride None Order allow,deny Allow from all </Directory> <Directory /var/www/vhosts/hg.example.net/cgi-bin> Options ExecCGI FollowSymlinks AddHandler wsgi-script .wsgi AllowOverride None Order allow,deny Allow from all </Directory></VirtualHost>
第三步,配置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 timefrom mercurial import demandimport; demandimport.enable()from mercurial.hgweb import hgwebapplication = 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
突擊Mercurial SCM(HG)5---Ubuntu下apache+mod_wsgi搭建hg server