自有服务器搭建git并同步到网站根目录,实现git更新网站

一.GIT安装

 yum install -y git

服务器端创建 git 用户

[root@localhost home]# id git

id: git:无此用户

[root@localhost home]# useradd git

[root@localhost home]# passwd git

创建用户后,centos系统会在home文件夹下自动新建用户的文件夹git,

/home/git

设置git用户为www用户组,前面是用户组,后面是用户,

usermod -a -G www git

 

二、在服务器上创建裸版本库:

远程仓库通常只是一个裸仓库(bare repository),

即一个没有当前工作目录的仓库。因为该仓库只是一个合作媒介,所以不需要从硬盘上取出最新版本的快照;

仓库里存放的仅仅是 Git 的数据。

简单地说,裸仓库就是你工作目录中 .git 子目录内的内容

裸仓库,在服务器端,是看不到新增加的文件夹和文件的。

1 mkdir /home/git/testgit
2 cd /home/git/testgit
3 git init --bare te.git
4 //这里 git init 是初始化空仓库的意思,而参数 --bare 是代表创建裸仓库,这个参数一定记得带上

当运行完上面的最后一句命令时,会有提示:

Initialized empty Git repository in /home/testgit/te.git/

cd /home/git/
chown -R git:www te.git/

 

三、在nginx服务器网站根目录/www/wwwroot/下,新建网站根目录,比如test文件夹。

我是在root用户下操作的。

mkdir /www/wwwroot/test
chmod -R 775 /www/wwwroot/test

四、配置公钥

1、查看自己计算机PC(客户端)的公钥:

window系统,命令行模式下

$ ssh-keygen -t rsa -C "xxxx@qq.com"
/// 双引号内未用户邮箱,或者其他

此时 C:\Users\ Administrator \.ssh 下会多出两个文件 id_rsa 和 id_rsa.pub

id_rsa 是私钥

id_rsa.pub 是公钥

2、服务器端 Git 打开 RSA 认证

进入 /etc/ssh 目录,编辑 sshd_config,打开以下三个配置的注释:

RSAAuthentication yes(可能没有需要加上)

PubkeyAuthentication yes

AuthorizedKeysFile /home/git/.ssh/authorized_keys
保存并重启 sshd 服务:

查看状态:

systemctl status sshd.service

启动服务:

systemctl start sshd.service

重启服务:

systemctl restart sshd.service

开机自启:

systemctl enable sshd.service

在 /home/git/ 下创建目录 .ssh

[root@localhost git]# pwd /home/git

[root@localhost git]# mkdir .ssh

[root@localhost git]# ls -a 

然后把 .ssh 文件夹的 owner 修改为 git

[root@localhost git]# chown -R git:www .ssh

[root@localhost git]# ll -a 

客户端公钥导入服务器端 /home/git/.ssh/authorized_keys 文件

打开客户端的id_rsa.pub可以手动复制到服务器上述路径的authorized_keys文件中。

重要:

修改 .ssh 目录的权限为 700

修改 .ssh/authorized_keys 文件的权限为 600

[root@localhost git]# chmod 700 .ssh

[root@localhost git]# cd .ssh

[root@localhost .ssh]# chmod 600 authorized_keys  

git用户登录的安全选项

nano /etc/passwd

找到git用户,一般都在最后,按键盘方向键移动光标到最后,将

git:x:1001:1001:,,,:/home/git:/bin/bash

修改为

git:x:1001:1001:,,,:/home/git:/usr/bin/git-shell

保存。


 客户端使用TortoiseGit

TortoiseGit是一个图形界面的Git,不用打“繁琐”的命令了。

1、创建PPK私钥

找到开始菜单TortoiseGit文件夹下的PuTTYgen打开:

在打开的窗口中,Load 之前PC客户端生成的私钥id_rsa,然后save private key 生成.ppk文件,文件名自取,比如为qq.ppk

2、Clone

客户端,新建一个文件夹C,鼠标右键菜单中,使用TortoiseGit的Git Clone,在URL中填入:

git@1.1.1.1:/home/git/gittest/te.git
//(1.1.1.1替换为自己服务器ip)

load putty key选择刚才新建的qq.ppk。

最后,点击ok确认。

3、服务器端,利用钩子同步服务器代码

 如果在客服端提交代码,希望在服务器同时更新,这里我们可以使用git仓库的hooks目录下新建一个post-receive文件来进行更新。

cd /home/git/gittest/te.git/hooks
nano post-receive

在post-receive里写入以下代码:

#!/bin/bash
git --work-tree=/www/wwwroot/test checkout -f

/www/wwwroot/test  替换为你想更新的文件夹。需要确定该文件夹有git用户的权限。之前已经设置git用户归属于www用户组了。

usermod -a -G www git //这个之前已经设置过了。
chmod -R 775 /www/wwwroot/test  //这个之前已经设置过了。

4、push

在文件夹c下,新建文件夹K,然后使用TortoiseGit提交更新,必须写备注信息,才可以点击提交按钮,之后点击推送按钮。

 

 

参考:https://blog.51cto.com/lxw1844912514/2982246

https://www.cnblogs.com/chinaifae/articles/10173325.html

https://www.cnblogs.com/chinaifae/articles/10174382.html

https://blog.csdn.net/qq_40692629/article/details/124420970

posted @ 2022-11-11 23:01  总统先生  阅读(394)  评论(0编辑  收藏  举报