1448 字
7 分钟
Hexo 博客的部署方式

前言#

技术要点:Hexo+Github+Netlify+Cloudflare

部署参考

1.安装Node.js(已安装可跳过)#

Node.js官网
(可选)配置nodejs
如果是更新nodejs,直接下载安装包安装即可

2.安装Hexo#

Hexo官网
Hexo是一个全局插件,需要通过与他相关的命令来创建项目

npm install -g hexo-cli

2.5.创建项目#

通过命令生成项目文件夹,第一次用才安装,你可以拉取现成的配置仓库,再npm install

hexp init 项目名称
cd 项目名称
npm install

2.6.主要操作内容#

_config.yml文件 / package.json文件

2.7._config.yml文件使用说明#

全局配置文件,网站的很多信息都在这里配置,诸如网站名称,副标题,描述,作者,语言,主题,部署等等参数。

# Site
title: Hexo # 网站标题
subtitle: # 网站副标题
description: # 网站描述
author: John Doe # 作者
language: # 语言
timezone: # 网站时区。Hexo 默认使用您电脑的时区。时区列表。比如说:America/New_York, Japan, 和 UTC 。

# URL
## If your site is put in a subdirectory, set url as 'http://yoursite.com/child'
## and root as '/child/'
url: http://yoursite.com # 你的站点Url
root: / # 站点的根目录
permalink: :year/:month/:day/:title/ # 文章的 永久链接 格式
permalink_defaults: # 永久链接中各部分的默认值

# Directory
source_dir: source # 资源文件夹,这个文件夹用来存放内容
public_dir: public # 公共文件夹,这个文件夹用于存放生成的站点文件。
tag_dir: tags # 标签文件夹
archive_dir: archives # 归档文件夹
category_dir: categories # 分类文件夹
code_dir: downloads/code # Include code 文件夹
i18n_dir: :lang # 国际化(i18n)文件夹
skip_render: # 跳过指定文件的渲染,您可使用 glob 表达式来匹配路径。

# Writing
new_post_name: :title.md # 新文章的文件名称
default_layout: post # 预设布局
titlecase: false # 把标题转换为 title case
external_link: true # 在新标签中打开链接
filename_case: 0 # 把文件名称转换为 (1) 小写或 (2) 大写
render_drafts: false # 是否显示草稿
post_asset_folder: false # 是否启动 Asset 文件夹
relative_link: false # 把链接改为与根目录的相对位址
future: true # 显示未来的文章
highlight: # 内容中代码块的设置
  enable: true # 开启代码块高亮
  line_number: true # 显示行数
  auto_detect: false # 如果未指定语言,则启用自动检测
  tab_replace: # 用 n 个空格替换 tabs;如果值为空,则不会替换 tabs

# Category & Tag
default_category: uncategorized
category_map: # 分类别名
tag_map: # 标签别名

# Date / Time format
## Hexo uses Moment.js to parse and display date
## You can customize the date format as defined in
## http://momentjs.com/docs/#/displaying/format/
date_format: YYYY-MM-DD # 日期格式
time_format: HH:mm:ss # 时间格式

# Pagination
## Set per_page to 0 to disable pagination
per_page: 10 # 分页数量
pagination_dir: page # 分页目录

# Extensions
## Plugins: https://hexo.io/plugins/
## Themes: https://hexo.io/themes/
theme: landscape # 主题名称

# Deployment
## Docs: https://hexo.io/docs/deployment.html
#  部署部分的设置
deploy:
  type: # 类型,常用的git

3.生成文章并测试生成#

hexo new post "第一篇博客" // 会在 source/_posts/ 目录下生成文件 ‘第一篇博客.md’,打开编辑
hexo g // 生成静态HTML文件到 /public 文件夹中
hexo s // 本地运行server服务预览,打开 http://localhost:4000 即可预览你的博客

4.安装git(已安装可跳过)#

Git官网

4.1.配置github公钥私钥#

① 安装完git后输入,生成公钥私钥
邮箱要与后面git配置的邮箱一致

ssh-keygen -t rsa -C “邮箱”

② 查看公钥并配置到github中
公私钥生成路径: C:\Users\用户名\.ssh
id_rsa: 私钥
id_rsa.pub: 公钥
查看公钥 > 打开Github > Setting > SSH and GPG keys > New SSH key > 输入名称 > 输入公钥内容 > Add SSH key

③ 验证配置

ssh git@github.com

4.5.在github上创建仓库并将本地项目上传到github上#

第一次 上传到github:

git config --global user.name “用户名”
git config --global user.email “邮箱”
git init
git add .
git 次提交 -m "第一次提交"
git remote add origin ssh://git@ssh.github.com:443/Ctory-Nily/myhexo.git
git push -u origin main

更新本地仓库后上传:

git add .
git commit -m "更新信息"
git push -u origin main

第一次 拉取最新的github仓库:

git clone ssh://git@ssh.github.com:443/Ctory-Nily/myhexo.git
npm install

更新拉取最新的github仓库内容:

git pull origin main

5.将github项目托管到Netlify上#

Netlify官网

① 首先添加npm script,在 package.json中进行修改

...
scripts: {
  "build": "hexo generate",
  "clean": "hexo clean",
  "server": "hexo server",
  "netlify": "npm run clean && npm run build"
}
...

② 更新_config.yml中的域名为Netlify生成的域名

...
url: https://your-netify-hexo.netlify.app
...

③ 重新推送到仓库,此时Netlify的配置就部署好了

git add .
git commit -m "重新推送"
git push -u origin main

④ 在 Netlify 中选择新建项目,并选择此github项目, 然后就会自动部署了

6.购买域名#

7.使用Cloudflare对域名进行加速#

Cloudflare官网

8.本地自动更新脚本 “提交更新.bat”#

无论本地文件先修改还是后修改都可以同步上传上去,注意bat文件格式要为ANSI

@echo off
echo 正在同步github仓库
git pull origin main

echo 正在添加所有文件到暂存区
git add .

:: 格式化日期和时间
for /f "tokens=1-3 delims=/- " %%a in ("%date%") do (
    set year=%%a
    set month=%%b
    set day=%%c
)
for /f "tokens=1-3 delims=:.," %%a in ("%time%") do (
    set hour=%%a
    set minute=%%b
)

:: 去掉小时前面的空格(如果小时是单数)
set hour=%hour: =%

:: 组合成 "年 月 日 时 分" 格式
set formatted_time=%year%年%month%月%day%日%hour%时%minute%分

:: 询问是否自定义提交信息
:input_confirm
set /p confirm=是否自定义提交信息?(输入 y 或 n): 
if "%confirm%"=="" (
    echo 输入不能为空,请重新输入!
    goto input_confirm
)

if "%confirm%"=="y" (
    :input_msg
    set /p commit_msg=请输入提交信息: 
    if "%commit_msg%"=="" (
        echo 输入不能为空,请重新输入!
        goto input_msg
    )
    git commit -m "%commit_msg%"
) else (
    git commit -m "%formatted_time%"
)

echo 正在推送到远程仓库
git push -u origin main

echo 操作完成!
pause
Hexo 博客的部署方式
https://fuwari.vercel.app/posts/部署教程/hexoblog/hexo-博客的部署方式/
作者
Ctory-Nily
发布于
2025-02-08
许可协议
CC BY-NC-SA 4.0