Git服務器搭建和鉤子使用

git的安裝

1 安裝git

git --version #查看當前系統安裝的git版本
yum install -y git #安裝git

如果安裝了會顯示對於的版本,沒有安裝則執行安裝命令。卸載命令
cd /usr/bin/git #git一般默認位置
sudo rm -rf git* #卸載

2 添加git的管理的賬戶和設置密碼

# 添加git賬戶
$ adduser git

# 修改git的密碼
$ passwd git
# 然後兩次輸入git的密碼確認後。

# 查看git是否安裝成功
$ cd /home && ls -al
# 默認還給我們分配一個名字叫git的組。

遇到的問題:
【1】 添用戶報錯:useradd:警告:此主目錄已經存在
解決辦法 userdel -rf mysql

3 配置服務端的ssh訪問

# 1.切換到git賬號
$ su git
# 2.進入 git賬戶的主目錄
$ cd /home/git

# 3.創建.ssh的配置,如果此文件夾已經存在請忽略此步。
$ mkdir .ssh

# 4. 進入剛創建的.ssh目錄並創建authorized_keys文件,此文件存放客戶端遠程訪問的 ssh的公鑰。
$ cd /home/git/.ssh
$ touch authorized_keys

# 5. 設置權限,此步驟不能省略,而且權限值也不要改,不然會報錯。
$ chmod 700 /home/git/.ssh/
$ chmod 600 /home/git/.ssh/authorized_keys

4 配置客戶端的ssh私鑰並上傳服務器

 rz #拷貝公鑰 id_rsa.pub到git服務器,位置 /home/git/.ssh

本地上傳文件到服務器
5 服務器端添加客戶端的SSH公鑰

cat id_rsa.pub >> authorized_keys
# 在客戶端用ssh測試連接遠程服務器
$ ssh 服務器ip

服務器端創建測試git倉庫

1 創建遠程倉庫

$ cd /home/git
# 在用戶主目錄下創建 demo.git倉庫的文件夾
$ mkdir demo.git  && cd demo.git
# 在test.git目錄下初始化git倉庫
$ git init --bare
# 輸出如下內容,表示成功
Initialized empty Git repository in /home/git/demo.git/

2 客戶端測試連接git 遠程倉庫

mkdir demo && cd demo #本地創建項目
git init #初始化倉庫
git add
git commit -m "註釋"
git remote add origin git@ip地址:demo.git #把當前倉庫跟遠程倉庫添映射
git push -u origin master #推送到遠程倉庫

遇到的問題
[1]關聯遠程倉庫提示:‘fatal:remote origin already exists’
git remote rm origin

自動同步到站點目錄

cd /home/git/demo.git #進入項目倉庫
cd hooks
//創建post-receive文件
vim post-receive
//在該文件裏輸入以下內容
#!/bin/bash
git --work-tree=/home/demo checkout -f
// 這裏demo是自己的項目站點目錄
//保存退出後,將該文件用戶及用戶組都設置成git
chown git:git post-receive
//由於該文件其實就是一個shell文件,我們還應該爲其設置可執行權限
chmod +x post-receive

注意:通過域名或IP直接訪問項目,需要配置nginx.conf文件

server {
        listen       8070;
        server_name  IP;
        root         /home/demo; #項目目錄
        # Load configuration files for the default server block.

         add_header X-Frame-Options "SAMEORIGIN";
         add_header X-XSS-Protection "1; mode=block";
         add_header X-Content-Type-Options "nosniff";

         index index.html index.htm index.php;

         charset utf-8;

         location / {
                try_files $uri $uri/ /index.php?$query_string;
        }

         location = /favicon.ico { access_log off; log_not_found off; }
         location = /robots.txt  { access_log off; log_not_found off; }


        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;

參考文章

https://www.cnblogs.com/fly_dragon/p/8718614.html
https://blog.csdn.net/baidu_30000217/article/details/51327289
https://www.jianshu.com/p/aeccbe91fff3

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