我的Hexo配置

记录一下我的Hexo配置,我的操作系统是Gentoo Linux,其他操作系统也都类似。

安装gitnodejs

1
$ emerge --ask dev-vcs/git net-libs/nodejs

安装hexo

1
2
3
4
$ npm install hexo-cli -g
$ hexo init blog
$ cd blog && npm install
$ hexo server

用浏览器打开http://127.0.0.1:4000,可以打开hexo的demo,默认的主题是landscape

安装next主题

我用的是hexo-next主题:

1
$ git clone https://github.com/theme-next/hexo-theme-next.git themes/next

编辑_config.yml,把themes选项的landscape改成next。主题的配置文件在themes/next/_config.yml(这是老版本的主题配置,新的有变更)

Deploy配置

安装hexo-deployer-git

1
npm install hexo-deployer-git --save

然后把_config.yml改成这样:

1
2
3
4
5
6
# Deployment
## Docs: https://hexo.io/docs/deployment.html
deploy:
type: git
repo: git@server_ip:/opt/hexo
branch: master

其他一些记录

nodejs版本问题
v14的nodejs好像不能deploy,所以建议暂时屏蔽掉v14版本。(已解决)

1
2
3
4
# /etc/portage/packages.mask/nodejs
=net-libs/nodejs-14.2.0
=net-libs/nodejs-14.1.0
=net-libs/nodejs-14.0.0

正文字体问题
hexo-next默认的正文字体太大了,试了改themes/next/_config.yml但是没有效果。
themes/next/source/css/_variables/base.styl第90行,把1em改成.875em,可以非常暴力的解决这个问题。(已经不需要了)

服务端配置

Hexo是可以放在Github Page上的,这样不需要额外的VPS,但缺点是任何人都可以访问github来fork博客的所有内容。所以我最后是选择自建VPS来布署博客。首先是在服务器端安装git,我的服务器选择的是CentOS:

1
# yum install git -y

然后是创建一个git用户:

1
# adduser git

然后是把公钥id_rsa.pub文件导入到/home/git/.ssh/authorized_keys,可以在本地ssh登录git试一下有没有问题。

确认没有问题之后就可以关闭gitshell登录了,编辑/etc/passwd,改成如下这样:

1
2
3
4
# /etc/passwd
...
git:x:1001:1001:,,,:/home/git:/usr/bin/git-shell
...

建立一个Git仓库:

1
2
# git init --bare /opt/hexo
# chown -R git:git /opt/hexo

然后设置HOOKS,

1
2
3
4
5
6
7
8
9
10
11
12
#! /bin/bash

# /opt/hexo/hooks/post-recevie

GIT_REPO=/opt/hexo
TMP_GIT_CLONE=/tmp/hexo
PUBLIC_WWW=/var/www/html
rm -rf ${TMP_GIT_CLONE}
git clone $GIT_REPO $TMP_GIT_CLONE
rm -rf ${PUBLIC_WWW}/*
cp -rf ${TMP_GIT_CLONE}/* ${PUBLIC_WWW}
rm -rf ${TMP_GIT_CLONE}

配置一下post-recevie的文件属性:

1
2
# chown -R git:git /opt/hexo/hooks/post-recevie
# chomd +x /opt/hexo/hooks/post-recevie

收尾

最后在本地的hexo目录试一下:

1
bash# hexo g -d

应该就可以通过web访问博客了。