nginx 将 hexo 部署到私有 vps
blog
本文字数:3.2k 字 | 阅读时长 ≈ 15 min

nginx 将 hexo 部署到私有 vps

blog
本文字数:3.2k 字 | 阅读时长 ≈ 15 min

博主打算将 hexo 博客从 windows 上迁移到私有的 linux 服务器,这里记录了一下迁移过程

1. 前置包安装

1.1 更新系统

sudo apt update -y
sudo apt upgrade -y

1.2 安装 git

apt-get install git

1.3 安装 nodejs

查看系统配置

root@iZuf67mlx4ftb5vohxeobnZ:~# uname -p
x86_64
root@iZuf67mlx4ftb5vohxeobnZ:~# uname -o
GNU/Linux

进入https://nodejs.org/download/release/v20.11.1/,根据配置下载相应的包文件,这里我下载的是 node-v20.11.1-linux-x64.tar.gz

root@iZuf67mlx4ftb5vohxeobnZ:~# tar -zxvf node-v20.11.1-linux-x64.tar.gz 
root@iZuf67mlx4ftb5vohxeobnZ:~# mv node-v20.11.1-linux-x64 nodejs
root@iZuf67mlx4ftb5vohxeobnZ:~# ln -s /root/nodejs/bin/node /usr/local/bin/node
root@iZuf67mlx4ftb5vohxeobnZ:~# ln -s /root/nodejs/bin/npm /usr/local/bin/npm
root@iZuf67mlx4ftb5vohxeobnZ:~# ln -s /root/nodejs/bin/npx /usr/local/bin/npx
root@iZuf67mlx4ftb5vohxeobnZ:~# node -v
v20.11.1
root@iZuf67mlx4ftb5vohxeobnZ:~# npm -v
10.4.0

到这里就安装成功了

1.4 安装 hexo

npm install -g hexo-cli #安装hexo

如果安装卡住,并报以下错误 rollbackFailedOptional verb npm-session,是因为 npm 的安装来自国外源,修改源即可,执行下面命令,就可以正常安装了

npm config set registry http://registry.npm.taobao.org
npm config get registry

1.5 安装 nginx

安装和卸载 nginx

sudo apt-get --purge remove nginx # 卸载
sudo apt install nginx # 安装

验证是否被成功安装

sudo systemctl status nginx # 验证是否被成功安装

出现 nginx.service - A high performance web server and a reverse proxy server 等内容则表示成功安装了

启动和重启 nginx

service nginx start  # 启动
systemctl restart nginx  # 重启

此时 ping 一下网页,执行 wget http://127.0.0.1,网页被成功下载则表示已经被部署了

至此,所有的前置包都已经安装完毕

2. 开始使用

从 win 迁移到 linux 上有几个比较重要的文件夹,scaffolds,source,themes,_config.yml,package.json,其余的可以不用关注

现在我们要实现这样的功能,首先是文件夹的创建。一个文件夹 hexo-blog 放我的 hexo 博客文件,HexoBlog 放 hexo g 生成的 public 文件夹,并且让 nginx 将 public 中的 html 文件部署到网页。其次,我们要用 git 管理工具,配合 github 来对博客文件进行管理,其中 hexo-blog 中的 scaffolds,source,themes,_config.yml,package.json 文件夹用 git 的 master 分支进行管理,生成的 public 文件夹则用 run-page 分支管理,同时 master 分支要提交到 github 中防止丢失,run-page 分支则在每次更新 public 文件夹之后的内容同步到 HexoBlog 中

注意:hexo-blog 和 HexoBlog 这两个文件夹不要放到/root 目录下!!!我之前部署的时候一直有问题,可能是读写权限的问题

2.1 构建 nginx 托管文件夹

创建 HexoBlog 文件夹,并对其赋予权限

makedir /blog/HexoBlog
chown -R $USER:$USER HexoBlog
chown -R 755 HexoBlog

修改 nginx 配置文件,将其部署的网页设置为 HexoBlog

vim /etc/nginx/sites-available/default

将其中的 root 后的文件夹修改为 root /blog/HexoBlogservice nginx restart 重启 nginx

Tips:为了验证新的 root 路径已经生效了,可以在 HexoBlog 文件夹中随便写一个 html 文件,然后重启 nginx,在网页端输入 vps 的公网 ip 看看路径是否修改成功了

2.2 用 git 对博客文件进行管理

1. 管理博客文件夹(.git)

创建 /blog/hexo-blog 作为我们的博客的文件夹,将之前的 scaffolds,source,themes,_config.yml,package.json 移动到该文件夹中,执行 npm install 根据 package.json 安装相应的依赖

使用 git 对 /blog/hexo-blog 进行版本控制管理

