之前网站部署,基本用sftp,现打算用上git,实现版本控制,利用hook钩子自动部署。
服务器环境 centOS
本地 win7
首先在服务器安装好git
yum install git
在服务器上配置
新加用户git, 该用户将作为所有代码仓库和用户权限的管理者:
useradd git
passwd git
禁用shell登录
vi /etc/passwd
git:x:1001:1001:,,,:/home/git:/usr/bin/git-shell
初始化Git仓库,建立git 裸仓库,设置hook脚本(web目录需要git用户有权限,否则git的hook脚本将没有办法执行操作)
cd /var
mkdir repo
cd repo
git init --bare xxx.git
chown -R git:git xxx.git
cd xxx.git/hooks/
vi post-receive
写入以下内容
#!/bin/sh
GIT_WORK_TREE=/home/wwwroot git checkout -f
chmod +x post-receive
/home/wwwroot 为生产环境网站文件夹位置
本地配置
生成公钥 进入git-bash
ssh-keygen -t rsa -C "admin@xxx.com"
然后一直回车就行
生成的文件C:\Users\Administrator.ssh\id_rsa.pub
配置你的git用户
git-config user.name XXX
git-config user.email XXX@xxx.com
clone服务器上的仓库
clone coding add commit push
$ git clone git@serverip:/var/repo/xxx.git
Cloning into 'xxx'...
warning: You appear to have cloned an empty repository.
$ cat 'hello world!' > index.html
$ git add index.html
$ git commit -m 'first commit'
$ git remote add origin ssh://git@serverip:/opt/git/demosite.git
$ git push origin master
可以在服务器上看到/home/wwwroot/index.html 证明文件已经部署上去