Hexo部署到个人服务器

前言·

博客已经是部署到GitHub上了,但自己还有一个服务器,就想说能不能也把博客部署到服务器上,然后再通过Nginx发布到网上。毕竟GitHub访问慢,而且在国内访问还不稳定,放在服务器上,国内访问也能更方便。

本地Hexo配置·

修改Hexo博客根目录下的_config.yml文件中的deploy配置,如下

1
2
3
4
5
deploy: 
type: git
repo:
aliyun: git@server:/home/git/Blog/hexoBlog.git,master
github: https://github.com/yunying61/yunying61.github.io.git,master

以上是我自己的配置,我配置了两个部署位置,一个部署到服务器上,一个部署到Github上。

本文只需要看第一个部署到aliyun上就可以

git:用于ssh连接的账户

server:服务器IP或域名

服务器配置·

安装git软件

1
yum install git -y

创建git用户·

  1. 添加git用户

    1
    adduser git
  2. 设置git用户密码

    1
    sudo passwd git

添加ssh权限·

  1. 编辑*/etc/ssh/sshd_config*文件

    1
    vim /etc/ssh/sshd_config

    i键,并在最后面添加配置项 AllowUsers root git ,按esc输入:wq保存退出

  2. 重启sshd服务器

    1
    /etc/init.d/ssh restart

保存ssh公钥·

请在搜索引擎上搜索关键词:生成SSH公钥并保存 ,本文不介绍如何操作

将本地端生成的SSH秘钥保存到服务器端的 /home/git/.ssh/authorized_keys 文件中保存

一定要是 /home/git/.ssh/authorized_keys 目录,不然无法免密登陆

执行以下命令,确保 /home/git 中所有目录和文件所有者都是 git

1
chown git /home/git -R

测试SSH连接

1
ssh -v root@server

仓库配置·

先切换到git用户,以防后面创建的文件和目录的所有者不是git

1
su git

新建git仓库·

Blog将作为Git的博客仓库

1
2
3
mkdir /home/git/Blog
chown -R git:git /home/git/Blog
chmod -R 755 /home/git/Blog

创建 Git 裸仓库

1
2
cd /home/git/Blog
git init --bare hexoBlog.git

新建网站仓库·

1
2
3
mkdir -p /www/wwwroot/hexo
chown -R git:git /www/wwwroot/hexo
chmod -R 755 /www/wwwroot/hexo

新建Git钩子·

通过/home/git/Blog/hooks下的post-receive钩子文件,自动部署到 /www/wwwroot/hexo

1
vim /home/git/Blog/hexoBlog.git/hooks/post-receive

i 键编辑,输入以下内容,按 Esc 键输入 :wq 保存退出

1
2
3
# !/bin/bash

git --work-tree=/www/wwwroot/hexo --git-dir=/home/git/Blog/hexoBlog.git checkout -f

修改成可执行权限

1
2
chown -R git:git /home/git/Blog/hexoBlog.git/hooks/post-receive
chmod +x /home/git/Blog/hexoBlog.git/hooks/post-receive

配置网站·

到这里,如果没出什么问题,那基本上可以部署到服务器上了,但还不能直接访问。需要配置成网站站点才能通过网址访问你的博客。

接下来,我将通过宝塔的网站配置来实现该功能。

  1. 进入你的宝塔面板 → 进入左侧的网站 → 添加站点

    域名:请填写你的服务器IP或域名

  2. 然后就可以用了,当然也可以在站点设置里,进行更详细的配置。