Greenhats の Blog.

Hexo 常用命令

2025/02/01
loading

Hexo 常用命令

Hexo静态博客框架的常用命令整理,按功能分类说明:


项目初始化

  1. 安装Hexo

    1
    npm install -g hexo-cli  # 全局安装Hexo命令行工具
  2. 创建新博客

    1
    2
    3
    hexo init myblog         # 初始化博客项目
    cd myblog # 进入项目目录
    npm install # 安装依赖包

写作与生成

  1. 新建文章

    1
    2
    3
    hexo new "文章标题"       # 在`source/_posts`生成Markdown文件
    hexo new draft "草稿标题" # 创建草稿(生成到`source/_drafts`)
    hexo publish "草稿标题" # 将草稿移动到正式文章目录
  2. 生成静态文件

    1
    2
    hexo generate            # 生成静态文件到`public`目录(简写:`hexo g`)
    hexo g --watch # 监听文件变动并自动重新生成

服务器管理

  1. 启动本地服务器
    1
    2
    3
    4
    hexo server              # 启动服务(默认端口4000,简写:`hexo s`)
    hexo s -p 8080 # 指定端口启动
    hexo s --draft # 预览时包含草稿文章
    sudo rsync -av /root/hexo/public/ /var/www/hexo/public/ #移动静态网页到nginx的网站目录

部署与发布

  1. 一键部署(需配置)

    1
    2
    hexo deploy              # 部署到远程仓库(如GitHub Pages,简写:`hexo d`)
    hexo d --generate # 生成后立即部署(等同于 `hexo g -d`)
  2. 手动部署(直接推送)

    1
    2
    3
    4
    # 生成后手动提交到Git仓库
    git add .
    git commit -m "更新博客"
    git push origin main

主题管理

  1. 安装主题

    1
    2
    git clone https://github.com/theme-name/theme.git themes/theme-name
    # 然后在`_config.yml`中设置:`theme: theme-name`
  2. 更新主题

    1
    2
    cd themes/theme-name
    git pull

插件与配置

  1. 安装插件

    1
    npm install hexo-plugin-name --save  # 例如:hexo-deployer-git
  2. 修改全局配置

    1
    2
    3
    4
    5
    6
    7
    # 编辑根目录下的 _config.yml
    title: 我的博客
    subtitle: 记录技术与生活
    deploy:
    type: git
    repo: [email protected]:username/username.github.io.git
    branch: main

维护与清理

  1. 清除缓存

    1
    hexo clean               # 清除缓存和生成的`public`文件
  2. 重置数据库

    1
    2
    3
    # 删除`db.json`文件后重新生成
    rm db.json
    hexo generate

其他实用命令

  1. 列出所有文章

    1
    hexo list post          # 显示所有已发布文章
  2. 查看Hexo版本

    1
    hexo version            # 或 `hexo v`
  3. 自定义生成器

    1
    hexo generate --config custom.yml  # 使用自定义配置文件

配置文件示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# _config.yml 关键配置项示例
title: Hexo Blog
subtitle: A Static Blog Framework
description: 欢迎访问我的技术博客!
keywords: 技术,前端,Node.js
author: Your Name
language: zh-CN
timezone: Asia/Shanghai

# 部署配置(需安装 hexo-deployer-git)
deploy:
type: git
repo:
github: [email protected]:username/username.github.io.git
branch: main

# 主题配置
theme: landscape

常用工作流

  1. 本地写作与预览

    1
    2
    hexo new "Hello World"   # 新建文章
    hexo server --draft # 启动服务(包含草稿)
  2. 发布到GitHub Pages

    1
    hexo clean && hexo g -d  # 清理缓存、生成并部署

CATALOG
  1. 1. Hexo 常用命令
    1. 1.0.1. 项目初始化
    2. 1.0.2. 写作与生成
    3. 1.0.3. 服务器管理
    4. 1.0.4. 部署与发布
    5. 1.0.5. 主题管理
    6. 1.0.6. 插件与配置
    7. 1.0.7. 维护与清理
    8. 1.0.8. 其他实用命令
    9. 1.0.9. 配置文件示例
    10. 1.0.10. 常用工作流