使用Hexo搭建个人博客

前言

本文主要讲述如何使用Hexo搭建个人博客、配置Hexo插件和主题、以及部署相关。

本地运行

首先需要确保你的电脑有 Node.jsGit,如果没有则需要先安装一下

安装Node.js

Node.js 安装包及源码下载地址为:https://nodejs.org/zh-cn/download/

根据不同的系统选择对应的 Node.js 的安装包进行下载,然后直接傻瓜式安装即可

安装Git

Git 安装包下载地址为:https://git-scm.com/download

根据不同的系统选择对应的安装方式进行安装即可

安装Hexo

接下来需要全局安装 Hexo

打开命令行界面,输入以下命令开始安装

1
npm install hexo-cli -g

等待安装完成后命令行输入

1
hexo -v

显示对应版本号即为安装成功,如下图

建站并本地运行

安装 Hexo 完成后,请执行下列命令,Hexo 将会在指定文件夹中新建所需要的文件

1
2
3
hexo init <指定文件夹路径或文件夹名>
cd <指定文件夹路径或文件夹名>
npm install

新建完成后,指定文件夹的目录如下:

1
2
3
4
5
6
7
├── _config.yml
├── package.json
├── scaffolds
├── source
│ ├── _drafts
│ └── _posts
└── themes

在当前文件夹内执行以下命令本地启动

1
hexo server

等待命令执行完浏览器访问http://localhost:4000/,显示内容即为运行成功,如下图:

到这里我们就用hexo成功的搭建了最基础的博客,并在本地成功运行了

Hexo主题

以本博客为例,说一下主题的更换

  1. 在官网的主题页选一个你喜欢的主题,这里我用的是 Icarus

  1. 点击这个主题的名字跳转到对应的 github 仓库,并复制 github 地址

  1. 打开博客目录下的 themes 目录,在当前目录下打开命令窗口,执行 git clone <复制的github地址> 命令并等待执行完成

  1. 打开博客根目录的 _config.yml 文件,找到 theme 配置并修改为你要切换的主题名称
1
theme: icarus
  1. 重启服务,主题切换完成

这是最基础的主题切换,如果有要定制化的修改请在主题源码里自行修改

1
hexo server

Hexo插件

本博客用到的插件:

插件名功能
hexo-abbrlink生成URL短链
hexo-generator-sitemap / hexo-generator-baidu-sitemap生成站点地图
hexo-auto-excerpt自动摘录
hexo-generator-archive生成归档目录
hexo-generator-category生成分类目录
hexo-generator-tag生成标签目录
gitalk评论系统(利用github的issues)

gitalk配置这块我会单拉出一篇文章说

插件的安装一般为博客文件夹目录打开命令面版并执行以下命令

1
npm install <插件名> --save

具体请查看不同插件的文档

部署

博客的部署我这里介绍两种,github托管和自有服务器部署

使用 github 托管的优点是免费,缺点因国内网络问题访问不是很稳定

自有服务器部署的优点是访问稳定,缺点是需要自行购买服务器

github 托管

请参考 https://jeam.org/338f9b1

自有服务器部署

首先需要有一台自己的服务器,如何购买我这里就不说了,参考 https://jeam.org/338f9b1

安装 Nginx

参考 https://www.cnblogs.com/lywJ/p/10710361.html

使用命令一键部署

首先说一下我使用的是 rsync 命令来完成静态文件远程同步的

rsync 可以在本地计算机与远程计算机之间,或者两个本地目录之间同步文件

下面讲一下 rsync 的安装及 ssh 的配置

首先要在本地写作的机器和服务器都安装 rsync

1
2
3
4
5
6
7
8
# Debian
$ sudo apt-get install rsync

# Red Hat
$ sudo yum install rsync

# Arch Linux
$ sudo pacman -S rsync

安装成功后可以试一下好不好用

1
rsync <本地传输路径> root@<你的服务器ip>:<需要同步到服务器的路径>

