安装 & 创建 Git用户

  1. 安装 Git
    sudo apt-get install git
  2. 创建 Git 群组并添加git用户
    # Ubuntu
    groupadd git
    useradd -g git git
    # Debian
    groupadd git
    usermod -a -g git git
    # CentOS
    groupadd git
    useradd -g git git
  3. 设置密码
    passwd git
  4. 屏蔽git用户登录
    vim /etc/passwd
    # 将git:x:1001:1001:,,,:/home/git:/bin/bash
    # 修改为git:x:1001:1001:,,,:/home/git:/usr/bin/git-shell

    把/bin/sh改为/usr/bin/git-shell,用户就只能用来克隆或者推送数据到远程 git 仓库,而不能用它登录到主机。

配置Nginx

使用面板的话,可以直接在面板上配置
这里以1panel为例

  1. 创建站点,选择静态站点
    创建站点
  2. /opt/1panel/apps/openresty/openresty/www/sites/efu.me/index 即为下一步中的工作目录

直接在Nginx配置文件中配置

server {
    listen 80 ; 
    server_name www.efu.me; 
    index index.html;
    root /www/sites/www.efu.me/index; 
    include /www/sites/www.efu.me/proxy/*.conf; 
}

/www/sites/www.efu.me/index 即为下一步中的工作目录

创建裸仓库

  1. 新建一个裸仓库
    mkdir -p /home/git/project/blog.git # -p 递归创建目录
    cd /home/git/project/blog.git
    git init --bare --initial-branch=master # --bare 创建裸仓库 --initial-branch=master 创建主分支
  2. 设置权限
    chown -R git:git /home/git/project/blog.git
  3. 配置钩子
    vim hooks/post-receive
    添加以下内容 --work-tree 指定工作目录(你在Nginx上设置的网站根目录),--git-dir 指定仓库目录
    #!/bin/bash
    git --work-tree=/home/git/project/blog --git-dir=/home/git/project/blog.git checkout -f
  4. 设置权限,让钩子可执行
    chmod +x hooks/post-receive

配置Hexo

  1. 安装 hexo-deployer-git
    npm install hexo-deployer-git --save
  2. 修改配置文件
    deploy:
      type: git
      repo: ssh://git@你的服务器IP:/home/git/project/blog.git
      branch: master
  3. 部署
    hexo clean
    hexo generate
    hexo deploy
  4. 进后台查看是否部署成功
    Nginx目录