网上利用GitHub搭建个人的hexo博客的教程很多,而且也比较简单,这里不多介绍。

由于自己有个vps,不想闲置了,于是磕磕绊绊踩了无数的坑,把自己的hexo博客部署在了阿里云学生机上。

大致步骤

  1. hexo本地
  2. 创建git用户,并创建一个仓库
  3. 建立SSH信任关系,并禁用git用户的shell登录权限
  4. 下载配置nginx
  5. 测试

hexo本地配置

以下指令,本地新建文件夹(博客本地文件夹),然后右键git bash here后执行

安装git

1
https://git-scm.com/

安装nodejs

1
https://nodejs.org/en/

安装hexo

1
$ npm install -g hexo-cli

初始化hexo

1
2
$ hexo init//若成功,这个文件夹即为网站根目录,若执行hexo init blog,那么会有个blog文件夹,它为网站根目录
$ npm install

以上,hexo就安装好了

hexo init过慢的问题

由于墙的原因,hexo初始化很容易卡死,下面提供解决方案

方案来自:https://blog.nfz.moe/archives/hexokit-intro.html

1
2
//一键脚本,我这边使用无效
curl http://git.oschina.net/neoFelhz/hexokit/raw/master/install.sh | sh
1
2
3
4
5
6
7
8
9
10
11