root@iZuf67mlx4ftb5vohxeobnZ:/blog/hexo-blog# git init
Initialized empty Git repository in /blog/hexo-blog/.git/
root@iZuf67mlx4ftb5vohxeobnZ:/blog/hexo-blog# git init --bare blog.git
Initialized empty Git repository in /blog/hexo-blog/blog.git/

这里初始化两个 git,.git 是管理当前 hexo-blog 文件夹,blog.git 是作为中心仓库进行主控制的,上面我们说过 hexo-blog 是用来写博客的,HexoBlog 是用 nginx 进行部署的,当我们博客写完执行 hexo g 命令时,会生成 public 文件夹,此文件夹的内容要同步到 HexoBlog 中,此时就需要 blog.git

.gitblog.git 进行关联,这里我的 .gitignore 文件中有以下内容 blog.git,node_modules,public,.deploy_git

root@iZuf67mlx4ftb5vohxeobnZ:/blog/hexo-blog# git add .
root@iZuf67mlx4ftb5vohxeobnZ:/blog/hexo-blog# git commit -m "first commit"
[master (root-commit) 5680251] first commit
 19 files changed, 6353 insertions(+)
 create mode 100644 .gitignore
 create mode 100644 _config.yml
 create mode 100644 db.json
 create mode 100644 package-lock.json
 create mode 100644 package.json
 create mode 100644 scaffolds/draft.md
 create mode 100644 scaffolds/page.md
 create mode 100644 scaffolds/post.md
 create mode 100644 source/.DS_Store
 create mode 100644 source/_posts/.DS_Store
 create mode 100644 "source/_posts/\345\210\251\347\224\250nginx\345\260\206hexo\351\203\250\347\275\262\345\210\260\347\247\201\346\234\211vps.md"
 create mode 100644 "source/_posts/\351\255\224\346\263\225\346\226\271\346\263\225-getattr-setattr-delattr.md"
 create mode 100644 source/about/index.md
 create mode 100644 source/categories/index.md
 create mode 100644 source/repository/index.md
 create mode 100644 source/tags/index.md
 create mode 100644 themes/.DS_Store
 create mode 100644 themes/.gitkeep
 create mode 160000 themes/pure
root@iZuf67mlx4ftb5vohxeobnZ:/blog/hexo-blog# git remote add origin root@47.100.8.18:/blog/hexo-blog/blog.git
root@iZuf67mlx4ftb5vohxeobnZ:/blog/hexo-blog# git branch -M main
root@iZuf67mlx4ftb5vohxeobnZ:/blog/hexo-blog# git push -u origin main
Enumerating objects: 28, done.
Counting objects: 100% (28/28), done.
Delta compression using up to 2 threads
Compressing objects: 100% (20/20), done.
Writing objects: 100% (28/28), 103.64 KiB | 4.32 MiB/s, done.
Total 28 (delta 2), reused 0 (delta 0)
To 47.100.8.18:/blog/hexo-blog/blog.git
 * [new branch]      main -> main
Branch 'main' set up to track remote branch 'main' from 'origin'.
root@iZuf67mlx4ftb5vohxeobnZ:/blog/hexo-blog# 

Tips:其实这里的 hexo-blog 可以存在于任何一台机器上,比如你的 mac 电脑,或者 windows 笔记本,只要.git 和 blog.git 进行关联即可,这里我想在 linux 上写博客,所以就放在上面了

此时就成功将 hexo-blogblog.git 关联起来了,任何对此文件夹的更改都可以提交到 blog.git 中,那么我们怎么将生成的 public 文件夹同步到 HexoBlog 中呢,那就需要 hooks 功能

2. 将 hexo 生成的 html 文件同步到 HexoBlog 中(blog.git)

blog.git 是一个中央版本控制器,master 分支管理博客,run-page 分支管理 public

进入到 blog.git 中,增加 post-receive 文件

root@iZuf67mlx4ftb5vohxeobnZ:/blog/hexo-blog# vim blog.git/hooks/post-receive

添加以下内容

#!/bin/bash

git --work-tree=/blog/HexoBlog --git-dir=/blog/hexo-blog/blog.git checkout -f run-page
echo '拉取完毕/blog/HexoBlog'
root@iZuf67mlx4ftb5vohxeobnZ:/blog/hexo-blog# chmod 755 blog.git/hooks/post-receive

此时,blog.git 也配置完毕

3. 配置 hexo 的_config.yml 文件

找到 deploy 的部分,修改为如下内容

deploy:
  type: git
  repository: root@47.100.8.18:/blog/hexo-blog/blog.git
  branch: run-page

这里表明 hexo g -d 部署的文件会放到 blog.git 的 run-page 分支