接着会让你输入服务器的密码,等待传输完成后上服务器看一下是否成功同步

到这里就利用 rsync 完成了博客部署,但是有一个问题就是不能每次都输入一遍服务器密码吧,这样会很麻烦,理想的情况是一个命令就直接完成部署,不需要密码,这里就需要ssh的相关配置

首先在写作的机器上执行命令

1
ssh-keygen -t rsa -b 2048 -f /root/.ssh/hostkey

如果没有 .ssh 目录,手动创建一个,此时会在该目录下生成2个文件 hostkeyhostkey.pub

将生成的hustkey.pub传输给server,由于此处是要用于身份验证的

1
scp /root/.ssh/hostkey.pub 192.168.71.98:/.ssh/

接着再服务器 /etc/hosts.allow 里添加 sshd:192.168.71.178 ,这样做是为了让客户端可以登陆

/.ssh 目录下手动创建 touch authorized_keyschomd 600 authorized_keys 再将由客户端传过来的 hostkey.pub 导进去 cat hostkey.pub >> authorized_keys

vi /etc/ssh/sshd_config 文件

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
#
# Copyright 2004 Sun Microsystems, Inc. All rights reserved.
# Use is subject to license terms.
#
# ident "@(#)sshd_config 1.8 04/05/10 SMI"
#
# Configuration file for sshd(1m)

# Protocol versions supported
#
# The sshd shipped in this release of Solaris has support for major versions
# 1 and 2. It is recommended due to security weaknesses in the v1 protocol
# that sites run only v2 if possible. Support for v1 is provided to help sites
# with existing ssh v1 clients/servers to transition.
# Support for v1 may not be available in a future release of Solaris.
#
# To enable support for v1 an RSA1 key must be created with ssh-keygen(1).
# RSA and DSA keys for protocol v2 are created by /etc/init.d/sshd if they
# do not already exist, RSA1 keys for protocol v1 are not automatically created.

# Uncomment ONLY ONE of the following Protocol statements.

# Only v2 (recommended) #关闭
#Protocol 2

# Both v1 and v2 (not recommended) #开启,建议使用,增加兼容性
Protocol 2,1

# Only v1 (not recommended)
#Protocol 1

# Listen port (the IANA registered port number for ssh is 22)
Port 22

# The default listen address is all interfaces, this may need to be changed
# if you wish to restrict the interfaces sshd listens on for a multi homed host.
# Multiple ListenAddress entries are allowed.

# IPv4 only
#ListenAddress 0.0.0.0
# IPv4 & IPv6
ListenAddress ::

# Port forwarding
AllowTcpForwarding no

# If port forwarding is enabled, specify if the server can bind to INADDR_ANY.
# This allows the local port forwarding to work when connections are received
# from any remote host.
GatewayPorts no

# X11 tunneling options
X11Forwarding yes
X11DisplayOffset 10
X11UseLocalhost yes

# The maximum number of concurrent unauthenticated connections to sshd.
# start:rate:full see sshd(1) for more information.
# The default is 10 unauthenticated clients.
#MaxStartups 10:30:60

# Banner to be printed before authentication starts.
#Banner /etc/issue

# Should sshd print the /etc/motd file and check for mail.
# On Solaris it is assumed that the login shell will do these (eg /etc/profile).
PrintMotd no

# KeepAlive specifies whether keep alive messages are sent to the client.
# See sshd(1) for detailed description of what this means.
# Note that the client may also be sending keep alive messages to the server.
KeepAlive yes

# Syslog facility and level
SyslogFacility auth
LogLevel info

#
# Authentication configuration
#

# Host private key files
# Must be on a local disk and readable only by the root user (root:sys 600).
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_dsa_key

# Default Encryption algorithms and Message Authentication codes
#Ciphers aes128-ctr,aes128-cbc,arcfour,3des-cbc,blowfish-cbc
#MACS hmac-md5,hmac-sha1,hmac-sha1-96,hmac-md5-96

