hexo-deployer-git 的思路是推到仓库,再由托管平台发布;自己有机器和 Nginx 的话,其实可以跳过仓库这一步,hexo d 直接把 public/ 打包传上去。这篇记录本站的 scp 部署器:完整代码、配置写法、宝塔环境的几个坑。

为什么要换成 scp

  • 路径最短:本地 hexo g 出 public/,打包 → 上传 → 服务器解压,三步到位,不用等平台构建。
  • 不再依赖仓库:没有远端仓库权限问题,也不用管 Pages 的构建缓存。
  • 可以双发:deploy 写成数组,先同步 GitHub Pages 备份,再发自己的服务器,两边互不影响。

前提条件

  1. 服务器已经装好 Nginx,知道站点根目录(宝塔面板里一般形如 /www/wwwroot/你的域名)。
  2. SSH 免密:本机公钥已经加到服务器的 ~/.ssh/authorized_keys,否则每次部署都要输密码。

    1
    2
    ssh-keygen -t ed25519 -C "deploy@blog"
    # 把 ~/.ssh/id_ed25519.pub 的内容追加到服务器
  3. 本机有 tar、scp、ssh:Linux / macOS 自带,Windows 用 Git Bash 也有。

第一步:写部署器

Hexo 的部署器就是注册一个名叫 scp 的 deployer:收到配置后自己决定怎么发布。

文件:[Blogroot]/scripts/deployer-scp.js | 改法:新建,整段写入。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
* 自定义 Hexo 部署器:scp
*
* 用法(_config.yml):
* deploy:
* type: scp
* host: 1.2.3.4 # 服务器公网 IP
* user: root # 登录用户
* port: 22
* path: /var/www/cookiesky-test # 远端站点根目录
* clean: true # 先清空远端目录再解包(false 则只覆盖)
*
* 说明:
* - 本机需要 tar / scp / ssh(Windows 10+ 自带,Git Bash 里也有)。
* - 免密:先把本机公钥传到服务器 ~/.ssh/authorized_keys,否则每次会提示输密码。
* - 实现:本地打包 public/ → scp 到 /tmp → 服务器解包到目标目录 → 清理临时包。
*/
'use strict';

const { execFileSync } = require('child_process');
const os = require('os');
const path = require('path');
const fs = require('fs');

function run(cmd, args, log) {
log.info('$ %s %s', cmd, args.join(' '));
execFileSync(cmd, args, { stdio: 'inherit' });
}

hexo.extend.deployer.register('scp', function (args, callback) {
const cfg = args || {};
const host = cfg.host;
const user = cfg.user || 'root';
const port = String(cfg.port || 22);
const remoteDir = cfg.path || '/var/www/cookiesky';
const clean = cfg.clean === true;

if (!host) {
return callback(new Error('deploy.host 未配置(服务器公网 IP)'));
}

const publicDir = hexo.public_dir;
if (!fs.existsSync(publicDir)) {
return callback(new Error('public/ 不存在,先执行 hexo generate'));
}

const stamp = new Date().toISOString().replace(/[-:T]/g, '').slice(0, 14);
const localTar = path.join(os.tmpdir(), `hexo-deploy-${stamp}.tar.gz`);
const remoteTar = `/tmp/hexo-deploy-${stamp}.tar.gz`;
const target = `${user}@${host}`;

try {
hexo.log.info('打包 %s ...', publicDir);
run('tar', ['-czf', localTar, '-C', publicDir, '.'], hexo.log);

hexo.log.info('上传到 %s:%s ...', host, remoteTar);
run('scp', ['-P', port, '-q', localTar, `${target}:${remoteTar}`], hexo.log);

// 宝塔会给站点根目录放 .user.ini(chattr +i 防跨站),既不能删也不能 chmod,
// 所以清理和改权限都要跳过它,否则会报 “Operation not permitted”。
// api/ 目录由服务器 cron 生成(近 7 天访问统计),部署时不能删
const skip = "! -name '.user.ini' ! -name '.htaccess' ! -name '.well-known'";
const remoteScript = [
'set -e',
`mkdir -p '${remoteDir}'`,
clean ? `find '${remoteDir}' -mindepth 1 -maxdepth 1 ${skip} -exec rm -rf {} +` : 'true',
`tar -xzf '${remoteTar}' -C '${remoteDir}'`,
// .user.ini 是 chattr +i,chmod 会报错;GNU chmod -R 会继续处理其它文件,这里忽略退出码
`chmod -R a+rX '${remoteDir}' 2>/dev/null || true`,
`rm -f '${remoteTar}'`,
].join('\n');

hexo.log.info('服务器解包到 %s ...', remoteDir);
run('ssh', ['-p', port, target, remoteScript], hexo.log);

hexo.log.info('部署完成:http://%s 已更新 %s', host, remoteDir);
callback();
} catch (err) {
callback(err);
} finally {
try { fs.unlinkSync(localTar); } catch (e) { /* ignore */ }
}
});

