将hexo博客部署到服务器,并支持多线部署


  • 本文提供两种部署方式 SFTP、Rsync ,但其实均是通过 SSH 方式进行连接及部署。
  • 目的: 将一份 blog 文件推送到两台 web 服务器

SFTP 方式部署

  • 以下基于 sftp 实现双线部署 ( 基于 ssh 实现 )

  • 缺点: 速度慢,会卡住导致超时。( 不推荐 )

服务器免密配置

以下配置均在同一台机器上操作

  • 生成公私钥

    ssh-keygen -t rsa -C "备注"
    • 如下
    [root@nes_dev ~]# ssh-keygen -t rsa -C "hexo_deploy"
    Generating public/private rsa key pair.
    Enter file in which to save the key (/root/.ssh/id_rsa): /root/.ssh/hexo_deploy  
    
    >>>
    输出私钥为 /root/.ssh/hexo_deploy
    输出公钥为 /root/.ssh/hexo_deploy.pub
  • 创建 ssh 账号

    groupadd hexo
    useradd -g hexo hexo
  • 创建 authorized_keys 文件

    mkdir /home/hexo/.ssh
    cat ~/.ssh/hexo_deploy.pub > /home/hexo/.ssh/authorized_keys
    
    # 赋权
    chown -R hexo.hexo /home/hexo/.ssh
    chmod 700 /home/hexo/.ssh
    chmod 600 /home/hexo/.ssh/authorized_keys

第二台服务器同样操作第二、三步,最后只需复制成品公钥内容即可。

博客部署配置

  • 将刚才服务器生成的私钥复制到本地 /data/keys/.ssh/hexo_deploy

  • 进入博客根目录,安装部署插件

    cd /usr/local/nes_hexo_blog
    
    npm install hexo-deployer-sftp --save
  • 修改博客部署配置 vim _config.yml

    • 配置模板

      deploy:
        type: sftp
        host: <host> # 目标主机IP,或域名
        port: [port] # 目标主机的SSH端口
        user: <user> # SSH 用户
        pass: <pass> # leave blank for paswordless connections
        privateKey: [path/to/privateKey] # Optional
        passphrase: [passphrase] # Optional
        agent: [path/to/agent/socket] # Optional, defaults to $SSH_AUTH_SOCK
        remotePath: [remotePath] # Default is `/`,目标服务器部署路径
    • 例如:

      deploy:
      # 双线部署,type一定要加横线
      - type: sftp
        host: hexo.nestealin.com
        port: 12345
        user: hexo
        privateKey: /data/keys/.ssh/hexo_deploy
        remotePath: /data/nes_hexo_blog
      
      - type: sftp
        host: 172.17.20.3
        port: 23456
        user: hexo
        privateKey: /data/keys/.ssh/hexo_deploy
        remotePath: /data/nes_hexo_blog
  • 此时,在执行 hexo generate 以及hexo deploy 的时候就可以实现双线部署推送了。

Rsync 方式部署

  • 以下基于 Rsync 实现双线部署 ( 实际还是基于 ssh 实现 )

  • 优点: 解决 sftp 传输速度慢的问题。

  • 继续使用上述创建的免密公私钥。

博客部署配置

  • 将刚才服务器生成的私钥复制到本地 /data/keys/.ssh/hexo_deploy

  • 进入博客根目录,安装部署插件

    cd /usr/local/nes_hexo_blog
    
    npm install hexo-deployer-rsync --save
  • 修改博客部署配置 vim _config.yml

    • 配置模板

      deploy:
        type: rsync
        host: <host>  # 目标主机IP或域名
        user: <user>  # SSH 用户名
        root: <root>  # 目标主机存放目录,需要给予上述用户写入权限
        port: [port]  # SSH端口
        delete: [true|false]   # 部署时是否全量覆盖远端目录
        verbose: [true|false]  # 部署时是否输出详细内容
        ignore_errors: [true|false]
    • 例如:

      deploy:
      # 双线部署,type一定要加横线
      - type: rsync
        host: hexo.nestealin.com
        user: hexo
        root: /data/nes_hexo_blog/
        port: 12345
        delete: true
        verbose: true
        ignore_errors: true
      
      - type: rsync
        host: 1.2.3.4
        user: hexo
        root: /data/nes_hexo_blog/
        port: 23456
        delete: true
        verbose: true
        ignore_errors: true
  • 由于本次使用私钥进行ssh登陆,所以要在 js 中指定私钥位置

    • 在博客根路径下编辑部署插件的 JS 配置

      cd /usr/local/nes_hexo_blog
      
      vim node_modules/hexo-deployer-rsync/lib/deployer.js
      • 大约41行增加 -i 配置指定私钥文件

        if (args.port && args.port > 0 && args.port < 65536) {$
          params.splice(params.length - 2, 0, '-e');$
          params.splice(params.length - 2, 0, 'ssh -i /data/keys/.ssh/hexo_deploy -p ' + args.port);$
        }$
  • 此时,在执行 hexo generate 以及 hexo deploy 的时候就可以实现双线部署推送了。


文章作者: NesteaLin
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 NesteaLin !
  目录