ubuntu下配置git 幾天來一直在折騰git版本管理伺服器,感覺有點頭大,一會兒許可權不夠,一會兒加密檔案與使用者名稱不匹配……但功夫不負有心人今天下午總是有所成效,把這些天來我所遇到和所範錯誤作一個記錄,給大家一個參考!今天重新把日誌修改了一下,主要解決了gitweb顯示問題,與新加入的成員無法clone git倉庫的問題! www.2cto.com 1 需求硬體需求:一台Ubuntu或者debian電腦(虛擬機器),能通過網路訪問到。軟體需求:git-core, gitosis, openssh-server, openssh-client2 安裝配置git伺服器安裝git和openssh:a@server:~$ sudo apt-get install git-core openssh-server openssh-client新加使用者git, 該使用者將作為所有代碼倉庫和使用者權限的管理者:a@server:~$ sudo useradd -m gita@server:~$ sudo passwd git
建立一個git倉庫的儲存點:a@server:~$ sudo mkdir /home/repo讓除了git以外的使用者對此目錄無任何許可權:a@server:~$ sudo chown git:git /home/repoa@server:~$ sudo chmod 755 /home/repo 註:此處要把git倉庫repo的屬性設定成755,不然在gitweb中一直無法找到工程
3 安裝配置gitosis初始化一下伺服器的git使用者,這一步其實是為了安裝gitosis做準備。在任何一 台機器上使用git,第一次必須要初始化一下:a@server:~$ git config –global user.name “myname”a@server:~$ git config –global user.email “myname@server“安裝一下python的setup tool, 這個也是為了gitosis做準備:a@server:~$ sudo apt-get install python-setuptools獲得gitosis包: www.2cto.com a@server:~$ cd /tmpa@server:/tmp$ git clone git://eagain.net/gitosis.gita@server:/tmp$ cd gitosisa@server:/tmp/gitosis$ sudo python setup.py install
切換到git使用者下:a@server:/tmp/gitosis$ su git預設狀態下,gitosis會將git倉庫放在 git使用者的home下,所以我們做一個連結到/home/repo$ ln -s /home/repo /home/git/repositories再次返回到預設使用者$ exit如果你將作為git伺服器的管理員,那麼在你的電 腦上(另一台pc)產生ssh公開金鑰:usr@pc1:~$ ssh-keygen -t rsa將公開金鑰拷貝到伺服器的/tmp下:usr@pc1:~$ scp .ssh/id_rsa.pub git@<server>:/tmp回到git伺服器上a@server:/tmp/gitosis$ sudo chmod a+r /tmp/id_rsa.pub讓gitosis運行起來:a@server:/tmp/gitosis$ sudo -H -u git gitosis-init < /tmp/id_rsa.pubInitialized empty Git repository in /home/repo/gitosis-admin.git/Reinitialized existing Git repository in /home/repo/gitosis-admin.git/gitosis的有趣之處在於,它通過一個git倉庫來管理設定檔,倉庫就放在了/home/repo/gitosis- admin.git。我們需要為一個檔案加上可執行許可權:a@server:/home/git$ sudo passwd roota@server:/home/git$ suroot@server:/home/git# cd repositoriesroot@server:/home/git/repositories# cd gitosis-admin.git/root@server:/home/git/repositories/gitosis-admin.git# sudo chmod 755 /home/repo/gitosis-admin.git/hooks/post-updateroot@server:/home/git/repositories/gitosis-admin.git# exit
4 在伺服器上建立一個測試專案倉庫我們在伺服器上建立一個空的項目倉庫,叫“teamwork”。切換到git使用者: www.2cto.com a@server:/home/git$ su – git$ cd /home/repo$ mkdir teamwork.git$ cd teamwork.git$ git init - -bare 註:這是在伺服器上啟動並執行,是為了初始化一個根級的git倉庫$ exit但是,到目前為止,這隻是一個空倉庫,空倉庫是不能clone下來的。為了能做clone,我們必須先讓某個有許可權的人放一個初始化的版本到倉庫中。所以,我們必須先修改一下gitosis-admin.5 管理gitosis的設定檔剛剛提到,gitosis本身的配置也是通過git來實現的。在你自己的開發機裡,把gitosis-admin.git這個倉庫clone下來,就可以以管理員的身份修改配置了。在你的電腦裡:usr@pc1:~/work$ git clone git@<server>:gitosis-admin.gitusr@pc1:~/work$ cd gitosis-admin/該目錄下的keydir目錄是用來存放所有需要訪問git伺服器的使用者的ssh公開金鑰:各個使用者按照前面提到的辦法產生各自的ssh公開金鑰檔案後,把所有人的 ssh公開金鑰檔案都拿來,按名字命名一下,比如b.pub, lz.pub等,統統拷貝到keydir下:usr@pc1:~/work/gitosis-admin$ su rootroot@pc1:/home/a/work/gitosis-admin# cp /path/to/.ssh/id_rsa.pub ./keydir/b.pubroot@pc1:/home/a/work/gitosis-admin# exit修改gitosis.conf檔案,我的配置大致如下:[gitosis][group gitosis-admin]writable = gitosis-adminmembers = a@server usr@pc1[group hello]writable = teamworkmembers = a@server b[group hello_ro]readonly = teamworkmembers = lz這個設定檔表達了如下含義:gitosis-admin群組成員有a, usr,該組對gitosis-admin倉庫有讀寫權限; www.2cto.com team組有a,b兩個成員,該組對teamwork倉庫有讀寫權限;team_ro組有lz一個成員,對teamwork倉庫有唯讀許可權。當然目前這些設定檔的修改只是在你的本地,你必須推送到遠端gitserver上才能真正生效。加入新檔案、提交並push到git伺服器:usr@pc1:~/work/gitosis-admin$ git add .usr@pc1:~/work/gitosis-admin$ git commit -am “add teamweok prj and users”usr@pc1:~/work/gitosis-admin$ git push origin master
6 初始化測試專案好了,現在伺服器就搭建完了,並且有一個空的項目teamwork在伺服器上。接下來呢?當然是測試一下,空倉庫是不能clone的,所以需要某一個有寫入權限的人初始 化一個版本。就我來做吧,以下是在用戶端完成。usr@pc1:~/work$ mkdir teamwork-oriusr@pc1:~/work$ cd teamwork-ori/usr@pc1:~/work/teamwork-ori$ git init 註:這是在使用者端的PC上執行的,為的是初始化一個本地的版本庫usr@pc1:~/work/teamwork-ori$ echo “/*add something*/” > hellousr@pc1:~/work/teamwork-ori$ git add .usr@pc1:~/work/teamwork-ori$ git commit -am “initial version”usr@pc1:~/work/teamwork-ori$ git remote add origin git@<server>:teamwork.gitusr@pc1:~/work/teamwork-ori$ git push origin master到此為止teamwork已經有了一個版本了,team的其他成員只要先clone一下 teamwork倉庫,就可以任意玩了。usr@pc1:~/work/teamwork-ori$ su b$ cd /home/b$ git clone git@<server>:teamwork.git$ cd teamwork www.2cto.com $ vim hello$ git add .$ git commit -am “b add”$ git push origin master$ exit
7 添加已有git項目另外:如果你有一個現成的git倉庫,想放到 gitserver上供team使用(比如你clone了一個官方的kernel倉庫,想在內部使用它作為基礎倉庫),怎麼辦呢。首先需要從你的工作倉庫中得到一個純倉庫, 比如你的工作目錄為~/kernel, 你想匯出純倉庫到你的優盤裡,然後拷貝到gitserver上去。$ git clone –bare ~/kernel /media/udisk然後就拿著優盤,交給gitserver的管理員,讓他拷貝到/home/repo/下,同時需要配置 gitosis相關設定檔哦,這個就不用再說了吧。比如:下載ALSA庫:git clone git://android.git.kernel.org/platform/external/alsa-lib.gitgit clone git://android.git.kernel.org/platform/external/alsa-utils.git產生bare庫git clone –bare alsa-lib alsa-lib.gitgit clone –bare alsa-utils alsa-utils.git將bare 庫移動到git伺服器目錄cp alsa-lib.git /home/repo注意變更所有者,以擷取提交許可權。chown -R git alsa-lib.git然後就O 了,呵呵.
8 建立gitwebsudo apt-get install gitweb8.1 配置 gitweb預設沒有 css 載入,把 gitweb 要用的靜態檔案串連到 DocumentRoot 下:cd /var/www/sudo ln -s /usr/share/gitweb/* .8.2 修改配置sudo vi /etc/gitweb.conf將 $projectroot 改為git倉庫儲存目錄(例如:/home/git/repositories),儲存後重新整理瀏覽器。如果沒有找到項目,你需要將$projectroot/*.git 的屬性改為755,讓apache使用者有可讀許可權。可以只改你需要讓別人通過web訪問的那個git。http://localhost/cgi-bin/gitweb.cgi8.3 修改/etc/gitweb.conf 內容# path to git projects (<project>.git)#$projectroot = "/var/cache/git";$projectroot = "/home/git/repositories";# directory to use for temp files$git_temp = "/tmp"; www.2cto.com # target of the home link on top of all pages$home_link = $my_uri || "/";# html text to include at home page$home_text = "indextext.html";# file with project list; by default, simply scan the projectroot dir.$projects_list = $projectroot;# stylesheet to use@stylesheets = ("/gitweb/static/gitweb.css");# javascript code for gitweb$javascript = "gitweb/static/gitweb.js";# logo to use$logo = "/gitweb/static/git-logo.png";# the 'favicon'$favicon = "/gitweb/static/git-favicon.png";# git-diff-tree(1) options to use for generated patches#@diff_opts = ("-M");@diff_opts = ();
8.4 配置apache2ubuntu中預設的web目錄是/var/www,預設的cgi目錄是 /usr/lib/cgi-bin/,安裝完成gitweb後,gitweb的gitweb.cgi會自動放置到該目錄下。如果你的cgi路徑不是預設的/usr/lib/cgi-bin/,需要將gitweb安裝在/usr/lib/cgi-bin中的gitweb.cgi複製到原來配置的cgi-bin路徑,並在apache的設定檔/etc/apache2/apache.conf末尾加上以下內容:SetEnv GITWEB_CONFIG /etc/gitweb.conf<Directory "/srv/www/cgi-bin/gitweb"> Options FollowSymlinks ExecCGI Allow from all AllowOverride all Order allow,deny <Files gitweb.cgi> SetHandler cgi-script </Files> www.2cto.com RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^.* /gitweb.cgi/$0 [L,PT]</Directory>
8.5 重新啟動apachesudo /etc/init.d/apache2 restart訪問http://localhost/cgi-bin/gitweb.cgi 說明一點:如果其它新加入的成員無法clone一個git倉庫時,如出現如下錯誤提示:ERROR:gitosis.serve.main:Repository read access denied 原因是當前成員的key命名與在gitosis.conf中的members中的名字不一致!例如所用的key名是 lxq_rsa.pub,而在gitosis.conf中某一group下所用的名稱為 lxq@ubuntu則只需要把lxq_rsa.pub重新命名為lxq@ubuntu.pub並重新push到伺服器上即可! 作者 GUMINCONG