Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> 關於Linux

在 CentOS 上打造 Git 私有倉庫:Nginx + Gitweb 完整實戰

開始之前,請確認你已經準備好一台 CentOS,並且 Nginx 已安裝且配置完成。

一、安裝 Git 私服相關套件

適用於 CentOS 6.5 至 CentOS 7.0。先安裝 Nginx,接著執行:

yum install -y git gitweb spawn-fcgi fcgi-devel fcgi

這條命令一次裝齊了四個關鍵角色:

git 服務本身。它只是一個「光板」,僅支援 git clone gituser@hostname:/repositories 這類協議,對 Eclipse 使用者而言,每次都要敲一堆指令無疑是惡夢。所以我們需要 Gitweb。

Gitweb 提供網頁介面,並讓 Git 支援 HTTP 協議,真正的「遠端協作」才跑得起來。

spawn-fcgi:Nginx 預設不支援 CGI,而 Gitweb 正是用 CGI 寫的,因此需要 FastCGI 作為橋樑,而 FastCGI 又必須靠 spawn-fcgi 啟動。

fcgi-devel 與 fcgi 則是 FastCGI 執行期所需的函式庫。

二、安裝 fcgiwrap

從官方下載原始碼:

https://codeload.github.com/gnosek/fcgiwrap/legacy.tar.gz/master

解壓後依次執行:

cd fcgiwrap
autoreconf -i
./configure
make
make install

安裝完成後,請確認下列檔案都存在:

  • /etc/init.d/spawn-fcgi 與 /usr/bin/spawn-fcgi
  • /var/www/git 目錄下是 Gitweb 的所有網頁檔案
  • /etc/gitweb.conf
  • /etc/sysconfig/spawn-fcgi

三、配置 Gitweb:修改 /etc/gitweb.conf

$projectroot = "/home/git/repositories";
@git_base_url_list = ("git://192.168.0.101", "https://192.168.0.101:81");
$git_temp = "/tmp";
$home_text = "indextext.html";
@stylesheets = ("gitweb.css");
$javascript = "gitweb.js";
@diff_opts = ();
$feature{pathinfo}{default} = [1];
$feature{'highlight'}{'default'} = [1];

這幾行做了三件事:所有 Git 子專案(如 hello.git)都放在 /home/git/repositories 下;對外同時暴露 git 協議與 HTTP 協議(後者監聽 81 埠)兩種存取方式;透過 @feature 開啟路徑資訊、語法高亮等 Gitweb 能力——Gitweb 還有許多好用的 feature 值得逐一試用。

四、配置 spawn-fcgi:修改 /etc/sysconfig/spawn-fcgi

原檔內容只是範例與說明:

# You must set some working options before the "spawn-fcgi" service will work.
# If SOCKET points to a file, then this file is cleaned up by the init script.
# See spawn-fcgi(1) for all possible options.
# Example :
#SOCKET=/var/run/php-fcgi.sock
#OPTIONS="-u apache -g apache -s $SOCKET -S -M 0600 -C 32 -F 1 -P /var/run/spawn-fcgi.pid -- /usr/bin/php-cgi"

改成下面這樣,重點是最後幾行:

FCGI_SOCKET=/var/run/fcgiwrap.socket
FCGI_PROGRAM=/usr/local/sbin/fcgiwrap
FCGI_USER=nginx
FCGI_GROUP=nginx
FCGI_EXTRA_OPTIONS="-M 0700"
OPTIONS="-u $FCGI_USER -g $FCGI_GROUP -s $FCGI_SOCKET -S $FCGI_EXTRA_OPTIONS -F 1 -P /var/run/spawn-fcgi.pid -- $FCGI_PROGRAM"

五、啟動 spawn-fcgi

chkconfig --levels 2345 spawn-fcgi on
/etc/init.d/spawn-fcgi start

設為開機自啟並立即啟動。服務起來之後,/var/run 下會出現一個 fcgiwrap.socket 檔案,代表一切正常。

在 CentOS 上打造 Git 私有倉庫:Nginx + Gitweb 完整實戰

六、初始化 Git 服務

useradd git
passwd git
su git

mkdir repositories          # 在 /home/git 下建立倉庫根目錄
mkdir /home/git/.ssh
cd /home/git/.ssh
ssh-keygen -t rsa
git gitosis-init < ~/.ssh/id_rsa.pub

這幾步的重點是:初始化會產生名為 gitosis-admin 的特殊倉庫,它是 Git 的權限管理中樞,所有倉庫的存取權限都由它統籌;同時,該公鑰的持有者也就取得了修改這份配置倉庫的資格。有了 gitosis,後續建立子專案、分配權限都能集中管理。

七、建立子專案

su git
cd repositories
mkdir hello.git
cd hello.git
git --bare init
chmod -R 777 /home/git/repositories/hello.git

這裡給的是最寬鬆的權限,實際環境建議收緊,只要確保該目錄具備讀、寫、執行權限即可(Git 的 unpack、pack 需要執行位)。

在 CentOS 上打造 Git 私有倉庫:Nginx + Gitweb 完整實戰

八、用其他帳號推送與拉取

新增一個普通使用者作為「客戶端」:

useradd ymk
passwd ymk
cd /home/ymk
mkdir git_repo
cd git_repo
mkdir hello.git
cd hello.git
git init

工作區初始化完成,接著就能對遠端倉庫進行推拉:

touch readme
vi readme                                     # 隨便寫點內容後存檔
git add .
git commit -a -m "init helloworld"
git remote add origin git@192.168.1.101:/home/git/repositories/hello.git
git push origin master

