安装 & 创建 Git用户

  1. 安装 Git
    1
    sudo apt-get install git
  2. 创建 Git 群组并添加git用户
    1
    2
    3
    4
    5
    6
    7
    8
    9
    # Ubuntu
    groupadd git
    useradd -g git git
    # Debian
    groupadd git
    usermod -a -g git git
    # CentOS
    groupadd git
    useradd -g git git
  3. 设置密码
    1
    passwd git
  4. 屏蔽git用户登录
    1
    2
    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 即为下一步中的工作目录
  3. 设置目录所有者给git用户
    1
    2
    chown -R git:git /opt/1panel/apps/openresty/openresty/www/sites/efu.me
    chown -R 755 /opt/1panel/apps/openresty/openresty/www/sites/efu.me

创建裸仓库

  1. 新建一个裸仓库
    1
    2
    3
    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. 设置权限
    1
    chown -R git:git /home/git/project/blog.git
  3. 配置 Hook
    1
    vim hooks/post-receive
  4. 添加以下内容 –work-tree 指定工作目录(你在Nginx上设置的网站根目录),–git-dir 指定仓库目录
    1
    2
    #!/bin/bash
    git --work-tree=/home/git/project/blog --git-dir=/home/git/project/blog.git checkout -f
  5. 设置权限,让钩子可执行
    1
    chmod +x hooks/post-receive

配置Hexo

  1. 安装 hexo-deployer-git
    1
    npm install hexo-deployer-git --save
  2. 修改配置文件
    1
    2
    3
    4
    deploy:
    type: git
    repo: ssh://git@你的服务器IP:/home/git/project/blog.git
    branch: master
  3. 部署
    1
    hexo clean && hexo g && hexo d

效果