tools/hexo-博客搭建指南
tools/hexo-博客搭建指南
一、安装与初始化
1. 安装 Node.js 和 Git
确保系统已安装 Node.js 和 Git。
# 验证安装node -vnpm -vgit version2. 安装 Hexo CLI
npm install -g hexo-clihexo version3. 初始化博客
hexo init <folder>cd <folder>npm install目录结构:
├── _config.yml # 站点配置文件(核心)├── package.json├── scaffolds/ # 模板文件├── source/ # 源码(文章、页面)│ ├── _posts/ # 文章│ └── _drafts/ # 草稿└── themes/ # 主题二、常用命令
hexo new "文章标题" # 新建文章hexo new draft "草稿" # 新建草稿hexo publish "草稿名" # 发布草稿hexo generate (hexo g) # 生成静态文件hexo clean # 清理缓存(public/ 和 db.json)hexo server (hexo s) # 本地预览,默认 http://localhost:4000hexo deploy (hexo d) # 部署hexo d -g # 先 clean 再 generate 再 deploy(常用)hexo new page "about" # 新建独立页面三、站点配置 (_config.yml)
# 站点信息title: 我的博客subtitle:description: '博客描述'keywords: '关键词1,关键词2'author: 作者名language: zh-CNtimezone: ''
# URLurl: https://yourdomain.comroot: /permalink: :year/:month/:day/:title/
# 主题theme: butterfly
# 部署deploy: type: git repo: ssh://git@your-server-ip:port/home/git/blog.git branch: master四、Git SSH 密钥管理
部署到服务器需要 SSH 免密登录,先管理好本地密钥。
1. 查看 git 配置
git config --listgit config user.namegit config user.email2. 初始化用户名和邮箱
git config --global user.name "你的用户名"git config --global user.email "你的邮箱"3. 查看密钥文件位置
cd ~/.sshpwd # 输出 .ssh 文件夹路径ls # 查看密钥文件4. 生成密钥(若没有)
ssh-keygen -t rsa -C "你的邮箱"# 一路回车,默认路径即可生成后在 ~/.ssh/ 下会有:
id_rsa—— 私钥(保密)id_rsa.pub—— 公钥(配置到服务器/GitHub)
5. 查看公钥
cat ~/.ssh/id_rsa.pub复制输出内容,添加到服务器的 ~/.ssh/authorized_keys 或 GitHub/Gitee 的 SSH Keys 设置中。
五、部署至自己的服务器
1. 服务器安装 Git
# CentOS / RHELyum install git
# Ubuntu / Debianapt-get install git
# 验证git version2. 创建 git 用户
# 添加用户adduser git
# 赋予 sudo 权限 ,为了安全可以不加chmod 740 /etc/sudoersvim /etc/sudoers
# 在 root 行下方添加:git ALL=(ALL) ALL
# 按 Esc → :wq 保存退出chmod 400 /etc/sudoers
# 设置 git 用户密码sudo passwd git3. 配置 SSH 免密登录
切换到 git 用户:
su - git创建 .ssh 目录和 authorized_keys 文件:
mkdir ~/.sshvim ~/.ssh/authorized_keys# 将本地 id_rsa.pub 的内容粘贴进去# Esc → :wq 保存退出设置权限:
chmod 600 ~/.ssh/authorized_keyschmod 700 ~/.ssh本地测试连接:
ssh -v git@服务器IP# 能进入 git 用户 shell 即成功4. 创建 Git 裸仓库
切回 root 用户,创建仓库和网站根目录:
# 创建网站根目录chown -R git:git /opt/1panel/www/sites/zhangzhiwu.cn/indexchmod -R 755 /opt/1panel/www/sites/zhangzhiwu.cn/index
# 创建裸仓库cd /home/gitgit init --bare blog.gitchown -R git:git blog.git5. 配置 post-receive 钩子(自动部署)
在 /home/git/blog.git/hooks/ 下创建钩子文件:
cd /home/git/blog.git/hooks/vim post-receive写入以下内容:
#!/bin/bashgit --work-tree=/opt/1panel/www/sites/zhangzhiwu.cn/index/hexo --git-dir=/home/git/blog.git checkout -f保存后赋予可执行权限:
chmod +x post-receivechown git:git post-receive作用:每次本地
hexo d推送代码到仓库,服务器自动将文件更新到/opt/1panel/www/sites/zhangzhiwu.cn/index/hexo目录。
6. 本地 Hexo 配置部署
编辑博客根目录 _config.yml,找到 deploy 段:
deploy: type: git repo: git@服务器IP:/home/git/blog.git # 如果 SSH 端口不是默认 22,使用 ssh:// 格式: # repo: ssh://git@服务器IP:端口/home/git/blog.git branch: master安装 git 部署插件:
npm install hexo-deployer-git --save执行部署:
hexo cleanhexo ghexo d7. Nginx 配置
服务器上安装 Nginx,将网站根目录指向 /opt/1panel/www/sites/zhangzhiwu.cn/index/hexo:
server { listen 80; server_name yourdomain.com; # 你的域名或IP
root /opt/1panel/www/sites/zhangzhiwu.cn/index/hexo; index index.html index.htm;
location { try_files $uri $uri/ $uri/index.html =404; }
# 开启 gzip 压缩 gzip on; gzip_types text/plain text/css application/json application/javascript text/xml;}重载 Nginx:
nginx -t # 测试配置nginx -s reload # 重载访问 http://yourdomain.com 即可看到博客。
六、主题安装(以 Butterfly 为例)
1. 安装
npm install hexo-theme-butterfly2. 启用
在 _config.yml 中修改:
theme: butterfly3. 主题配置
创建 _config.butterfly.yml(优先级高于主题内置配置)。
七、常见问题
| 问题 | 解决 |
|---|---|
ERROR Deployer not found: git | npm install hexo-deployer-git --save |
| 部署后页面空白/404 | 检查 Nginx root 目录是否指向正确路径(/opt/1panel/www/sites/zhangzhiwu.cn/index/hexo) |
| SSH 连接超时 | 检查服务器安全组/防火墙是否放行 SSH 端口 |
| 推送成功但页面未更新 | 检查 post-receive 钩子权限(chmod +x)和路径 |
Permission denied (publickey) | 本地公钥未添加到服务器 authorized_keys,或权限不是 600 |
| 本地预览正常部署后样式丢失 | 执行 hexo clean 清理缓存后重新 hexo g |
八、快速部署流程总结
本地 服务器──── ────hexo clean 1. yum install githexo g 2. adduser git + 配置 SSHhexo d ──── git push ────────▶ 3. git init --bare blog.git 4. post-receive 钩子 5. 自动 checkout 到 /opt/1panel/www/sites/zhangzhiwu.cn/index/hexo 6. Nginx 指向 /opt/1panel/www/sites/zhangzhiwu.cn/index/hexo
用户访问 yourdomain.com ◀──────── Nginx 返回静态文件九、Butterfly 音乐播放器
1. 插件安装
npm install --save hexo-tag-aplayer2. 配置文件修改
在 Hexo 根目录 _config.yml 中添加:
aplayer: meting: true asset_inject: false在主题配置文件中开启:
# Inject the css and script (aplayer/meting)aplayerInject: enable: true per_page: true3. 普通界面播放器
{% meting "7422861869" "netease" "playlist" "autoplay" "mutex:false" "listmaxheight:400px" "preload:none" "theme:#ad7a86"%}常用选项:
| 参数 | 可选值 | 说明 |
|---|---|---|
| server | netease, tencent, kugou, xiami, baidu | 音乐平台,建议网易云 |
| type | song, playlist, album, search, artist | 类型 |
| id | 数字 | 歌单/歌曲 ID,从网页版 URL 获取 |
| lrcType | -1 | 默认显示歌词,fixed 模式下适用 |
也可直接使用 HTML:
<div class="aplayer" data-id="000PeZCQ1i4XVs" data-server="tencent" data-type="artist" data-preload="auto" data-theme="#3F51B5"></div>4. 全局吸底播放器
把代码插入到主题配置文件的 inject.bottom:
inject: head: bottom: - <div class="aplayer no-destroy" data-id="7422861869" data-server="netease" data-type="playlist" data-fixed="true" data-autoplay="true" data-lrcType="-1"> </div>5. 切换页面音乐不中断
pjax: enable: true exclude:6. Aplayer 完整配置参数
| Name | Default | Description |
|---|---|---|
| container | document.querySelector('.aplayer') | player container |
| fixed | false | enable fixed mode |
| mini | false | enable mini mode |
| autoplay | false | audio autoplay |
| theme | ’#b7daff’ | main color |
| loop | ’all’ | player loop play: ‘all’, ‘one’, ‘none’ |
| order | ’list’ | play order: ‘list’, ‘random’ |
| preload | ’auto’ | values: ‘none’, ‘metadata’, ‘auto’ |
| volume | 0.7 | default volume |
| mutex | true | prevent multiple players at same time |
| lrcType | 0 | lrc type |
| listFolded | false | whether list should folded at first |
| listMaxHeight | - | list max height |
| storageName | ’aplayer-setting’ | localStorage key |

苏公网安备32021402002558号