VPS上部署hexo个人博客

前言

如果觉得Blog托管服务满足不了自己折腾,可将资源迁移到自己的云服务器上

目标

  • 服务端部署hexo博客站点环境
  • 客户端发布文章推送到服务端

准备环境

服务端必备软件

  • Git
  • Nginx

客户端必备软件

  • Git
  • Node.js
  • hexo

服务端配置(以Centos为例)

添加Epel源方便yum安装Nginx

1
2
3
4
wget https://mirrors.tuna.tsinghua.edu.cn/epel/7/x86_64/Packages/e/epel-release-7-11.noarch.rpm
rpm -ivh epel-release-7-11.noarch.rpm
yum clean all
yum makecache

安装Git Nginx

1
yum install git nginx -y

创建Nginx用户与群组

1
2
groupadd nginx
useradd -g nginx -s /sbin/nologin nginx

创建Nginx hexo站点文件目录

1
2
mkdir -pv /webdata/www/hexo
chown -R nginx:nginx /webdata/www/hexo

配置Nginx
配置文件路径

1
vim /etc/nginx/nginx.conf

所需修改段

1
2
3
4
5
6
7
8
.....
server {
listen 80 default_server;
server_name www.example.com;
root /webdata/www/hexo;
index index.html index.htm;
}
.....

执行service nginx configtest检查nginx.conf语法是否正确,显示success然后重启service nginx restart

Git 配置
创建文件目录,用于私人Git仓库搭建

1
mkdir -pv /data/repository

Git 初始化裸库

1
2
cd /data/repository
git init --bare hexo.git

创建Git钩子(hook)

1
vim /data/repository/hexo.git/hooks/post-receive

指定Nginx站点目录与Git仓库地址

1
2
#!/bin/bash
git --work-tree=/webdata/www/hexo --git-dir=/data/repository/hexo.git checkout -f

保存退出并添加执行权限

1
chmod +x /data/repository/hexo.git/hooks/post-receive

客户端配置

本篇以本地虚拟机Centos为例,其它平台对应搜索方知如何安装。
安装Git Nodejs

1
yum install git nodejs -y

安装hexo

1
npm install -g hexo-cli

hexo初始化与安装

1
2
3
4
mkdir -pv <folder>  (/data/www/hexo)
hexo init <folder>
cd <folder>
npm install

至于后面的hexo详细配置,请参照hexo 文档.

配置本地客户端与服务端连接
打开站点配置文件

1
vim /data/www/hexo/_config.yml

设置Deployment字段

1
2
3
4
5
6
## Docs: https://hexo.io/docs/deployment.html
deploy:
type: git
repo: root@域名或者IP:/data/repository/hexo.git #SSH默认22端口
#repo: ssh://root@域名或者IP:端口/data/repository/hexo.git #SSH非默认22端口
branch: master

如果每次发布都要输入密码嫌麻烦,可以配置客户端与服务端RSA认证,如果不知可以通过搜索引擎找到。

配置Git邮箱与用户
Git初次连接需要配置邮箱与用户

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

部署hexo

将本地生成的静态文件部署到服务端
清除缓存

1
hexo clean

生成静态页面

1
hexo generate

启动服务器

1
hexo server   #调试网页用,并非必要

将本地hexo目录文件部署到服务端
但是在这之前需要安装hexo-deployer-git插件

1
npm install hexo-deployer-git --save

然后执行部署

1
hexo deploy

结语

看似写了一大推,其实也没多少地方要配置的,Blog部署起来反而简单了。