1.首先安装gitphp
yum install git
2.新建一个linux用户,起名为githtml
adduser git
3.在git用户目录中新建目录 .sshlinux
cd /home/git mkdir .ssh
4.在/home/git/.ssh/目录中新建authorized_keys文件,并将客户端提供的公钥(id_rsa.pub)黏贴到该文件中git
vim authorized_keys
5.在项目目录建立一个git裸仓库,假如当前项目目录为/home/git/project.gitweb
git init --bare project.git
6.将项目目录和git用户目录下的.ssh目录的全部者和所属组都设置成gitshell
chown -R git.git project.git chown -R git.git /home/git/.ssh/
7.为了安全考虑,禁用git用户的shell登陆vim
vim /etc/passwd 注释 #git:x:500:500::/home/git:/bin/bash 改成 git:x:500:500::/home/git:/usr/bin/git-shell
8.git服务器打开RSA认证安全
vim /etc/ssh/sshd_config 下面3个打开 1.RSAAuthentication yes 2.PubkeyAuthentication yes 3.AuthorizedKeysFile .ssh/authorized_keys
1.查看公钥bash
cat ~/.ssh/id_rsa.pub
若是没有的话,能够执行如下命令服务器
ssh-keygen -t rsa
2.在本地新建git仓库
git init
3.新建一个文件并推送到服务器
touch readme.txt git add readme.txt git commit -m "readme" git remote add origin git@xxx.xxx.xxx.xxx:/home/git/project.git git push origin master
注:若是提示须要密码,请检测公钥是否配置成功或RSA是否开启。
报错信息为ssh: connect to host 104.224.152.22 port 22: Connection refused
的时候注意下,sshd服务是否开启(通常都是默认开启的)
这个时候,咱们要检查sshd
服务的端口是否为22
netstat -lnp|grep 22
sshd
服务的端口号不为22,咱们能够在/etc/ssh/sshd_config
修改默认端口
1.直接修改URL为SSH://开头
git remote set-url origin ssh://git@domain.com:3022/~/Projects/p1.git
2.修改本地配置文件
vim ~/.ssh/config # 映射一个别名 host newdomain hostname domain.com port 3022
post-receive
#!/bin/bash IS_BARE=$(git rev-parse --is-bare-repository) if [ -z "$IS_BARE" ]; then echo >&2 "fatal: post-receive: IS_NOT_BARE" exit 1 fi unset GIT_DIR DeployPath="/var/www/blog" #这里写项目实际部署的目录 cd $DeployPath git fetch --all git reset --hard origin/master
cd /var/www git clone /home/git/project.git 项目名
注:权限问题
在实际使用的时候,会遇到Permission Denied 之类的事情。
那么你要检查下 /var, /var/www, /var/www/your_git 三个目录的权限是否至少开到了 770 上. 而后还要考虑是否有 SELinux 在挡道。
个人处理方式是权限所有开启(由于是我的的服务器,并且也没什么人访问,主要是拿来玩的)
chmod -R 777 /var/www/xxx
还有一个问题,就是push和clone的时候,仓库是最开始建立的空仓库(本文是/home/git/project.git)
最后还有一个问题
Git: push 出错的解决 master -> master (branch is currently checked out)
在使用Git Push代码到数据仓库时,提示以下错误:
[remote rejected] master -> master (branch is currently checked out) remote: error: refusing to update checked out branch: refs/heads/master remote: error: By default, updating the current branch in a non-bare repository remote: error: is denied, because it will make the index and work tree inconsistent remote: error: with what you pushed, and will require 'git reset --hard' to match remote: error: the work tree to HEAD. remote: error: remote: error: You can set 'receive.denyCurrentBranch' configuration variable to remote: error: 'ignore' or 'warn' in the remote repository to allow pushing into remote: error: its current branch; however, this is not recommended unless you remote: error: arranged to update its work tree to match what you pushed in some remote: error: other way. remote: error: remote: error: To squelch this message and still keep the default behaviour, set remote: error: 'receive.denyCurrentBranch' configuration variable to 'refuse'. To git@xxx.xxx.xxx.xxx:/xxx/xxxx/xxxx ! [remote rejected] master -> master (branch is currently checked out) error: failed to push some refs to 'git@xxx.xxx.xxx.xxx:/xxx/xxxx/xxxx'
这是因为git默认拒绝了push操做,须要进行设置,修改.git/config添加以下代码:
git config receive.denyCurrentBranch ignore
Centos搭建GIT服务器---老高的技术博客
利用Git自动部署环境
GIT服务器实现web代码自动部署
Git服务器端代码自动部署