代码里几个关键点:

  • 打包:tar -czf 把 public/ 压成一个临时包,比逐个文件 scp 快得多。
  • 上传:scp -P 端口 把包丢到服务器的 /tmp,不占站点目录。
  • 解包:ssh 进服务器执行一段脚本:建目录 → (可选)清空 → 解包 → 修权限 → 删临时包。
  • 跳过 .user.ini / .htaccess / .well-known:宝塔会给站点根目录放 .user.ini 并 chattr +i,删不掉也改不了权限,硬删会报 Operation not permitted;.well-known 是证书验证用的,也不能动。
  • clean: true 只清远端目录的第一层,api/ 这类服务器自己生成的目录记得像注释里那样排除。
  • 本地临时包在 finally 里删掉,失败也不会留垃圾。

第二步:配置

文件:[Blogroot]/_config.yml | 改法:把 deploy 换成下面任意一种。

只发服务器:

1
2
3
4
5
6
7
deploy:
type: scp
host: 1.2.3.4 # 服务器公网 IP
user: root
port: 22
path: /www/wwwroot/example.com # Nginx 站点根目录
clean: true # 先清空远端再解包

双发(GitHub Pages 备份 + 自己的服务器):

1
2
3
4
5
6
7
8
9
10
deploy:
- type: git
repository: git@github.com:用户名/用户名.github.io.git
branch: main
- type: scp
host: 1.2.3.4
user: root
port: 22
path: /www/wwwroot/example.com
clean: true

注意:deploy 是数组时按顺序执行,git 失败会中断后面的 scp。

第三步:部署

1
hexo clean && hexo generate && hexo deploy

或者一条命令:

1
hexo d -g

部署过程会在终端打印每一条实际执行的命令($ tar ...、$ scp ...、$ ssh ...),出错时能直接看出卡在哪一步。本站 hexo g 里还挂了 gulp 压缩 CSS/JS,hexo d -g 时会在部署前自动压完再上传。

常见问题

现象 原因 处理
Permission denied (publickey) 公钥没加到服务器 重新把 id_ed25519.pub 追加到 authorized_keys,权限 600
tar: command not found Windows 的 cmd/PowerShell 没有 tar 用 Git Bash 执行,或装上 tar
部署成功但网站 404 / 403 path 不是站点根目录 对照宝塔面板「网站目录」再改
Operation not permitted .user.ini 被 chattr +i 代码已跳过它,别手动 chmod -R 全目录
首次连接提示 host key verification failed known_hosts 没有记录 先手动 ssh user@host 输一次 yes
页面样式没变 浏览器 / Service Worker 缓存 强刷,或到 DevTools 里更新 Service Worker

改完怎么验证

  1. 终端最后出现 部署完成:http://... 已更新 ...。
  2. ssh user@host "ls -la /www/wwwroot/example.com | head",文件时间和本地 public/ 一致。
  3. 浏览器强刷首页,确认是最新内容。
  4. 改一行文字重新 hexo d -g,线上能在十几秒内看到变化。

小结

hexo-deployer-git 适合托管平台,scp 部署器适合自己有机器的场景:不用仓库、不用等构建,一个 hexo d 就上线。GitHub Pages 那边继续用 git 部署当备份,两条线互不干扰。