# Length of the server key
# Default 768, Minimum 512
ServerKeyBits 768

# sshd regenerates the key every KeyRegenerationInterval seconds.
# The key is never stored anywhere except the memory of sshd.
# The default is 1 hour (3600 seconds).
KeyRegenerationInterval 3600

# Ensure secure permissions on users .ssh directory.
StrictModes yes

# Length of time in seconds before a client that hasn't completed
# authentication is disconnected.
# Default is 600 seconds. 0 means no time limit.
LoginGraceTime 600

# Maximum number of retries for authentication
# Default is 6. Default (if unset) for MaxAuthTriesLog is MaxAuthTries / 2
MaxAuthTries 6
MaxAuthTriesLog 3

# Are logins to accounts with empty passwords allowed.
# If PermitEmptyPasswords is no, pass PAM_DISALLOW_NULL_AUTHTOK
# to pam_authenticate(3PAM).
PermitEmptyPasswords no

# To disable tunneled clear text passwords, change PasswordAuthentication to no.
PasswordAuthentication yes

# Use PAM via keyboard interactive method for authentication.
# Depending on the setup of pam.conf(4) this may allow tunneled clear text
# passwords even when PasswordAuthentication is set to no. This is dependent
# on what the individual modules request and is out of the control of sshd
# or the protocol.
PAMAuthenticationViaKBDInt yes

# Are root logins permitted using sshd.
# Note that sshd uses pam_authenticate(3PAM) so the root (or any other) user
# maybe denied access by a PAM module regardless of this setting.
# Valid options are yes, without-password, no.
PermitRootLogin yes

# sftp subsystem
Subsystem sftp /usr/lib/ssh/sftp-server


# SSH protocol v1 specific options
#
# The following options only apply to the v1 protocol and provide
# some form of backwards compatibility with the very weak security
# of /usr/bin/rsh. Their use is not recommended and the functionality
# will be removed when support for v1 protocol is removed.

# Should sshd use .rhosts and .shosts for password less authentication.
IgnoreRhosts yes
RhostsAuthentication yes #开启

# Rhosts RSA Authentication 此处重点修改。
# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts.
# If the user on the client side is not root then this won't work on
# Solaris since /usr/bin/ssh is not installed setuid.
RhostsRSAAuthentication yes #开启

AuthorizedKeysFile .ssh/authorized_keys #增加这一行,关键的精髓


# Uncomment if you don't trust ~/.ssh/known_hosts for RhostsRSAAuthentication.
#IgnoreUserKnownHosts yes

# Is pure RSA authentication allowed.
# Default is yes
RSAAuthentication yes #开启
ChRootGroups sftp,guest

最后再测试一下

1
rsync -av -e ssh <本地传输路径> root@<你的服务器ip>:<需要同步到服务器的路径>

可以把部署相关写到一个 shell 文件里,在当前目录下创建 deploy.sh 文件,复制粘贴下面代码

1
2
3
4
5
6
7
#!/bin/bash

hexo clean # 清除缓存
hexo generate # 生成最新的博客的静态文件
echo "--静态文件已生成--"
rsync -av -e ssh <本地传输路径> root@<你的服务器ip>:<需要同步到服务器的路径> # 与服务器同步静态文件
echo "--自动化部署完成--"

然后在 package.json 里的 scripts 里添加下面这行

1
"deploy": "./deploy.sh"

然后在需要部署的时候直接 npm run deploy 就可以了

最后

有一些功能并不是主题或者插件,比如夜间模式切换功能、页脚显示邮箱等等,这些都是在主题源码的基础上做的更改

如果你喜欢我配置的主题可以直接 fork 我的代码然后做你的修改

博客的 github 地址为:https://github.com/lvboda/blog

如果你有任何问题,可以在下方评论留言

参考资料

使用Hexo搭建个人博客

https://lvboda-blog.pages.dev/a927.html

作者

Boda Lü

发布于

2022-08-12

更新于

2026-03-24

许可协议


评论