Pixiv - KiraraShss
1280 字
6 分钟
Hexo+GithubPages部署博客
Random Cover
使用HEXO + Github Pages搭建个人博客
功能特点
- 无需服务器
- 免费
- 主题,插件很多
- 开源社区 Hexo用户群体多 便于讨论或提出问题
Step.1
下载node.js,git
此处跳过
Step.2
下载Hexo并配置
确保nodejs git安装成功后,下载Hexo Hexo官网
先创建一个新的文件夹hexo,用于存放hexo相关文件
然后根据指令,输入
$ npm install hexo-cli -g #用于安装hexo 如果你有经验,可以使用 hexo库安装部分组件确保没有出现error报错,进行下一步
$ hexo init blog这一步默认从github上下载blog资源,慢是正常的,挂梯子会更快
没有出现报错,继续下一步
$ cd blog #进入blog目录$ npm install #确保没有报错$ hexo server #可缩写 hexo s之后使用命令行中的链接打开,打开成功?
恭喜你,你成功搭建了一个属于自己的个人博客
好的。接下来让我们更近一步!
目录
前置准备
1. 注册GitHub账号
- 访问 GitHub官网
- 点击”Sign up”注册新账号
- 记住用户名,将用于生成博客地址
2. 创建GitHub仓库
- 登录GitHub后,点击右上角”+” → “New repository”
- 仓库名格式:
用户名.github.io - 例如:用户名为
john,则仓库名为john.github.io - 选择”Public”,勾选”Add a README file”
环境安装
1. 安装Node.js
# Windows/macOS用户# 访问 https://nodejs.org 下载LTS版本安装
# Ubuntu/Debiancurl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -sudo apt-get install -y nodejs
# 验证安装node -vnpm -v2. 安装Git
# Windows: 下载 https://git-scm.com/download/win# macOS: brew install git# Ubuntu/Debian: sudo apt install git
# 验证安装git --version3. 配置Git
# 设置用户信息git config --global user.name "你的用户名"git config --global user.email "你的邮箱"
# 生成SSH密钥ssh-keygen -t rsa -b 4096 -C "你的邮箱"# 将公钥添加到GitHubcat ~/.ssh/id_rsa.pub创建Hexo项目
1. 安装Hexo CLI
npm install -g hexo-cli2. 初始化博客
# 创建博客目录hexo init my-blogcd my-blog
# 安装依赖npm install
# 启动本地服务器hexo server# 访问 http://localhost:4000 查看效果配置Hexo
1. 基本配置(_config.yml)
# Sitetitle: 你的博客标题subtitle: 博客副标题description: 博客描述keywords: 关键词1, 关键词2author: 你的名字language: zh-CNtimezone: Asia/Shanghai
# URLurl: https://用户名.github.ioroot: /permalink: :year/:month/:day/:title/permalink_defaults:
# Directorysource_dir: sourcepublic_dir: publictag_dir: tagsarchive_dir: archivescategory_dir: categoriescode_dir: downloads/code
# Writingnew_post_name: :title.mddefault_layout: post
# Extensionstheme: landscape # 默认主题
# Deploymentdeploy: type: git repo: https://github.com/用户名/用户名.github.io.git branch: main2. 安装部署插件
npm install hexo-deployer-git --save配置GitHub Pages
1. 设置仓库
- 进入仓库设置:Settings → Pages
- Source选择:Deploy from a branch
- Branch选择:main
- 文件夹选择:/(root)
- 点击Save
2. 检查Pages状态
- 访问
https://用户名.github.io - 初次部署需要几分钟生效
部署博客
1. 生成静态文件
# 清理旧文件hexo clean
# 生成新文件hexo generate# 或简写hexo g2. 部署到GitHub
# 部署命令hexo deploy# 或简写hexo d
# 组合命令(常用)hexo clean && hexo g && hexo d3. 验证部署
- 访问
https://用户名.github.io - 查看博客是否正常显示
常用命令
创建新文章
hexo new "文章标题"# 简写hexo n "文章标题"创建新页面
hexo new page about# 会在source/about/index.md创建关于页面本地预览
hexo server# 简写hexo s# 指定端口hexo s -p 8080草稿功能
# 创建草稿hexo new draft "草稿标题"
# 预览草稿hexo server --draft
# 发布草稿hexo publish "草稿标题"主题配置
1. 安装主题(以NexT为例)
cd my-bloggit clone https://github.com/theme-next/hexo-theme-next themes/next2. 启用主题
# _config.yml中修改theme: next3. 配置主题
- 复制主题配置:
cp themes/next/_config.yml _config.next.yml - 修改
_config.next.yml进行自定义配置
4. 推荐主题
自定义域名
1. 购买域名
- 在阿里云、腾讯云等平台购买域名
2. 添加CNAME文件
# 在source目录创建CNAME文件echo "你的域名.com" > source/CNAME3. 配置DNS
记录类型:CNAME主机记录:www记录值:用户名.github.io4. 更新GitHub Pages设置
- 进入仓库Settings → Pages
- 在Custom domain填入你的域名
- 勾选Enforce HTTPS
故障排除
常见问题
-
页面404错误
- 检查仓库名是否正确
- 等待几分钟让GitHub Pages生效
-
部署失败
Terminal window # 检查Git配置git config --list# 重新安装部署插件npm uninstall hexo-deployer-gitnpm install hexo-deployer-git -
样式丢失
Terminal window # 清理缓存重新生成hexo clean && hexo g
获取帮助
- Hexo官方文档:https://hexo.io/zh-cn/docs/
- GitHub Pages文档:https://docs.github.com/pages
- 主题文档:查看各主题的GitHub仓库
后续优化建议
1. SEO优化
- 添加sitemap插件
- 配置robots.txt
- 添加Google Analytics
2. 功能增强
- 安装评论系统(评论系统类型: twikoo, waline, giscus(github discussions), disqus, artalk都很不错)
- 添加搜索功能
- 配置RSS订阅
3. 自动化部署
- 使用GitHub Actions自动部署
- 配置Travis CI持续集成
提示:每次修改配置后,记得运行hexo clean && hexo g && hexo d重新部署。
支持与分享
如果这篇文章对你有帮助,欢迎分享给更多人或赞助支持!
Hexo+GithubPages部署博客
https://grushtom.github.io/posts/hexo/
GrushTomの部屋