笔者一直以来都习惯于在稀土掘金 平台进行技术博文的分享和记录。掘金作为一个独立运营的第三方平台,确实提供了优质的技术交流环境和丰富的资源,对技术人来说是一片很好的内容创作天地。然而,随着使用时间的增加,笔者开始意识到自己在数据安全方面的隐患以及平台依赖的问题。
首先,掘金目前并没有提供历史文章的导出功能,这意味着用户的所有创作内容都被深深绑定在平台之上,一旦平台无法正常运行或出现类似删库跑路这样的极端情况,用户可能就会面临失去所有文章的风险。此外,如果管理员误删数据、服务器故障导致数据丢失等问题出现,则笔者之前所有的心血可能会付之东流。这种“不受控制”的内容存储方式让我感到缺乏安全感和预见性。
因此,笔者回忆起自己之前曾研究过的个人博客搭建方式,并决定重新捡起基于 Hexo 的博客搭建工具,来构建一个完全属于自己的内容创作和存储空间。在拥有个人博客的基础上,不仅可以保持创作自由,同时也能确保数据的安全和持久性。接下来,本文将详细记录 Hexo 博客的搭建过程,希望对有相同需求的朋友有所帮助。
环境推荐
本地环境:Windows11
服务器环境:RockyLinux 8.10(Centos系列)
服务器环境搭建
为了方便我直接使用了root用户进行操作,如果想要使用其他用户进行搭建要主要权限问题哦!
安装git环境
安装nodejs与pnpm环境 打开node.js-download 这个链接,选择符合自己的系统环境复制命令到终端进行下载即可。
安装nginx环境 1 2 3 4 5 6 7 8 # 通过yum安装nginx sudo yum install -y nginx # 启动nginx开机自启 sudo systemctl enable nginx # 启动nginx服务 sudo systemctl start nginx # 查看nginx状态 sudo systemctl status nginx
创建博客目录与git钩子
创建git钩子是为了当本地环境执行hexo d
时可以实时更新服务器的博文内容
1 2 3 4 5 6 7 8 9 # 创建博客目录 mkdir -p /root/document/blog # 创建git钩子 cd /root/document && git init --bare hexo.git sudo tee /root/document/hexo.git/hooks/post-receive <<'EOF' git --work-tree=/root/document/blog --git-dir=/root/document/hexo.git checkout -f EOF # 基于钩子文件执行权限 sudo chmod +x /root/document/hexo.git/hooks/post-receive
配置nginx代理
修改/etc/nginx/nginx.conf
文件
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 user root; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; # Load dynamic modules. See /usr/share/doc/nginx/README.dynamic. include /usr/share/nginx/modules/*.conf; events { worker_connections 1024; } http { log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; include /etc/nginx/mime.types; default_type application/octet-stream; include /etc/nginx/conf.d/*.conf; }
新增/etc/nginx/conf.d/blog.conf
配置文件
如果仅有公网ip、没有域名的情况,配置文件内容如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 server { listen 80; listen [::]:80; # 配置为自己的公网IP server_name 106.14.19.58; location / { # 配置为服务器中个人博客的目录 root /root/document/blog; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } }
如果有域名、且有SSL证书的情况,配置文件内容如下
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 server { listen 80; listen [::]:80; # 配置为自己的域名 server_name liboshuai.icu; rewrite ^(.*)$ https://${server_name}$1 permanent; } server { listen 443 ssl; # 配置为自己的域名 server_name liboshuai.icu; # 配置为自己ssl证书pem文件的路径 ssl_certificate /etc/nginx/ssl/liboshuai.icu.pem; # 配置为自己ssl证书key文件的路径 ssl_certificate_key /etc/nginx/ssl/liboshuai.icu.key; ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; location / { proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $http_host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 配置为服务器中个人博客的目录 root /root/document/blog; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } }
重新加载nginx配置
1 2 3 4 # 检查nginx配置文件是否正确 /usr/sbin/nginx -t # 热加载nginx配置文件 /usr/sbin/nginx -s reload
本地环境搭建 安装git环境 打开git-download 链接,选择符合自己的系统点击进行下载即可,随后打开安装包一直进行下一步即可。
随后配置git全局变量
1 2 3 4 # 修改为自己的邮箱 git config --global user.email "liboshuai01@gmail.com" # 修改为自己的英文名 git config --global user.name "BoShuai Li"
安装nodejs与yarn环境 打开node.js-download 这个链接,选择符合自己的系统环境复制命令到终端进行下载安装即可。
安装hexo环境
新建一个目录用于存储个人博客数据,例如:C:\Me\Project\other
。
在新建的目录中打开git bash
终端,执行下面的命令。
一定要在C:\Me\Project\other
目录下执行
1 2 3 4 5 6 7 8 9 10 # 全局安装hexo-cli yarn install -g hexo-cli # 创建并进入博客目录 hexo init blog && cd blog # 安装Hexo项目依赖 yarn install # 安装deploy命令支持 yarn add hexo-deployer-git # 启动Hexo本地服务 hexo s
下面是安装结束后的目录结构
在浏览器打开http://localhost:4000
查看页面
安装icarus主题
一定要在C:\Me\Project\other\\blog
目录下执行
1 2 3 4 5 6 # 通过yarn安装icarus主题 yarn add hexo-theme-icarus hexo-renderer-inferno # 配置hexo当前主题为icarus hexo config theme icarus # 重新启动hexo验证主题是否生效 hexo s
icarus主题生效后,目录中多出了_config.icarus.yml
这个文件
在浏览器打开http://localhost:4000
查看页面
配置远程部署地址,修改_config.yml
文件中的deploy
项
注意:我的ssh端口为22222,而非22
1 2 3 4 deploy: type: 'git' repo: ssh://root@106.14.19.58:22222/root/document/hexo.git branch: master
配置本地SSH免密登录服务器(使用git bash)
1 2 3 4 5 # 生成密钥,随后一路回车 ssh-keygen -t rsa # 这一步的目的是将本地生产的`id_rsa.pub`文件的内容拷贝到服务器上的`~/.ssh/authorized_keys`文件中 # 当然也可以自己手动将生成的`id_rsa.pub`文件内容拷贝到服务器上的`~/.ssh/authorized_keys`文件中 ssh -p 22222 root@106.14.19.58 "cat >> ~/.ssh/authorized_keys" < C:/Users/libos/.ssh/id_rsa.pub
现在需要测试一下本地是否可以免密登录到服务器了
1 ssh -p 22222 root@106.14.19.58
发布博客内容到服务器
浏览器打开https://liboshuai.icu
链接访问博客页面,进行查看
主题美化
_config.yml
文件内容如下
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 title: 技术博客 subtitle: '李博帅的技术博客' description: '记录自己的技术成长' keywords: author: 李博帅 language: zh-CN timezone: 'Asia/Shanghai' url: https://liboshuai.icu permalink: '/pages/:title/' permalink_defaults: pretty_urls: trailing_index: true trailing_html: true source_dir: source public_dir: public tag_dir: tags archive_dir: archives category_dir: categories code_dir: downloads/code i18n_dir: :lang skip_render: new_post_name: :title.md default_layout: post titlecase: false external_link: enable: true field: site exclude: '' filename_case: 0 render_drafts: false post_asset_folder: false relative_link: false future: true syntax_highlighter: highlight.js highlight: line_number: true auto_detect: false tab_replace: '' wrap: true hljs: false prismjs: preprocess: true line_number: true tab_replace: '' index_generator: path: '' per_page: 10 order_by: -date default_category: uncategorized category_map: tag_map: meta_generator: true date_format: YYYY-MM-DD time_format: HH:mm:ss updated_option: 'mtime' per_page: 10 pagination_dir: page include: exclude: ignore: theme: icarus deploy: type: 'git' repo: ssh://root@106.14.19.58:22222/root/document/hexo.git branch: master
_config.icarus.yml
配置文件内容如下
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 version: 5.1 .0 variant: default logo: https://lbs-images.oss-cn-shanghai.aliyuncs.com/202503181853491.svg head: favicon: https://lbs-images.oss-cn-shanghai.aliyuncs.com/202503181853491.svg manifest: name: short_name: start_url: theme_color: background_color: display: standalone icons: - src: '' sizes: '' type: open_graph: title: type: blog url: image: site_name: author: description: twitter_card: twitter_id: twitter_site: google_plus: fb_admins: fb_app_id: structured_data: title: description: url: author: publisher: publisher_logo: image: meta: - '' rss: navbar: menu: 首页: / 归档: /archives 分类: /categories 标签: /tags 关于: /about links: Download on GitHub: icon: fab fa-github url: https://github.com/liboshuai01 footer: copyright: © 2025 links: article: highlight: theme: atom-one-light clipboard: true fold: unfolded readtime: true update_time: true licenses: Creative Commons: icon: fab fa-creative-commons url: https://creativecommons.org/ Attribution: icon: fab fa-creative-commons-by url: https://creativecommons.org/licenses/by/4.0/ Noncommercial: icon: fab fa-creative-commons-nc url: https://creativecommons.org/licenses/by-nc/4.0/ search: type: insight index_pages: true sidebar: left: sticky: false right: sticky: false widgets: - position: left type: profile author: 李博帅 author_title: Java Developer location: Shang Hai avatar: https://lbs-images.oss-cn-shanghai.aliyuncs.com/202503181739528.jpg avatar_rounded: true gravatar: follow_link: https://github.com/liboshuai01 social_links: Github: icon: fab fa-github url: https://github.com/liboshuai01 - position: left type: toc index: true collapsed: true depth: 3 - position: left type: links links: Github: https://github.com/liboshuai01 Gitee: https://gitee.com/liboshuai01 掘金: https://juejin.cn/user/1878395516357111 - position: left type: categories - position: left type: recent_posts - position: left type: archives - position: left type: tags order_by: name amount: 20 show_count: true plugins: animejs: true back_to_top: true baidu_analytics: tracking_id: bing_webmaster: tracking_id: busuanzi: false cnzz: id: web_id: cookie_consent: type: info theme: edgeless static: false position: bottom-left policyLink: https://www.cookiesandyou.com/ gallery: true google_analytics: tracking_id: hotjar: site_id: katex: false mathjax: false outdated_browser: false pjax: true progressbar: true statcounter: project: security: twitter_conversion_tracking: pixel_id: providers: cdn: jsdelivr fontcdn: google iconcdn: fontawesome
hexo常用命令 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 # 初始化站点,生成一个简单网站所需的各种文件。 hexo init # 清除缓存 网页正常情况下可以忽略此条命令(简写 hexo c) hexo clean # 新建文章 hexo new "postName" # 新建页面 hexo new page "pageName" # 生成静态页面至public目录 简写:hexo g hexo generate # 开启预览访问端口(默认端口4000,'ctrl + c' 关闭server) 简写:hexo s,可用--debug hexo server # 将.deploy目录部署到GitHub 简写:hexo d hexo deploy # 组合命令,先生成页面,后部署到服务器 hexo d -g
结语 这样就可以在本地编写博文,然后快速部署到服务器中。
参考: https://www.cnblogs.com/cheyaoyao/p/17836522.html