将hexo部署至阿里云服务器

将hexo部署至阿里云服务器

使用Hexo搭建博客目前有以下三种方案:
  • Hexo 框架 + github
  • Hexo 框架 + github + 自定义域名
  • Hexo 框架 + 云服务器 + 自定义域名

我主要讲述 第三种方式,使用自己已经有的服务器,以及可用的域名来搭建自己的博客.

「Hexo + 阿里云 + 域名」搭建步骤
整体框架结构图

整体框架结构图

整体流程
  1. 本地PC 安装node.js 以及Hexo 的初始化
  2. 服务器环境搭建 安装 git node nginx配置 创建git用户
  3. 使用Git 自动化部署发布博客
1.本地Hexo环境

1.1 安装git

1.2 安装 node

1.3 安装 npm 强烈建议 使用 cnpm (只要网络问题很多时候 npm 命令会报错 导致 步骤失败 )

1.4 node -v npm -v 检查 版本 验证是否安装成功

1.5 安装Hexo

1
npm install -g hexo-cli

在本地自己喜欢的地方新建一个博客文件夹 blog 比将其初始化

1
2
3
hexo init blog
cd blog
npm install

初始化之后在项目根目录 里面进行Hexo 插件 hexo-deployer-git(自动化部署的时候使用),hexo-server 的安装

1
2
npm install hexo-deployer-git --save
npm install hexo-server

配置GIT全局变量

项目根目录中打开 GitBash here

1
2
git config --global user.email "youremail@mail.com"
git config --global user.name "yourname"
2.服务器端

1.会用到 80端口 所以需要配置安全组 开放80端口(阿里云默认不开启)

2.最好使用 https 在 配置nginx时可以一起配置

3.在配置 密钥时 .ssh文件夹(隐藏文件夹) 在ftp中 默认不可见 需要调出来 ls -a 查看

4.在 服务器端操作时 需要新建 git 用户 需要注意 当前用户 和可操作 权限问题

1.安装nginx

安装有多种方式 我主要说下 配置
首先 服务器上需要新建 一个hexo 部署目录 我使用的是 /home/git/blog

1
mkdir -p /home/git/blog

进入nginx配置文件

1
2
cd /usr/local/nginx/conf
vim nginx.conf

将顶端的user(用户)改为 root
以下为我的配置

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

user root;
worker_processes 1;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;


events {
worker_connections 1024;
}


http {
include mime.types;
default_type application/octet-stream;

#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 logs/access.log main;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;

#gzip on;

server {
listen 80;
server_name www.xuwx.top;
rewrite ^(.*)$ https://$host$1 permanent;
}

# HTTPS server
#
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name www.xuwx.top;

#正式配置
ssl_certificate cert/www.xuwx.top.pem;
ssl_certificate_key cert/www.xuwx.top.key;

ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;

location / {
root /home/git/blog;
proxy_read_timeout 300;
proxy_connect_timeout 300;
proxy_redirect off;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
}

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

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

}

主要是两个地方 location 下 配置 root 为直接的 /home/git/blog 博客要部署的位置 以及 80端口的配置

注意 修改完毕 记得重启 nginx

2. node的安装

这里不再复述 当然npm 最好使用 国内仓库

3. 服务器端git的安装以及配置

1.git 安装 不再 复述

  1. 新建git 用户 (目的用于后面blog代码的自动化部署)
1
adduser git

3.修改git用户的权限 740

1
chmod 740 /etc/sudoers

4.配置文件中增加 Git 用户

1
vim /etc/sudoers

5.在 root ALL=(ALL) ALL
下 添加 git ALL=(ALL) ALL

6.Git 用户的权限改回去。

1
chmod 400 /etc/sudoers

7.设置 Git 用户密码

1
sudo passwd git

完成了 Git 用户的创建后,接下来我们向 Git 用户添加公钥,就像配置 Github 那样

  1. Git 用户配置 ssh 公钥
    将 本地pc user\用户.ssh 文件夹下的 id_rsa.pub(没有的话网上参考如何创建该文件) 文件 拷贝至 服务器 /home/git/.ssh文件夹下

8.1在服务器端 切换到 git 用户,在根目录下创建 .ssh文件夹。

1
2
3
su git
cd ~
mkdir .ssh

8.2 用GIT用户身份 .ssh 文件夹内新建 authorized_keys 文件,并将公钥内容拷贝到该文件中。

1
2
3
4
cd ~/.ssh
cp id_rsa.pub authorized_keys
cat id_rsa.pub >> ~/.ssh/authorized_keys

8.3 修改文件权限

1
2
chmod 600 ~/.ssh/authorized_keys
chmod 700 ~/.ssh

8.4 确保设置了正确的SELinux上下文

1
restorecon -Rv ~/.ssh

8.5 测试一下是否设置成功。在本地任意位置右键打开 GitBash Here,输入公网 IP

1
ssh -v git@xxx.xxx.xxx.xxx(你的公网 IP)

成功就会和 xshell 连进去一样了.

9.设置Git仓库

9.1 继续使用Git 用户 新建 post-receive 钩子文件

1
2
3
cd ~
git init --bare hexo.git
vi ~/hexo.git/hooks/post-receive

新增如下命令 注意跟换为自己的博客部署目录

1
git --work-tree=/home/git/blog --git-dir=/home/git/hexo.git checkout -f

9.2 授予钩子文件可执行权限

1
2
3
chmod +x ~/hexo.git/hooks/post-receive
cd ~
sudo chmod -R 777 /home/git/blog

10.完成上述配置之后就可以 重启服务器了.

3.自动化部署

1.现在 回到本地pc blog 文件根目录 找到 _config.yml文件 打开 并修改

***为自己的服务器公网ip

1
2
3
4
5
6
# Deployment
## Docs: https://hexo.io/docs/one-command-deployment
deploy:
type: git
repo: git@***.***.***.***:/home/git/hexo.git
branch: master

除此之外,URL 项改为自己的域名,没有备案的化可以先填写公网 IP

1
2
3
4
5
6
7
8
9
# URL
## If your site is put in a subdirectory, set url as 'http://yoursite.com/child' and root as '/child/'
url: www.xuwx.top
root: /
permalink: :year/:month/:day/:title/
permalink_defaults:
pretty_urls:
trailing_index: true # Set to false to remove trailing 'index.html' from permalinks
trailing_html: true # Set to false to remove trailing '.html' from permalinks

2.Hexo 自动部署
执行以下三个命令(每次文章发布的时候都需要用到)

1
2
3
4
5
6
7
8
9
hexo clean
hexo generate
hexo deploy

hexo cl
hexo g
hexo d

hexo cl && hexo g && hexo d

最后 hexo s 启动博客

4.参考的文章

https://zhuanlan.zhihu.com/p/144774977

https://www.jianshu.com/p/e1ccd49b4e5d


将hexo部署至阿里云服务器
https://www.xuwx.top/2020/08/30/将hexo部署至阿里云服务器/
作者
Shine_ssr
发布于
2020年8月30日
许可协议