Hexo搭建个人博客教程

环境配置

环境名称 环境版本 命令 官网
Git 2.42.0 git --version https://git-scm.com/download/win
Node.js 18.18.0 node -v https://nodejs.org/zh-cn/

安装Hexo框架

  • 安装 cnpm 工具
1
npm install -g cnpm --registry==https://registry.npm.taobao.org
  • 安装 Hexo
1
cnpm install -g hexo-cli
  • 查看 Hexo 版本
1
hexo -v

搭建博客

  1. 创建存放 Hexo 项目的文件夹,在文件夹内右键选择 Open Git Bash here

  2. 输入命令 hexo init,进行项目初始化

    image-20240924153541810

  3. 输入命令 hexo s 开启本地预览服务,打开浏览器访问 http://localhost:4000 即可看到博客内容

  4. 更换主题

    1. 官方网址: Themes | Hexo

    2. 选择一款自己喜欢的主题,点击链接跳转至 github 页面

      image-20240924170106568

    3. 复制主题的http链接和主题名

      image-20240924170143311

    4. 在项目文件夹下的 git 窗口中

      执行命令 git clone https://github.com/fluid-dev/hexo-theme-fluid.git themes/fluid

      image-20240924170229541

    5. 修改 _config.yml 文件

      image-20240924170254639

    6. 预览效果

      1
      2
      3
      hexo clean //执行此命令后继续下一条
      hexo g //生成博客目录
      hexo s //本地预览

      配置指南:配置指南 | Hexo Fluid 用户手册 (fluid-dev.com)

部署至Github Page

默认已有 Github 账号

  1. 使用 git 设置 github 的用户名和邮箱

    1
    2
    git config --global user.name "GitHub 用户名"
    git config --global user.email "GitHub 邮箱"
  2. 创建添加密钥,一路回车

    1
    ssh-keygen -t rsa -C "GitHub 邮箱"
  3. 复制 id_rsa.pub 文件中的内容

    image-20240925093411581
  4. Github 中创建SSH Key

    image-20240925100731665

  5. 验证是否成功 ssh -T git@github.com

    image-20240925110035432
  6. 安装部署工具 npm install hexo-deployer-git --save

  7. Github 上创建一个仓库,命名格式:用户名.github.io

    image-20240925110449357
  8. 修改 _config.yml 文件

    image-20240925110154243
  9. 在git中执行命令 hexo d

  10. 在浏览器输入网址:用户名.github.io ,即可自动补全https

在后续更新博客,想要部署上线时,执行 hexo d -g 命令,重新编译项目

进阶玩法

网页访问统计

打开 https://tongji.baidu.com/sc-web/10000033910/home/site/getjs?siteId=13751376

在网站列表页新增网站

image-20240924191809599

在代码获取页复制百度的 ID

image-20240924191939530

添加到 _config.yml

image-20240924192025066

安装插件,检查百度是否成功检测自己的网站

​ 网址:https://chromewebstore.google.com/detail/library-sniffer/fhhdlnnepfjhlhilgmeepgkhjmhhhjkh

image-20240924192207520

UV、PV统计

使用不蒜子进行统计,不需要申请账号,直接开启即可

不蒜子 - 极简网页计数器 (ibruce.info)

引入不蒜子

header.ejs

1
<script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>

添加标签

footer.ejs

1
2
3
4
5
6
<span id="busuanzi_container_site_pv">
本站总访问量<span id="busuanzi_value_site_pv"></span>
</span>
<span id="busuanzi_container_site_uv">
本站访客数<span id="busuanzi_value_site_uv"></span>人次
</span>

优化建议

修改文章模板

文章的默认模板位置在此处:

image-20240924151722109

文章的默认模板是:

image-20240924151835509

我的模板:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
---
title:{{ title }}
date:{{ date }}
tags:[]
categories:环境搭建
index_img:/img/default.jpg
banner_img:/img/default.jpg
excerpt:这是摘要
hide:false
archive:false
sticky:0
description:
---

<!--more-->

冒号后面不能有空格!!!


Hexo搭建个人博客教程
http://example.com/2024/09/24/环境搭建/Hexo搭建个人博客教程/
作者
王承磊
发布于
2024年9月24日
许可协议