Hexo 部署配置

研究一下本地 Hexo 如何快速同步到 VPS 上

VPS 端需做

處於 Root

  • 安裝Git
    1
    sudo apt-get install git
  • 創建 git 用戶
    1
    2
    useradd -m git            # 创建 git 用户,也可以用 adduser
    passwd git # 设置密码
  • 修改 git 用戶的權限
    • 修改sudoers文件
      1
      vim /etc/sudoers
    • 內容加上,並且用:wq!保存
      1
      2
      3
      ## Allowroot to run any comment anywhere
      root ALL=(ALL) ALL
      git ALL=(ALL) ALL

跳到 git 用戶sudo git

  • git 用戶下創建一個裸倉庫
    1
    2
    cd ~
    git init blog.git --bare
  • 添加/修改 hook 配置
    • post-receive
      1
      2
      cd blog.git/hooks
      vim post-receive
    • 添加內容
      1
      2
      #!/bin/bash
      git --work-tree=/放網頁文件的web目錄 --git-dir=/home/git/blog.git checkout -f
    • 授權 post-receive
      1
      chmod +x post-receive
  • 授權 git 用戶去存放網頁到 web 目錄的位置
    1
    chown git:git -R /var/www

本地

  • 安裝 hexo-deployer-git
    1
    sudo npm install hexo-deployer-git --save
  • 創建密匙
    1
    ssh-keygen -t rsa -C "blog"
  • 上傳密匙到 VPS git 用戶下
    1
    ssh-copy-id git@VPShost
  • 修改 hexo 部署配置 _config.yml
    1
    2
    3
    4
    deploy:
    type: git
    repo: git@47.106.131.90:/home/git/blog.git
    branch: master
  • 部署
    1
    2
    3
    hexo clean
    hexo g
    hexo d