玩轉Linux(2)——在自己的服務器上搭建Git私有倉庫(GitWeb)

前言

GitWeb 和GitLab相比,一個是簡單的倉庫查看器,一個是複雜的Git管理系統。
之所以不安裝GitLab而選擇安裝GitWeb的原因有以下:
1、GitLab對配置要求很高
GitLab是基於ruby的,此外還使用了Postgresql、redis等,啓動的worker process很多,官方推薦至少需要2核4G。

2、不需要特別多人,沒有複雜的權限控制要求
基本是一個“私服”,用來與Jenkins配合實現自動集成,未來可能會有別人用,但也不會有多少人。
如果有人可以直接通過ssh添加公鑰的方式。
因此如果搭建GitLab是殺雞用牛刀。不選擇gogs、gitea的原因也是如此。

所以選擇了GitWeb(其實搭建GitWeb是更麻煩的),如果看官更喜歡gitlab、gogs、gitea可以去github上搜索,對應的配置在網絡上可以參考的教程也很多。

Git遠程倉庫搭建

1、安裝git、配置git

sudo apt-get install git
git config --global user.name "github用戶名"
git config --global user.email "github郵箱"

配置好之後就可以正常的在該服務器上使用git了。

2、建立git用戶

sudo adduser git

3、複製你的ssh公鑰
windows默認在C:\Users\你的用戶名.ssh\id_rsa.pub,把id_rsa.pub裏的內容複製到/home/git/.ssh/authorized_keys文件裏。後邊你可以通過這種方式添加協同夥伴,同樣複製他們的公鑰到authorized_keys即可。

4、建立倉庫

sudo git init --bare /home/git/test.git
sudo chown -R git:git  /home/git/test.git

https://www.liaoxuefeng.com/wiki/896043488029600/899998870925664
–bare:Git會創建一個裸倉庫,裸倉庫沒有工作區,因爲服務器上的Git倉庫純粹是爲了共享,所以不讓用戶直接登錄到服務器上去改工作區,並且服務器上的Git倉庫通常都以.git結尾。

5、禁止git用戶登錄shell
出於安全考慮,第二步創建的git用戶不允許登錄shell,這可以通過編輯/etc/passwd文件完成。找到類似下面的一行:

	git:x:1001:1001:,,,:/home/git:/bin/bash
	改爲
	git:x:1001:1001:,,,:/home/git:/usr/bin/git-shell

6、克隆
在你的本地電腦上輸入

git clone ssh://git@你的域名:你的SSH端口/home/git/test.git
  • 如果出現錯誤,請檢查前面的配置,如authorized_keys是否配置正確git用戶是否有文件權限,還有倉庫的權限是否設置正確。
  • 如果需要密碼,那麼可能公鑰複製出錯,可以刪除C:\Users\你的用戶名.ssh\known_hosts中對應該服務器IP的記錄,再次克隆試試。
  • 如果你的SSH端口沒有修改過,那麼默認是22,可以不用顯式指定。

GitWeb搭建過程

雖然沒有複雜的權限控制要求,但是還是要做登錄,因爲服務器內其他應用都是通過nginx轉發的,所以決定Git web也使用Nginx轉發。

1、安裝gitweb和用到的配置軟件

sudo apt-get -y install gitweb spawn-fcgi  autoconf pkg-config libfcgi-dev

2、安裝fastcgi-wrapper

git clone https://github.com/gnosek/fcgiwrap.git
cd fcgiwrap/
autoreconf -i
./configure
make CFLAGS='-Wno-implicit-fallthrough'
sudo make install

3、啓動

sudo spawn-fcgi -f /usr/local/sbin/fcgiwrap -p 12345

4、配置GitWeb

sudo vim /etc/gitweb.conf

# git倉庫存放的目錄
# path to git projects (<project>.git)
$projectroot = "/home/git/repositories/";

# directory to use for temp files
$git_temp = "/tmp";

# 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的話,gitweb會自動在$projectroot定義的目錄下遞歸查找合法的git repo來顯示。
$projects_list = "/home/git/projects.list";
# $strict_export參數規定只有顯示在首頁上的repo才能夠被訪問。換句話說在有projects.list的情況下,該文件列出的repo才能被訪問。
$strict_export = 1;

# stylesheet to use
#@stylesheets = ("static/gitweb.css");

# javascript code for gitweb
$javascript = "static/gitweb.js";

# logo to use
$logo = "static/git-logo.png";

# the 'favicon'
#$favicon = "static/git-favicon.png";

# git-diff-tree(1) options to use for generated patches
#@diff_opts = ("-M");
@diff_opts = ();

# $feature數組啓用了一些插件或者說特性。blame可以顯示代碼來源人,snapshot提供repo的打包下載,highlight提供代碼高亮。
$feature {'blame'}{'default'} = [1];
$feature {'blame'}{'override'} = 1;

$feature {'snapshot'}{'default'} = ['zip', 'tgz'];
$feature {'snapshot'}{'override'} = 1;

$feature{'highlight'}{'default'} = [1];

5、配置Nginx
鏈接gitweb文件

sudo ln -s /usr/share/gitweb/ /var/www/
# GitWeb
server {
        listen 80;

        server_name hostname; # 如xxx.com,建議使用二級域名,如gitweb.xxx.com

        root /var/www/gitweb/;

        rewrite ^/$ http://$host$1/index.cgi permanent;

        location ~ ^.*\.cgi$ {
            root /var/www/gitweb/;
            fastcgi_pass  127.0.0.1:12345;
            fastcgi_index index.cgi;
            include fastcgi.conf;
        }

        # access log file 訪問日誌
        access_log logs/gitweb.access.log;
}

6、瀏覽器訪問你的hostname,如http://gitweb.xxx.com/
在這裏插入圖片描述

7、將一個之前建好的倉庫添加到projects.list
未添加的倉庫是顯示不出來的(通過這個可以控制你想公開的倉庫)

echo "test.git" >> projects.list

8、樣式不好看,修改一下:

git clone https://github.com/kogakure/gitweb-theme.git
cd gitweb-theme
sudo ./setup -vi --insstall
sudo service nginx restart

在這裏插入圖片描述

9、設置登錄認證


sudo htpasswd -c /etc/nginx/gitweb.passwd name

在nginx配置中添加
auth_basic "請先登錄";
auth_basic_user_file /etc/nginx/gitweb.passwd;

進入網站提示:
在這裏插入圖片描述

到這裏就都搭建完成了~

附:GitWeb倉庫配置(在具體的倉庫中)

1、description 中可以設置倉庫的描述
2、cloneurl 顯示該倉庫的克隆路徑
3、README.html來創建你的倉庫內容描述
4、config中可以設置owner = Your Name

http://sourceforge.net/apps/trac/sourceforge/wiki/GitWeb%20repository%20browser

Create a “description” file in your git repository root, with a brief, one-line description of your repository. This file is treated as plain text, > > any HTML will be escaped. This will appear in the top section of the gitweb repository landing page.
Create a “cloneurl” file in your git repository root, containing one url per line. Use this to display the clone url for your repository. This will > appear in the same section as the description line, one url per line.
Create a “README.html” file in your git repository root, with arbitrary descriptive content. HTML is allowed, and will be displayed inside a > div tag on the gitweb page, in a section below the one with description.
Set the owner, by setting a gitweb.owner configuration variable in the “config” file, located in your git repository root. If the “gitweb” section does not exist, create it. The owner setting in the config should look like the sample below (you can use any arbitrary string for owner):
[gitweb]
owner = Your Name

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章