參考 https://www.pianshen.com/article/9462214176/
http://www.360doc.com/content/17/1030/11/412471_699395136.shtml
1.安裝git 這裏暫時不考慮gitlab ,因爲是私人研究用的,暫時不需要管理後臺
apt install git
2.設置git主目錄, 並新建git帳號 (歸屬於git組) 且將git權限添加到主目錄
mkdir -p /usr/local/gitrepository
useradd -g git git
chown -R git:git /usr/local/gitrepository
3.創建配置中心倉庫
cd /usr/local/gitrepository mkdir config-repo # git初始化 git init --bare config-repo/ # 再次設置權限 chown -R git:git config-repo
4.這時從客戶端的tortoiseGit上已經可以git clone 該項目了
url: [email protected]:usr/local/gitrepository/config-repo
但是如果想用IP地址訪問 如 http://192.168.1.22:8080/config-repo 那麼就需要 下面的步驟了
5.安裝 nginx
apt install nginx
安裝好的目錄在 /etc/nginx/
6.安裝 fcgiwrap (即通過訪問一個 http 請求,運行一下某臺遠程機器上的一個 shell 腳本)
apt install fcgiwrap
7. 找到對應的 配置文件
先找到對應的git-http-backend 和 fcgiwrap.socket 文件的位置 (可以用find / -name git-http-backend 命令查找)
在我的機器上位置分別在 /usr/lib/git-core/git-http-backend 和 /run/fcgiwrap.socket
8.配置nginx.conf文件 , /etc/nginx/nginx.conf
在http項目下增加 server 模塊
server { listen 8000; server_name localhost; root /usr/local/gitrepository; auth_basic off; auth_basic_user_file off; #charset koi8-r; #access_log logs/host.access.log main; location ~ (/.*) { fastcgi_pass unix:/run/fcgiwrap.socket; fastcgi_connect_timeout 24h; fastcgi_read_timeout 24h; fastcgi_send_timeout 24h; fastcgi_param SCRIPT_FILENAME /usr/lib/git-core/git-http-backend; fastcgi_param PATH_INFO $1; fastcgi_param GIT_HTTP_EXPORT_ALL ""; fastcgi_param GIT_PROJECT_ROOT /usr/local/gitrepository; fastcgi_param REMOTE_USER $remote_user; include fastcgi_params; }
其中 fastcgi_pass 和 fastcgi_param SCRIPT_FILENAME 對應的值,就是在第7步找到的配置文件地址
9.重啓nginx
/usr/sbin/nginx -s stop
/usr/sbin/nginx
10.在客戶端使用 git clone
地址爲: http://192.168.1.22:8080/config-repo (config-repo默認只支持讀取權限,所以這裏只支持fetch/pull等讀取操作) 這些操作對於配置中心來說是足夠了
一般情況都是可以的
11.如果要使用密碼來驗證就需要在nginx.conf裏如下配置了
auth_basic "git"; auth_basic_user_file /etc/nginx/conf.d/pass.db;
pass.db 通過 https://tool.lu/htpasswd/ 來生成
如用戶名 git ,密碼 1234567! 使用 Crypt方式 生成 結果爲 git:MTMw57.B1VYIY (注意密碼只有前8位有效,超出8位的會被忽略)
然後保存到 /etc/nginx/conf.d/pass.db 文件中
echo "git:MTMw57.B1VYIY" >> pass.db