前言

hexo 是方便用户快速搭建个人博客的一个工具平台, 我们可以使用 hexo 搭建自己的个人博客, 其次 hexo 也有好多主题, 可以满足各自的喜好。 hexo 文档介绍可见: hexo 文档

本文记录下 hexo 个人博客的搭建, 有三部分需要记录。 其一服务器的搭建, 其二 hexo 使用, 其三 nginx 基本使用。

个人博客服务搭建

假设你已经有自己的服务器, 那么需要安装 Git。 因为 hexo 编辑好发布博客的时候可以直接发布到自己的 Git 地址上。

由于是服务器的仓库, 所以初始化仓库的时候 –bare。

1
git init blog.git --bare

添加 hook, 把 hexo 提交的博客数据放置到其他位置。

1
2
3
4
5
6
# 如 blog.git 目录下的 hooks 文件夹。 
# /home/${username}/blog.git/hooks
# 新建 post-receive

#!/bin/bash
git --work-tree=/home/${username}/blog --git-dir=/home/${username}/blog.git checkout -f

hexo 使用

具体的命令可以直接查看 hexo 的官方文档。 这里只是列出几个简单的命令。

hexo 配置

hexo 的配置主要是 _config.yml 中。 如配置发布的 Git 的地址:

1
2
3
4
5
6
7
# Deployment
## Docs: https://hexo.io/docs/one-command-deployment
deploy:
type: 'git'
repo: xiaoxige@xxx.xxx.xxx.xxx:blog.git
branch: master

其他配置见官方文档。

hexo –help

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Usage: hexo <command>

Commands:
clean Remove generated files and cache.
config Get or set configurations.
deploy Deploy your website.
generate Generate static files.
help Get help on a command.
init Create a new Hexo folder.
list List the information of the site
migrate Migrate your site from other system to Hexo.
new Create a new post.
publish Moves a draft post from _drafts to _posts folder.
render Render files with renderer plugins.
server Start the server.
version Display version information.

Global Options:
--config Specify config file instead of using _config.yml
--cwd Specify the CWD
--debug Display all verbose messages in the terminal
--draft Display draft posts
--safe Disable all plugins and scripts
--silent Hide output on console

常用命令

  • 新建一个博客文档
1
hexo new [layout] fileName
  • 编译生成博客
1
hexo g
  • 发布命令(发布到自己服务的 Git 中)
1
hexo d

nginx 基本使用

使用 nginx 主要是为了可以访问到我们的个人博客。 我们通过第一步 hook 可知, 我们的博客文件在 /home/${username}/blog 中。 我们配置好 nginx 的配置文件, 使访问地址指向当前目录即可。

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

# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/

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 4096;

include /etc/nginx/mime.types;
default_type application/octet-stream;

# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;

server {
listen 80;
listen [::]:80;
server_name xiaoxige.cn www.xiaoxige.cn;
root /home/${username}/blog;

# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;

error_page 404 /404.html;
location = /40x.html {
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}

# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2;
# listen [::]:443 ssl http2;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers PROFILE=SYSTEM;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }

}