> ```bash
//一步步执行,执行完后hexokit即网站根目录,主要是更换了npm源
npm config set registry https://registry.npm.taobao.org
npm install hexo-cli -g
git clone https://git.oschina.net/neoFelhz/hexokit.git
rm install.sh
cd hexokit
npm install
npm config set registry https://registry.npmjs.org/
hexo version

新建git用户,创建仓库

以下命令在vps中运行

下载git

1
2
3
git --version //查看git版本,若无,则需要下载git
yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-devel//下载git
yum install -y git

创建用户并配置仓库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
useradd git //添加git用户
passwd git // 设置密码
su git // 这步很重要,不切换用户后面会很麻烦
cd /home/git/
mkdir -p projects/blog // 项目存在的真实目录
mkdir repos && cd repos
git init --bare blog.git // 创建一个裸露的仓库
cd blog.git/hooks
vi post-receive // 创建hook钩子函数,输入了内容如下(原理可以参考上面的链接)

#!/bin/sh
git --work-tree=/home/git/projects/blog --git-dir=/home/git/repos/blog.git checkout -f

//添加完毕后修改权限,执行如下命令
chmod +x post-receive
exit // 退出到 root 登录
chown -R git:git /home/git/repos/blog.git // 添加权限

//测试git仓库是否可用,另找空白文件夹,执行如下命令
git clone git@server_ip:/home/git/repos/blog.git

建立SSH信任关系,并禁用git用户的shell登录权限

由于每次连接仓库都要输入密码很不方便,需要建立信任关系

配置本地git用户

1
2
3
4
5
6
//配置本地git用户,不然会报有趣的错"你还没告诉我你是谁!"(错误详情写在最后了,因为我是后面碰到的)
$ git config --global user.email "yourname@qq.com"

$ git config --global user.name "yourname"

$ hexo d

建立ssh信任关系

1
2
3
4
//以下代码直接在本地新开git窗口执行(不是在网站根目录)
//建立ssh信任关系,在本地电脑,执行如下命令
ssh-copy-id -i C:/Users/yourname/.ssh/id_rsa.pub git@server_ip
ssh git@server_ip // 测试能否登录

禁用git用户的shell登录权限

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//如果第 5 步能成功,为了安全起见禁用git用户的 shell 登录权限,从而只能用git clone,git push等登录,执行如下命令

cat /etc/shells // 查看`git-shell`是否在登录方式里面,有则跳过
which git-shell // 查看是否安装
vi /etc/passwd
//添加上2步显示出来的路径,通常在 /usr/bin/git-shell

git:x:1000:1000::/home/git:/bin/bash
//修改为
git:x:1000:1000::/home/git:/usr/bin/git-shell

//我的路径在/bin/git-shell
//相对应的修改为git:x:1000:1000::/home/git:/bin/git-shell

下载配置nginx

下载安装nginx

1
2
3
4
5
6
7
8
9
10
cd /usr/local/src
wget http://nginx.org/download/nginx-1.15.2.tar.gz //下载
tar xzvf nginx-1.15.2.tar.gz //解压
cd nginx-1.15.2
./configure
make && make install
alias nginx='/usr/local/nginx/sbin/nginx' // 为nginx取别名,后面可直接用

//如果指令有报错,执行下面的语句
yum -y install gcc gcc-c++ autoconf automake make

配置nginx

1
2
3
4
5
6
7
8
9
10
11
12
cd /usr/local/nginx/conf
vi nginx.conf //修改配置文件

//第一行的 #user: nobody; 改成 user: root;
//然后其中有部分如下
#access_log logs/host.access.log main;

location / {
root html;//此处修改
index index.html index.htm;
}
//把root后面的html改为 /home/git/projects/blog/

运行nginx

1
nginx -s reload

此时可能会报错

nginx: [error] open() “/usr/local/nginx/logs/nginx.pid” failed (2: No such file ordirectory)

1
2
//使用指定nginx.conf文件的方式重启nginx,然后继续重新执行上面的指令
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
1
2
vi nginx.conf
nginx

此时可能会报错

nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

nginx: [emerg] still could not bind()

1
ps aux|grep nginx //输入指令

root 7110 0.0 0.0 24348 752 ? Ss 22:32 0:00 nginx: master process /usr/local/nginx/sbin/nginx

nobody 7111 0.0 0.0 26860 1508 ? S 22:32 0:00 nginx: worker process

root 7114 0.0 0.0 112664 968 pts/0 S+ 22:33 0:00 grep —color=auto

1
2
3
4
kill -9 7110
kill -9 7111
kill -9 7114
//可能数字不同,根据自己的情况“杀死那个进程”
1
nginx //继续执行

修改本地_config.yml站点配置文件

文件在本地在网站根目录中

1
2
3
4
deploy:
type: git
repo: git@yourseverip:/home/git/repos/blog.git
branch: master

测试

1
2
hexo s //博客本地根目录git执行
//在浏览器输入localhost:4000查看
1
2
hexo d -g //生成静态网页并发布
//在浏览器输入ip地址后查看

若报错

1
<strong>ERROR Deployer not found : git

执行

1
2
3
$ npm install hexo-deployer-git --save
$ hexo g
$ hexo d

若继续报错

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
INFO  Deploying: git
INFO Setting up Git deployment...
Initialized empty Git repository in E:/hexo/.deploy_git/.git/

*** Please tell me who you are.

Run

git config --global user.email "you@example.com"
git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.

fatal: unable to auto-detect email address (got 'dan@dan-PC.(none)')
FATAL Something's wrong. Maybe you can find the solution here: http://hexo.io/do
cs/troubleshooting.html
Error:
*** Please tell me who you are.

Run

git config --global user.email "you@example.com"
git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.

fatal: unable to auto-detect email address (got 'XXX@XXX-PC.(none)')

at ChildProcess.<anonymous> (E:\hexo\node_modules\hexo-deployer-git\node_mod
ules\hexo-util\lib\spawn.js:37:17)
at emitTwo (events.js:87:13)
at ChildProcess.emit (events.js:172:7)
at ChildProcess.cp.emit (E:\hexo\node_modules\hexo-deployer-git\node_modules
\hexo-util\node_modules\cross-spawn\node_modules\cross-spawn-async\lib\enoent.js
:37:29)
at maybeClose (internal/child_process.js:827:16)
at Process.ChildProcess._handle.onexit (internal/child_process.js:211:5)
FATAL

是因为本地没配置git用户信息执行

1
2
3
4
5
$ git config --global user.email "yourname@qq.com"

$ git config --global user.name "yourname"

$ hexo d

其它

  1. 视频教程来自https://www.bilibili.com/video/av50025574/
  2. 可能有其他的坑,我不想再踩一次就不一一解决了
    1. vps安全组问题
    2. SSH问题
    3. 手误问题(路径输错,把/漏掉了……)
  3. 主题推荐:强推next!虽然烂大街了,但是我还是觉得很好用!
  4. 有空可以学学markdown语法,还是很方便的。
  5. 详情参阅hexo和next的官方文档,还是挺详细的。