本地博客搭建教程

需求分析及工作原理

近期由于网络的原因需要在本地搭建博客,最初的方案是借用Github Pages 实现了自定义博客,但是并非所有的地方都能访问Github, 因此在这些地方可以采用本地博客的方式,实现原理为:

  • Github博客源码库同步到Gitee, 因为Gitee属于国内的开发平台,所认其速度非常快。
  • Gitee上的源码hexosurce克隆到本地。
  • 如果本机可以访问Github, 则同步克隆其上源码到本地。
  • 编辑博客时,其编辑的是Gitee上克隆下来的源码,如果本机不可访问外网,则启动本机http服务,直接访问 http://localhost:4000
  • 编辑博客时,其编辑的是Gitee上克隆下来的源码,如果本机可访问外网,则启动本机http服务,直接访问 http://localhost:4000 , 同时,将编辑好的源码复制到Github库一份,并PushGithub上,实现同步备份之目的。

使用 Python 内置服务器本地托管博客

安装 Python3

1
sudo pacman -S python3

创建服务文件

/etc/systemd/system/blog.service
1
2
3
4
5
6
7
8
9
10
11
12
13
[Unit]
Description=Hexo Blog Static Server
After=network.target

[Service]
Type=simple
User=你的用户名
WorkingDirectory=/home/你的用户名/hexo-blog/public # 直接指向 public 目录
ExecStart=/usr/bin/python3 -m http.server 4000 --bind 127.0.0.1
Restart=always

[Install]
WantedBy=multi-user.target

重要

  • WorkingDirectory 必须是博客根目录(即包含 public 文件夹的目录),而不是 public 本身。

  • User 建议填写你的普通用户名,不要用 root

重新加载并启动

1
2
3
sudo systemctl daemon-reload
sudo systemctl start blog
sudo systemctl status blog

如果状态为 active (running),即可通过 http://localhost:4000 访问。