环境:阿里云 ECS ,已安装BT面板,建站成功,BT默认会创建www用户且建站的属主为www
这里采用root账户进行操作
站点目录:/www/wwwroot/blog
站点属主:www:www
服务端操作
1、安装git
yun install -y git
2、创建裸仓库
mkdir -p /opt/hexo-deploy.git
git init --bare /opt/hexo-deploy.git
3、配置钩子
cat >/opt/hexo-deploy.git/hooks/post-receive <<'EOF'
#!/bin/bash
set -e
GIT_WORK_TREE="/www/wwwroot/main"
mkdir -p "$GIT_WORK_TREE"
git --work-tree="$GIT_WORK_TREE" checkout -f
chown -R www:www "$GIT_WORK_TREE"
EOF
chmod +x /opt/hexo-deploy.git/hooks/post-receive
本地操作
1、创建密钥
ssh-keygen -t ed25519 -C "hexo-deploy-root"
2、把公钥传到服务器
Get-Content $env:USERPROFILE\.ssh\id_ed25519.pub | `
ssh root@x.xxx.xx.x "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"
Hexo 配置
deploy:
type: git
repo: ssh://root@x.xxx.xx.x/opt/hexo-deploy.git
branch: master
hexo clean
hexo g
hexo d
Hexo 报错 警告
标记 .deploy_git 为安全目录
git config –global –add safe.directory F:/Blog/Blog/.deploy_git
Git 身份信息
git config –global user.name “YourName”
git config –global user.email “you@example.com“
Windows 默认是 CRLF (\r\n),Linux 默认是 LF (\n)
warning: in the working copy of ‘about/index.html’, LF will be replaced by CRLF the next time Git touches it
warning: in the working copy of ‘archives/2025/08/index.html’, LF will be replaced by CRLF the next time Git touches it
warning: in the working copy of ‘archives/2025/09/index.html’, LF will be replaced by CRLF the next time Git touches it
忽略警告:git config –global core.autocrlf false
设置免密 hexo d
首先确保上述步骤完成,且权限满足要求
authorized_keys中保存的是本地pub文件
在本地ssh文件夹下新增config文件
Host aliyun
HostName x.xxx.xx.x
User root
IdentityFile C:/Users/xxx/.ssh/hexo-deploy-root
修改deploy
deploy:
type: git
repo: ssh://aliyun/opt/hexo-deploy.git
branch: master