看到推送日誌並出現 OK、SUCCESS 字樣,就代表 SSH 通道已經暢通。接下來要讓 HTTP 協議也能推送。

九、配置 Gitweb + Nginx

編輯 Nginx 主設定檔(此例路徑為 /usr/local/nginx/nginx.conf):

server {
    error_log logs/git.error.log;
    access_log logs/git.access.log;
    listen 81;
    server_name 192.168.0.101;
    index gitweb.cgi;
    root /home/git;

    location ~ \.(cgi|pl).*$ {
        gzip off;
        fastcgi_pass unix:/var/run/fcgiwrap.socket;
        fastcgi_param SCRIPT_FILENAME /home/git/gitweb.cgi;
        fastcgi_param SCRIPT_NAME gitweb.cgi;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ ^.*\.git/objects/([0-9a-f]+/[0-9a-f]+|pack/pack-[0-9a-f]+.(pack|idx))$ {
        root /home/git;
    }

    location ~ ^.*\.git/(HEAD|info/refs|objects/info/.*|git-(upload|receive)-pack)$ {
        root /home/git;
        fastcgi_param QUERY_STRING $query_string;
        fastcgi_param SCRIPT_FILENAME /usr/libexec/git-core/git-http-backend;
        fastcgi_param GIT_HTTP_EXPORT_ALL true;
        fastcgi_param GIT_PROJECT_ROOT /home/git/repositories;
        fastcgi_param PATH_INFO $uri;
        include fastcgi_params;
        fastcgi_pass unix:/var/run/fcgiwrap.socket;
    }

    try_files $uri @gitweb;

    location @gitweb {
        fastcgi_pass unix:/var/run/fcgiwrap.socket;
        fastcgi_param SCRIPT_FILENAME /var/www/git/gitweb.cgi;
        fastcgi_param PATH_INFO $uri;
        fastcgi_param GITWEB_CONFIG /etc/gitweb.conf;
        include fastcgi_params;
    }
}

這段配置可以整份照抄,但每一行都有它的作用。逐段拆解:

第一塊宣告 Gitweb 的 HTTP 服務監聽 81 埠,首頁為 gitweb.cgi,根目錄 /home/git。

第二塊告訴 Nginx 遇到 cgi/pl 檔案該去哪裡找、由誰執行,千萬別漏掉 include fastcgi_params;,可以順手確認該檔案是否存在於 Nginx 目錄下。緊接著的 objects 規則負責讓 Git 的物件檔能被正確讀取。

第三塊是 HTTP 傳輸的關鍵:Git 透過 HTTP 傳輸時會呼叫 upload-pack、receive-pack 或 pack/unpack,這些都會用到 git-http-backend 這個類庫,因此只要 URL 中出現相關函數,就一律轉交給它處理。

第四塊的 try_files 相當於 default 分支:前面三段只說明了特定行為該由誰處理,並未涵蓋 *.git 與其他請求,剩下的全部落到 try_files。例如 https://192.168.0.101:81/hello.git 就會走這裡。若你想要更精細的 URL 規則,可以自己再練習拆分。

十、開啟 HTTP 推送能力

先別急著啟動 Nginx,我們來理解 HTTP 推送的原理:客戶端(例如 Eclipse 的 JGit)連上遠端後會送出 git-upload 請求,服務端回應 git-receive 以準備接收。但 Git 服務預設把 receive 設為 false,若不做處理,Eclipse 連上後就會出現找不到 git-receive 的錯誤。解法是:

su git
cd /home/git/repositories/hello.git
git config --file config http.receivepack true

執行後可以查看 /home/git/repositories/hello.git/config 的內容,確認 http.receivepack 已寫入。

在 CentOS 上打造 Git 私有倉庫:Nginx + Gitweb 完整實戰

一切就緒,啟動 Nginx:

cd /usr/local/nginx
./nginx -c /usr/local/nginx/nginx.conf

用戶端以瀏覽器打開 https://192.168.0.101:81,就能看到 Gitweb 的專案列表。

在 CentOS 上打造 Git 私有倉庫:Nginx + Gitweb 完整實戰

十一、用 Eclipse 存取遠端 Git

在 CentOS 上打造 Git 私有倉庫:Nginx + Gitweb 完整實戰

在 CentOS 上打造 Git 私有倉庫:Nginx + Gitweb 完整實戰

在 CentOS 上打造 Git 私有倉庫:Nginx + Gitweb 完整實戰

在 CentOS 上打造 Git 私有倉庫:Nginx + Gitweb 完整實戰

在 CentOS 上打造 Git 私有倉庫:Nginx + Gitweb 完整實戰

commit 只是把變更留在本地倉庫

在 CentOS 上打造 Git 私有倉庫:Nginx + Gitweb 完整實戰

commit 之後 push,才會真正同步到遠端

在 CentOS 上打造 Git 私有倉庫:Nginx + Gitweb 完整實戰

在 CentOS 上打造 Git 私有倉庫:Nginx + Gitweb 完整實戰

在 CentOS 上打造 Git 私有倉庫:Nginx + Gitweb 完整實戰

在 CentOS 上打造 Git 私有倉庫:Nginx + Gitweb 完整實戰

在 CentOS 上打造 Git 私有倉庫:Nginx + Gitweb 完整實戰

十二、為 Gitweb 加上帳號密碼

若希望網頁介面也需要認證,可在 Nginx 的 Gitweb location 中加入 basic auth 相關設定,並用 htpasswd 產生密碼檔。

在 CentOS 上打造 Git 私有倉庫:Nginx + Gitweb 完整實戰

Copyright © Linux教程網 All Rights Reserved