一. 简介

基于debian系统和HEXO框架来创建个人博客,并使用nginx作为http server来发表博客。

二. 必要工具

  • Node.js
1
2
3
4
5
/* 安装Node.js */
$ sudo apt install nodejs

$ node -v
v18.13.0
  • npm
1
2
3
4
5
/* 安装npm */
$ sudo apt install npm

$ npm -v
9.2.0
  • cnpm
1
2
3
4
5
6
7
8
9
10
11
/* 使用淘宝源安装cnpm */
$ sudo npm install -g cnpm --registry=https://registry.npm.taobao.org

$ cnpm -v
cnpm@9.2.0 (/usr/local/lib/node_modules/cnpm/lib/parse_argv.js)
npm@9.9.2 (/usr/local/lib/node_modules/cnpm/node_modules/npm/index.js)
node@18.13.0 (/usr/bin/node)
npminstall@7.11.1 (/usr/local/lib/node_modules/cnpm/node_modules/npminstall/lib/index.js)
prefix=/usr/local
linux x64 6.1.0-13-amd64
registry=https://registry.npmmirror.com
  • hexo-cli
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/* 全局安装hexo-cli */
$ sudo cnpm install -g hexo-cli

$ hexo -v
hexo-cli: 4.3.1
os: linux 6.1.0-13-amd64 Debian GNU/Linux 12 (bookworm) 12 (bookworm)
node: 18.13.0
v8: 10.2.154.23-node.21
uv: 1.44.2
zlib: 1.2.13
brotli: 1.0.9
ares: 1.18.1
modules: 108
nghttp2: 1.51.0
napi: 8
llhttp: 6.0.10
uvwasi: 0.0.13
openssl: 3.0.7
cldr: 42.0
icu: 72.1
tz: 2022e
unicode: 15.0

三. 使用hexo生成博客

  • hexo init
    初始化博客框架
1
2
3
4
$ hexo init
INFO Cloning hexo-starter https://github.com/hexojs/hexo-starter.git
INFO Install dependencies
INFO Start blogging with Hexo!
  • 启动server
1
2
3
4
$ hexo s
INFO Validating config
INFO Start processing
INFO Hexo is running at http://localhost:4000/ . Press Ctrl+C to stop.

这个时候我们访问本地的4000端口,就已经可以看到hexo默认生成的一篇文章了。
hexo
这个文章中介绍了如何生成一编文章。

四. 本地实战

  • hexo new
1
2
3
4
5
6
7
8
$ hexo n "如何搭建个人博客"
INFO Validating config
INFO Created: ~/hexo/source/_posts/如何搭建个人博客.md

$ pwd
/home/zrf/hexo/source/_posts
$ ls
hello-world.md 如何搭建个人博客.md
  • 编写md文档

  • hexo生成

1
2
3
4
$ hexo clean
INFO Validating config
INFO Deleted database.
$ hexo generate
  • 再次启动服务
1
2
3
4
$ hexo s
INFO Validating config
INFO Start processing
INFO Hexo is running at http://localhost:4000/ . Press Ctrl+C to stop.

可以看到这里已经有一篇新的博客生成了
个人博客

五. 将博客放在个人网站上

通过之前的步骤,hexo框架会将我们自己编写的markdown格式的文章导出为html格式的文件。hexo server命令就相当于启动了启动了一个http server,监听本地的4000端口,如果有浏览器访问4000端口,就相当于请求public目录下的index.html文件。

本节我们将自己启动一个http server,把博客推送到这个服务器上,这样所有人都可以看到我们的博客了(这个server必须有一个公网IP)。

  • 安装nginx
1
2
3
4
5
$ sudo apt install nginx
nginx is already the newest version (1.22.1-9).

$ sudo nginx -v
nginx version: nginx/1.22.1

我们可以在他的配置文件/etc/nginx/中查看下服务器默认的资源根目录

1
2
$ sudo grep -rn root
sites-available/default:41: root /var/www/html;

我们可以修改这个这个目录,我就不改了,选择将hexo生成的博客框架拷贝到这个目录下。

1
sudo cp /home/zrf/hexo/public/* /var/www/html/ -arf

如果你修改了ngnix的配置文件,可以使用sudo ngnix -s reload命令来让他重新加载配置。

这样,我们就可以通过浏览器来访问我们的个人博客了。