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 [email protected]
        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.

作者: Tsung

對新奇的事物都很有興趣, 喜歡簡單的東西, 過簡單的生活.

在〈Debian Linux 架設使用 HTTP 存取 的 Git Server〉中有 15 則留言

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

  2. 因為剛接觸git所以想請教一下,我目前是用ubuntu+apache架站,不知道git是不是我所需要的想請教一下疑問??
    因為ubuntu架在筆電上,而我是用我的桌機用dreamweaver去連筆電寫程式的,請問git可以達到桌機上也可以線上測試嗎? 我的意思是說我想要維持以前的作業習慣邊寫邊上傳邊看成果,但看的成果是我的branch而已,在這個環節我不知道如何達到所以請教你了阿~~

    1. 你似乎對 git 的認知不太對, git 是版本控制系統, 跟你要架站寫程式等等, 不會有關係.
      git 只是幫你管理程式, 至於你說的看線上測試結果等等, 那是你的 Ubuntu + Apache 設定, 與 Git 無關喔~ 🙂

  3. 您好~
    我的git使用https傳輸協定
    在 專案repository/objects/ 裡有一堆鬆散資料
    查詢了一下似乎是http這種協定的訪問比較會產生一大堆(主要是update-server-info 造成的) 需常做一些打包動作 否則無法存取
    請問您有這種狀況嗎
    謝謝

Tsung 發表迴響取消回覆

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料