此时就大功告成了!

下面执行 hexo g -d 看看效果,可以发现 HexoBlog 已经自动保存了 public 的内容

root@iZuf67mlx4ftb5vohxeobnZ:/blog/hexo-blog# hexo g -d
INFO  Validating config
INFO  Start processing
...中间过程忽略
remote: Resolving deltas: 100% (47/47), done.
remote: Switched to branch 'run-page'
remote: 拉取完毕/blog/HexoBlog
To 47.100.8.18:/blog/hexo-blog/blog.git
 * [new branch]      HEAD -> run-page
Branch 'master' set up to track remote branch 'run-page' from 'root@47.100.8.18:/blog/hexo-blog/blog.git'.
INFO  Deploy done: git
root@iZuf67mlx4ftb5vohxeobnZ:/blog/hexo-blog# cd ..
root@iZuf67mlx4ftb5vohxeobnZ:/blog# cd HexoBlog/
root@iZuf67mlx4ftb5vohxeobnZ:/blog/HexoBlog# ls
2021  2024  about  archives  atom.xml  baidusitemap.xml  categories  content.json  css  favicon.png  fonts  images  index.html  js  repository  sitemap.txt  sitemap.xml  tags
root@iZuf67mlx4ftb5vohxeobnZ:/blog/HexoBlog# cd ..
root@iZuf67mlx4ftb5vohxeobnZ:/blog# 

此时在浏览器输入 47.100.8.18 即可以看到实时渲染效果啦!,这里的 ip 改为自己的即可

4. 推送到 github

首先我们在 github 上构建一个私人仓库(当然也可以公开),这里我构建了 hexo-blog 仓库,一些基本的 ssh 配置等自行查阅,这里不做说明

由于我们的 blog.git 是中央控制器,我们进入到 blog.git 里面,并将其和我们的 github 仓库关联

root@iZuf67mlx4ftb5vohxeobnZ:/blog/hexo-blog# cd blog.git/
root@iZuf67mlx4ftb5vohxeobnZ:/blog/hexo-blog/blog.git# git remote add origin https://github.com/harrytea/hexo-blog.git
root@iZuf67mlx4ftb5vohxeobnZ:/blog/hexo-blog/blog.git# git branch -a
  main
* run-page
root@iZuf67mlx4ftb5vohxeobnZ:/blog/hexo-blog/blog.git# git branch -r

从上面可以看到,我们本地有两个分支,main 是 hexo-blog 的分支,里面有博客,run-page 分支是 HexoBlog 分支,也就是 hexo 部署的时候生成的,里面有生成的 html 文件,我们将两个分支都 push 上去

push 一下 main 分支

root@iZuf67mlx4ftb5vohxeobnZ:/blog/hexo-blog/blog.git# git push -u origin main
Enumerating objects: 28, done.
Counting objects: 100% (28/28), done.
Delta compression using up to 2 threads
Compressing objects: 100% (20/20), done.
Writing objects: 100% (28/28), 103.64 KiB | 7.40 MiB/s, done.
Total 28 (delta 2), reused 1 (delta 0)
remote: Resolving deltas: 100% (2/2), done.
To https://github.com/harrytea/hexo-blog.git
 * [new branch]      main -> main
Branch 'main' set up to track remote branch 'main' from 'origin'.

push 一下 run-page 分支

root@iZuf67mlx4ftb5vohxeobnZ:/blog/hexo-blog/blog.git# git push -u origin run-page
Enumerating objects: 145, done.
Counting objects: 100% (145/145), done.
Delta compression using up to 2 threads
Compressing objects: 100% (61/61), done.
Writing objects: 100% (145/145), 741.97 KiB | 92.75 MiB/s, done.
Total 145 (delta 47), reused 145 (delta 47)
remote: Resolving deltas: 100% (47/47), done.
remote: 
remote: Create a pull request for 'run-page' on GitHub by visiting:
remote:      https://github.com/harrytea/hexo-blog/pull/new/run-page
remote: 
To https://github.com/harrytea/hexo-blog.git
 * [new branch]      run-page -> run-page
Branch 'run-page' set up to track remote branch 'run-page' from 'origin'.
root@iZuf67mlx4ftb5vohxeobnZ:/blog/hexo-blog/blog.git# 

进入到 github 中,我们就可以看到两个分支的内容了,这样即使 vps 无法登陆,我们也不会丢失文件了

3. 其他注意事项

3.1 git 的管理逻辑

我们执行 hexo g -d 的时候,登录 47.100.8.18 是可以实时看到网页的变化的,但是 github 并没有更新,我们需要在执行下面两步

在 hexo-blog 文件夹中执行

