星期一, 五月 11, 2009

Debian Linux 架設使用 HTTP 存取 的 Git Server

此篇的 HTTP 存取方式, 與 SSH 存取方式, 可以使用同一份 Repository.

關於 Git 的說明、ssh:// 存取 Git 等, 可見: 存取 Git 使用 SSH (Debian Linux)

使用 http:// 存取, 並透過 Apache 去管控權限, 可以省掉開機器的帳號. 之前 svn 若也是用 http:// 存取, 亦可直接使用. (不用重新開帳號、密碼)

Git 安裝

  • Server: apt-get install git-core # in Debian / Ubuntu Linux
  • Client: apt-get install git-core curl # in Debian / Ubuntu Linux

相關資料準備

  • Server: git.example.com
  • Project name: project_name
  • Apache 帳號、密碼檔: /etc/apache2/dav_git.passwd
  • Git Repository Path: /var/cache/git (/var/cache/git/prject_name.git)

啟用 Apache2 模組(Server)

需啟用 Apache 的 dav_fs 和 dav 模組, 所以於命令列執行下述命令

  1. a2enmod dav_fs
  2. a2enmod dav

修改 Apache2 設定(Server)

  1. htpasswd -c /etc/apache2/dav_git.passwd USERNAME # 建立新的 帳號 / 密碼
  2. vim /etc/apache2/sites-available/git.conf
    <VirtualHost *>
        ServerAdmin user@example.com
        DocumentRoot /var/cache/git
        ServerName git.example.com
        ErrorLog /var/log/apache2/git-error.log
        CustomLog /var/log/apache2/git-access.log combined

        <Location "/project_name.git">
        DAV on
        AuthType Basic
        AuthName "Git"
        # AuthUserFile /etc/apache2/dav_svn.passwd # 若是跟 svn 相容的話, 檔名一致即可
        AuthUserFile /etc/apache2/dav_git.passwd # 若是想另外取名的話.
        Require valid-user
        </Location>
    </VirtualHost>
  3. ln -s /etc/apache2/sites-available/git.conf /etc/apache2/sites-enabled/git.conf
  4. /etc/init.d/apache2 restart

建立 / 設定 Git Repository (Server)

下述 Git 建立方式, 另一種方式可見: Debian Linux 架設使用 SSH 存取 的 Git Server

  1. mkdir -p /var/cache/git/project_name.git
  2. cd /var/cache/git/project_name.git
  3. git --bare init
  4. git update-server-info
    # 若執行此指令沒作用, 手動作下述初始化也可以.
    #   (不過不建議這樣做, 通常目前只有在 Server 和 Client 是同一台的狀況會有異常現象.)
    #   (Server 和 Client 是同一台的話, 可以考慮用 local commit 即可.)
    # 註: refs/heads/master 中間空白是 tab
    echo "0000000000000000000000000000000000000000" > /var/cache/git/project_name.git/refs/heads/master
    echo "0000000000000000000000000000000000000000    refs/heads/master" > /var/cache/git/project_name.git/info/refs
  5. cp hooks/post-update.sample hooks/post-update
  6. vim hooks/post-update # http 存取需要設這個, 沒有執行此行, 就不能 pull 到新資料
    exec git-update-server-info
    改成 # debian 沒有 git-update-server-info 的指令
    exec git update-server-info
  7. chown www-data:www-data -R .
  8. # 註: 到此 Server 部份就都完成囉 :)

存取 Git 的 帳號 / 密碼 設定(需透過 curl) (Client)

於 Client 端做下述設定 (註: 此設定不可略過)

  1. vim ~/.netrc
    machine example.com
    login USERNAME
    password PASSWORD
  2. chmod 600 ~/.netrc
  3. 測試: curl --netrc --location http://git.example.com/project_name.git/HEAD # 會看到 ref: refs/heads/master
  4. 測試: curl --netrc --location -v http://git.example.com/project_name.git/HEAD # 會出現所有過程訊息

由 Git Repository 取得資料 (Client)

  1. git clone http://git.example.com/project_name.git
  2. cd project_name
  3. touch index.html
  4. git add index.html
  5. git commit -m 'init' # local commit
  6. git push origin master # 若出現 PUT error: curl result=22, HTTP code=403 => 代表 www-data 權限不足
    Fetching remote heads...
      refs/
      refs/heads/
      refs/tags/
    updating 'refs/heads/master'
      from 5b78d8b9cc8677e341cb2d8aab3d87c4fa48570c
      to   64a09faa921dba45ea20d137156589c5fdb10714
        sending 3 objects
        done
    Updating remote server info
  7. git push # 第二次後, 只要 push 即可, 不需加 origin master.

測試

  1. mkdir /tmp/a /tmp/b
  2. cd /tmp/a
  3. git clone http://git.example.com/project_name.git
  4. cd /tmp/b
  5. git clone http://git.example.com/project_name.git
  6. echo "hello" > hello.html
  7. git add hello.html
  8. git commit -m 'add hello.html' # local commit.
  9. git push # 推到 Server 上.
  10. cd /tmp/a
  11. git pull # 會看到 hello.html

相關網頁

快速建立 Repository script

#!/bin/sh
# git-create-reps.sh <project_name>
# 此 script 需由 root 執行

if [ $# -ne 1 ]; then
    echo 1>&2 Usage: $0 project_name
    exit 127
fi
 
set project_name = $1
mkdir -p /var/cache/git/${project_name}.git
cd /var/cache/git/${project_name}.git
git --bare init
git update-server-info
cp hooks/post-update.sample hooks/post-update # 請再自行編修此檔案
chown www-data:www-data -R .

錯誤排除

若於 git push origin master 時, 出現下述錯誤:

error: Cannot access URL http://git.example.com/project_name.git, return code 22
原因
  • WebDAV 不支援
解法
  1. Apache 啟用 dav_fs, dav
  2. 於 Apache 設定檔 啟用 DAV on.

延伸閱讀

相關標籤

this is comment icon [回覆]

http方式访问git效率很低,
莫要用于实用。

Comment by Fwolf (05/16/2009 21:56)

this is comment icon 回 Fwolf [回覆]

那推薦的是?? git:// or ssh:// ?

Comment by Tsung (05/16/2009 22:48)

this is comment icon [回覆]

請問,如果發生"git push origin master # 若出現 PUT error: curl result=22, HTTP code=403 => 代表 www-data 權限不足 "該怎麼處理?

Comment by ww (01/26/2010 17:46)

this is comment icon 回 ww [回覆]

vim ~/.netrc 沒有設定吧?

Comment by Tsung (01/26/2010 18:11)

this is comment icon 回Tsung [回覆]

我有照你的步驟加上~/.netrc
machine,login,password都有設
請問還有什麼可能嗎?
謝謝回答^^

Comment by ww (01/29/2010 09:49)

this is comment icon 回 ww [回覆]

curl --netrc --location -v http://git.example.com/project_name.git/HEAD
這個可以看看中間有出現什麼問題.

Comment by Tsung (01/30/2010 02:13)
Add this page to del.icio.us

發表迴響

標題

內容 (限制 1000 字)

暱稱

電子郵件

個人網頁


 authimage


PS: 若無法留言, 請先確認是否有打開 JavaScript, 造成您的困擾, 實在萬分對不起 Orz...(如果無法留言, 勞煩可以發信給我好嗎? 謝謝.)
PS2: 若您的留言被誤判, 我都會再自行看過, 不需要一直重覆張貼~