Hexo 博客搭建 1 Hexo 介绍 Hexo 官网地址:https://hexo.io/zh-cn/
Hexo 是一个基于 Node.js 构建的静态博客生成框架。它允许用户使用 Markdown 或其他标记语言编写文章,并通过简单的命令将这些文章转换为静态网页,从而可以轻松地创建个人博客或网站。Hexo 的主要特点包括:
静态网页生成 : Hexo 将文章和页面内容转换为静态 HTML 文件,这意味着访问者在浏览时不需要执行服务器端的代码,从而加快页面加载速度。
Markdown 支持 : 用户可以使用 Markdown 语法来编写文章,这是一种轻量级的标记语言,易于学习和使用。
主题和插件 : Hexo 支持各种主题和插件,用户可以选择适合自己风格的主题,同时可以利用插件来增强博客的功能。
简单命令行操作 : Hexo 提供了一系列简单的命令,如创建新文章、生成静态页面、部署到远程服务器等,使用户可以轻松管理和维护博客。
版本控制和备份 : 由于 Hexo 生成的是静态文件,您可以将博客内容存储在版本控制系统中,如 Git,以便轻松地管理内容版本和进行备份。
2 Hexo 安装 安装 Hexo 相当简单,只需要先安装下列应用程序即可(对应官网下载安装包即可):
Node.js (Node.js 版本需不低于 10.13,建议使用 Node.js 12.0 及以上版本)
Git
上述应用安装完成后,替换 npm 镜像源:
1 npm config set registry https://registry.npmmirror.com
替换完成后,即可使用 npm 安装 Hexo:
3 Hexo 建站 3.1 本地部署 3.1.1 Hexo 快速搭建 安装 Hexo 完成后,请执行下列命令,Hexo 将会在指定文件夹中新建所需要的文件(在任意编辑器中打开 Git Bash 即可):
1 2 3 4 5 6 7 8 # 由于未知的网络问题,可能会出现代码拉取不下来的情况这里你懂的 git config --global http.proxy "http://127.0.0.1:7897" git config --global https.proxy "https://127.0.0.1:7897" # 创建并初始化文件夹 hexo init hexo-blog # 这里需要注意,如果使用 WebStorm 需要先初始化在打开
在 hexo-blog 目录中安装依赖:
3.1.2 Hexo 常用命令
命令
说明
hexo init [folder]
新建并初始化一个文件夹
hexo new [layout] <title>
新建一篇文章
hexo generate [-d]
生成静态文件
hexo server
启动服务器,在默认情况下访问网址为: http://localhost:4000/
hexo deploy
部署网站(上传)
hexo clean
清除缓存文件 (db.json) 和已生成的静态文件 (public)
快速启动查看效果:
1 hexo generate ; hexo server
3.2 GitHub 部署
前提:注册一个 GitHub 账号。
3.2.1 新建仓库 Hexo 仓库名格式:GitHub_Name.github.io(固定死了),其他默认即可。
3.2.2 配置 Git 配置 git 用户名,最好与 github 保持一致:
1 git config --global user.name "Y0n9zh"
配置 git 邮箱:
1 git config --global user.email "Y0n9zh@gmail.com"
3.2.3 配置 Hexo 打开 hexo-blog 目录下的 _config.yml 文件,配置如下内容:
1 2 3 4 deploy: type: 'git' repo: 'https://github.com/Y0n9zh/Y0n9zh.github.io.git' branch: main
3.2.4 推送 GitHub 安装 hexo 和 github 部署的插件:
1 npm install hexo-deployer-git --save
一条命令推送(如果没做免密,会要求输入 GitHub 账号密码):
1 hexo clean ; hexo generate ; hexo deploy
选择用于展示的分支:
访问地址:https://y0n9zh.github.io/
4 Hexo 配置文件说明 4.1 核心配置与依赖相关
文件 / 文件夹
说明
_config.yml
Hexo 博客的全局配置文件 ,用于设置站点标题、描述、作者、部署方式、主题相关配置等核心内容,是整个博客项目的关键配置入口。
package.json
项目依赖的包
package-lock.json
锁定依赖包的具体版本
node_modules
存放项目依赖的 Node.js 包,执行 npm install 时下载的各类包(如 Hexo 本身、各种插件 )都在这里,一般无需手动操作该文件夹,但项目运行依赖它。
4.2 博客内容与生成相关
文件 / 文件夹
说明
scaffolds
脚手架(模板)目录,里面是新建文章、页面等时的默认模板文件,可自定义模板格式,让每次新建内容都自带固定的前置内容。
source
存放博客的原始内容 ,比如 Markdown 格式的博客文章、页面文件,以及图片、音频等静态资源,Hexo 会读取这里的内容进行处理生成静态页面。
public
Hexo 执行 hexo generate 命令后,生成的静态网页文件 会存放在这里,这些就是最终部署到服务器上的内容,一般无需手动修改,由 Hexo 自动生成。
themes
存放博客使用的主题 ,每个主题是一个独立文件夹,主题里包含布局文件、样式文件、脚本文件等,可更换、自定义主题来改变博客外观和功能。
_config.landscape.yml
如果使用 landscape 主题(Hexo 默认主题 ),这是该主题的专属配置文件 ,用于覆盖主题默认设置。
4.3 辅助与部署相关
文件 / 文件夹
说明
.deploy_git
当使用 hexo-deployer-git 部署插件,执行 hexo deploy 时,可能会生成的临时目录,用于存放待推送到远程仓库的文件,和部署流程相关,一般无需手动干预。
.github
用于存放 GitHub 相关的配置,若有自动化部署、CI / CD 需求,会在这里配置脚本。
.gitignore
Git 版本控制的忽略文件配置,指定哪些文件或文件夹不需要被 Git 跟踪,避免把冗余文件提交到 GitHub 仓库。
db.json
Hexo 用于缓存内容数据 ,记录文章、页面的元信息,辅助 Hexo 快速生成静态页面,一般不用手动修改,Hexo 会自动维护。
5 Butterfly 主题安装 Hexo 官方主题地址:https://hexo.io/themes/
Butterfly 主题地址:https://github.com/jerryc127/hexo-theme-butterfly
Butterfly 配置教程地址:https://butterfly.js.org/
安装命令如下(Git):
1 git clone -b master https://github.com/jerryc127/hexo-theme-butterfly.git themes/butterfly
安装相关插件:
1 npm install hexo-renderer-pug hexo-renderer-stylus --save
修改 _config.yml 配置文件:
查看结果:
6 Butterfly 主题配置 6.1 快速编辑 Butterfly 配置 将 themes/butterfly/_config.yml 复制一份到 hexo-blog 目录下,并重命名为 _config.butterfly.yml,后续可以直接修改该文件内容:
1 2 3 4 5 # 复制 butterfly 配置文件 cp themes/butterfly/_config.yml _config.butterfly.yml # 删除 landscape 默认的配置文件 rm -f _config.landscape.yml
6.2 Front-matter 参数配置 Front-matter 是 markdown 文件最上方以 --- 分隔的区域,用于定义文章的元数据。
Page Front-matter:用于页面配置
Post Front-matter:用于文章页配置
书写示例如下(Hexo 官方):
1 2 3 4 --- title: Hello World date: 2013 /7/13 20 :46:25 ---
通常而言,一个 Page Front-matter 需要包含如下几部分内容:
参数
解释
title
页面标题
date
页面创建日期
type
标签、分类和友情链接三个页面需要配置
而 Post Front-matter 就比较多了:
参数
解释
title
页面标题
date
页面创建日期
updated
页面更新日期(太麻烦了我也没加)
categories
文章分类(建议不超过两个)
top_img
文章顶部图片
Post Front-matter 示例如下:
1 2 3 4 5 6 7 --- title: Hexo 博客搭建 date: 2025-08-20 20:23:04 categories: - Hexo top_img: ./img/post_img/page_img1.jpg ---
6.3 创建分类页
注意:这里之所以不创建标签页和友情链接页是因为我认为后期管理太麻烦了。
在 hexo-blog 目录下,执行如下命令:
1 2 # 创建分类页 hexo new page categories
将生成的 index.md 文件内容进行修改:
1 2 3 4 5 6 # source /categories/index.md--- title: 文章分类 date: 2025-08-20 19:09:44 type: "categories" ---
6.4 配置 404 页面 Butterfly 主题内置了一个简单的 404 页面,可在设置中开启:
1 2 3 4 5 error_404: enable: true subtitle: 'Page Not Found' background: /img/error-page.jpg
这里有两点需要注意:
6.5 修改默认配置文件
注意:仅供参考。
内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 nav: logo: display_title: true display_post_title: true fixed: true menu: 首页: / || fas fa-home 文章 || fas fa-archive || hide: 分类: /categories/ || fas fa-folder-open 归档: /archives/ || fas fa-archive ...... code_blocks: theme: light macStyle: true height_limit: false word_wrap: false copy: true language: true shrink: false fullpage: false social: fas fa-envelope: mailto:y0n9zh@gmail.com || Email || '#000000' fa-solid fa-paperclip: https://www.yuque.com/yongz || Yueque || '#000000' favicon: ./img/favicon.ico avatar: img: ./img/avatar.jpg effect: false disable_top_img: false default_top_img: ./img/default_top_img.jpg index_img: archive_img: tag_img: tag_per_img: category_img: category_per_img: footer_img: false background: cover: index_enable: true aside_enable: true archives_enable: true position: both default_cover: - ./img/default_cover.jpg error_img: flink: ./img/404.jpg post_page: ./img/404.jpg error_404: enable: true subtitle: 'Page Not Found' background: ./img/404.jpg post_meta: page: date_type: created date_format: date categories: true tags: false label: false post: position: center date_type: created date_format: date categories: true tags: false label: false index_site_info_top: index_top_img_height: subtitle: enable: true effect: true typed_option: source: 3 sub: index_layout: 1 index_post_content: method: 3 length: 500 toc: post: true page: false number: true expand: true style_simple: false scroll_percent: true post_copyright: enable: false decode: false author_href: Yongz丶 license: CC BY-NC-SA 4.0 license_url: https://creativecommons.org/licenses/by-nc-sa/4.0/ reward: enable: false text: QR_code: post_edit: enable: false url: related_post: enable: true limit: 6 date_type: created post_pagination: 1 noticeOutdate: enable: false style: flat limit_day: 365 position: top message_prev: 已经过了 message_next: 天自上次更新,文章内容可能已过时。 footer: nav: owner: enable: true since: 2023 custom_text: <a href="https://beian.miit.gov.cn/#/Integrated/index" style="color:white" target="_blank">闽ICP备2024058441号-1</a> | 免责声明:请勿利用文章内的相关技术从事非法测试,如因此产生的一切不良后果与本人无关 copyright: enable: true version: true aside: enable: true hide: false button: true mobile: true position: right display: archive: true tag: true category: true card_author: enable: true description: button: enable: true icon: fab fa-github text: Follow Me link: https://github.com/Y0n9zh?tab=repositories card_announcement: enable: true content: 不定时分享一些内容... card_recent_post: enable: true limit: 5 sort: date sort_order: card_newest_comments: enable: false sort_order: limit: 6 storage: 10 avatar: true card_categories: enable: true limit: 8 expand: none sort_order: card_tags: enable: false limit: 40 color: false orderby: random order: 1 sort_order: card_archives: enable: true type: monthly format: MMMM YYYY order: -1 limit: 8 sort_order: card_post_series: enable: true series_title: false orderBy: 'date' order: -1 card_webinfo: enable: true post_count: true last_push_date: true sort_order: runtime_date: 8 /29/2023 00 :00:00 rightside_bottom: translate: enable: false default: 繁 defaultEncoding: 2 translateDelay: 0 msgToTraditionalChinese: '繁' msgToSimplifiedChinese: '简' readmode: true darkmode: enable: true button: true autoChangeMode: false start: end: rightside_scroll_percent: false rightside_item_order: enable: false hide: show: rightside_config_animation: true anchor: auto_update: false click_to_scroll: false photofigcaption: false copy: enable: true copyright: enable: false limit_count: 150 wordcount: enable: true post_wordcount: true min2read: true total_wordcount: true busuanzi: site_uv: true site_pv: true page_pv: true math: use: per_page: true hide_scrollbar: false mathjax: enableMenu: true tags: none katex: copy_tex: false search: use: local_search algolia_search: hitsPerPage: 6 local_search: preload: true top_n_per_article: 1 unescape: false CDN: docsearch: appId: apiKey: indexName: option: share: use: sharejs sharejs: sites: facebook,twitter,wechat,weibo,qq addtoany: item: facebook,twitter,wechat,sina_weibo,facebook_messenger,email,copy_link comments: use: text: true lazyload: true count: false card_post_count: false disqus: shortname: apikey: disqusjs: shortname: apikey: option: livere: uid: gitalk: client_id: client_secret: repo: owner: admin: option: valine: appId: appKey: avatar: monsterid serverURLs: bg: visitor: false option: waline: serverURL: bg: pageview: false option: utterances: repo: issue_term: pathname light_theme: github-light dark_theme: photon-dark facebook_comments: app_id: user_id: pageSize: 10 order_by: social lang: zh_TW twikoo: envId: region: visitor: false option: giscus: repo: repo_id: category_id: theme: light: light dark: dark option: remark42: host: siteId: option: artalk: server: site: visitor: false option: chat: use: rightside_button: false button_hide_show: false chatra: id: tidio: public_key: crisp: website_id: baidu_analytics: google_analytics: cloudflare_analytics: microsoft_clarity: umami_analytics: enable: false serverURL: website_id: option: UV_PV: site_uv: false site_pv: false page_pv: false token: google_tag_manager: tag_id: domain: google_adsense: enable: false auto_ads: true js: https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js client: enable_page_level_ads: true ad: index: aside: post: site_verification: category_ui: tag_ui: text_align_justify: false mask: header: true footer: true preloader: enable: false source: 1 pace_css_url: enter_transitions: true display_mode: light beautify: enable: false field: post title-prefix-icon: title-prefix-icon-color: font: global-font-size: code-font-size: font-family: code-font-family: blog_title_font: font_link: font-family: hr_icon: enable: true icon: icon-top: activate_power_mode: enable: false colorful: true shake: true mobile: false activate_power_mode: enable: false colorful: true shake: true mobile: false canvas_ribbon: enable: false size: 150 alpha: 0.6 zIndex: -1 click_to_change: false mobile: false canvas_fluttering_ribbon: enable: false mobile: false canvas_nest: enable: false color: '0,0,255' opacity: 0.7 zIndex: -1 count: 99 mobile: false fireworks: enable: false zIndex: 9999 mobile: false click_heart: enable: false mobile: false clickShowText: enable: false text: fontSize: 15px random: false mobile: false lightbox: series: enable: false orderBy: 'title' order: 1 number: true abcjs: enable: false per_page: true mermaid: enable: false code_write: false theme: light: default dark: dark chartjs: enable: false fontColor: light: "rgba(0, 0, 0, 0.8)" dark: "rgba(255, 255, 255, 0.8)" borderColor: light: "rgba(0, 0, 0, 0.1)" dark: "rgba(255, 255, 255, 0.2)" scale_ticks_backdropColor: light: "transparent" dark: "transparent" note: style: flat icons: true border_radius: 3 light_bg_offset: 0 pjax: enable: false exclude: aplayerInject: enable: false per_page: true snackbar: enable: false position: bottom-left bg_light: '#49b1f5' bg_dark: '#1f1f1f' instantpage: false lazyload: enable: false native: false field: site placeholder: blur: false pwa: enable: false manifest: apple_touch_icon: favicon_32_32: favicon_16_16: mask_icon: Open_Graph_meta: enable: true option: structured_data: true css_prefix: true inject: head: bottom: CDN: internal_provider: local third_party_provider: jsdelivr version: false custom_format: option: ......
安装所需插件:
1 2 npm install hexo-wordcount --save npm install hexo-generator-search --save
在 _config.yml 末尾添加:
1 2 3 4 5 search: path: search.json field: post format: html content: true
6.6 语言 修改 Hexo 的配置文件 _config.yml:
6.7 网站资料 Hexo 文章链接默认的生成规则是::year/:month/:day/:title,是按照年、月、日、标题来生成的。生成的链接非常长,如果标题中含中文的话,复制 URL 链接会转一大串编码字符。
安装 abbrlink 插件:
1 npm install hexo-abbrlink --save
修改网站各种资料,例如标题、副标题和邮箱等个人资料,请修改 Hexo 的 _config.yml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 title: 'Yongz丶' subtitle: '' description: '欲买桂花同载酒,终不似,少年游。' keywords: Blog author: 'Yongz丶' language: zh-CN timezone: 'Asia/Shanghai' url: https://yongz.fun/ permalink: posts/:abbrlink.html abbrlink: alg: crc32 rep: hex permalink_defaults: pretty_urls: trailing_index: true trailing_html: true
这里将页面都添加了 .html 的后缀,用来伪装成静态页面(虽说 Hexo 的页面本身就是静态页面),这样可以直接从路径就知道这是个静态页面,方便 SEO。
7 插件推荐 上述过程中已经安装的插件:
1 2 3 4 hexo-deployer-git hexo-wordcount hexo-generator-search hexo-abbrlink
还可以安装一下插件:
在 _config.yml 添加在末尾:
1 2 3 pangu: enable: true field: site