git add .
git commit -m "update blog"
git push -u origin main

此时本地的博客文件更新提交到了 blog.git 中,然后我们进入到 blog.git 文件夹中提交到 github

git push -u origin main
git push -u origin run-page

整个流程演示如下

root@iZuf67mlx4ftb5vohxeobnZ:/blog/hexo-blog# git add .
root@iZuf67mlx4ftb5vohxeobnZ:/blog/hexo-blog# git commit -m "update blog"
[main bf0e2c6] update blog
 3 files changed, 180 insertions(+), 4 deletions(-)
 create mode 100644 "source/_posts/\345\210\251\347\224\250nginx\345\260\206hexo\351\203\250\347\275\262\345\210\260\347\247\201\346\234\211vps/github.png"
root@iZuf67mlx4ftb5vohxeobnZ:/blog/hexo-blog# git push -u origin main
Enumerating objects: 13, done.
Counting objects: 100% (13/13), done.
Delta compression using up to 2 threads
Compressing objects: 100% (7/7), done.
Writing objects: 100% (8/8), 309.70 KiB | 11.91 MiB/s, done.
Total 8 (delta 5), reused 0 (delta 0)
remote: Already on 'run-page'
remote: Your branch is ahead of 'origin/run-page' by 1 commit.
remote:   (use "git push" to publish your local commits)
remote: 拉取完毕/blog/HexoBlog
To 47.100.8.18:/blog/hexo-blog/blog.git
   5680251..bf0e2c6  main -> main
Branch 'main' set up to track remote branch 'main' from 'origin'.
root@iZuf67mlx4ftb5vohxeobnZ:/blog/hexo-blog# cd blog.git/
root@iZuf67mlx4ftb5vohxeobnZ:/blog/hexo-blog/blog.git# git push -u origin main
Enumerating objects: 13, done.
Counting objects: 100% (13/13), done.
Delta compression using up to 2 threads
Compressing objects: 100% (7/7), done.
Writing objects: 100% (8/8), 309.70 KiB | 13.46 MiB/s, done.
Total 8 (delta 5), reused 0 (delta 0)
remote: Resolving deltas: 100% (5/5), completed with 4 local objects.
To https://github.com/harrytea/hexo-blog.git
   5680251..bf0e2c6  main -> main
Branch 'main' set up to track remote branch 'main' from 'origin'.
root@iZuf67mlx4ftb5vohxeobnZ:/blog/hexo-blog/blog.git# git push -u origin run-page
Enumerating objects: 20, done.
Counting objects: 100% (20/20), done.
Delta compression using up to 2 threads
Compressing objects: 100% (9/9), done.
Writing objects: 100% (11/11), 303.84 KiB | 15.19 MiB/s, done.
Total 11 (delta 5), reused 0 (delta 0)
remote: Resolving deltas: 100% (5/5), completed with 5 local objects.
To https://github.com/harrytea/hexo-blog.git
   30f844e..e944862  run-page -> run-page
Branch 'run-page' set up to track remote branch 'run-page' from 'origin'.
root@iZuf67mlx4ftb5vohxeobnZ:/blog/hexo-blog/blog.git# 

3.2 配置域名访问

在使用 nginx 配置域名之前,需要先将域名解析为 IP 地址,域名解析是由 DNS 服务器完成的,将域名映射为相应的 IP 地址。
例如我这里使用的是阿里云服务器,添加下面两条解析命令

  1. 登陆购买域名服务器的网站,找到 DNS 解析一览,输入 A 类型,name 为 www(我这边 www 和@都添加了),data 为你的 IP,其余不变,此时解析完成了

  2. 找到 nginx 的配置文件,我这边是/etc/nginx/sites-available/default(网上有的说是在 nginx.conf 中配置的,但我的配置文件也有效,我就没深究了),找到 server 模块部分,在 server_name 处添加你的域名,如下

	root /blog/HexoBlog;

	# Add index.php to the list if you are using PHP
	index index.html index.htm index.nginx-debian.html;

	# server_name _;
	server_name wangyh.co;

然后 sudo nginx -s reload 重启 nginx,在网站上输入 www.wangyh.co 即可访问了,可以在命令行输入以下内容检查域名解析的 IP 是否正确,我这里显示是正确的。如果提示你要备案,就需要提前备案,我这里没有备案,所以暂时用 ip 访问了

(base) root@iZuf67mlx4ftb5vohxeobnZ:/etc/nginx/sites-available# nslookup
> www.wangyh.co
Server:         127.0.0.53
Address:        127.0.0.53#53

Non-authoritative answer:
Name:   www.wangyh.co
Address: 47.100.8.18
12月 14, 2023
8月 10, 2022
10月 06, 2021