hexo部署

系统更新与安装

1
2
3
4
5
6
7
sudo apt update
sudo apt install npm
cd /
sudo mkdir -p /data/blog
cd /data/blog/
sudo apt install docker.io
sudo apt install -y nodejs

安装 MCSManager

1
2
# 建议使用官网脚本
sudo su -c "wget -qO- https://script.mcsmanager.com/setup_cn.sh | bash"

ADUS.sh

1
2
# 个人脚本
sudo wget -qO- https://gitee.com/mcsmanager/script/raw/master/setup_cn.sh | bash

访问面板:

1
http://[IP]:23333/

进入后到节点终端执行以下命令:

1
2
3
4
5
6
cd /data/blog
sudo rm -rf *
sudo npm install hexo-cli -g
hexo init /data/blog
hexo s
# 按 Ctrl+C 停止

npm下载卡顿解决方法

1
2
npm config set registry http://registry.npmmirror.com
npm config get registry

安装 hexo admin(可选,建议安装下方的qexo):

1
2
sudo npm install --save hexo-admin
hexo s

访问管理面板:

1
http://[IP]:4000/admin

配置 Hexo

回到面板,新建实例:

  • 通用控制台应用程序
  • 无需文件或选择已存在文件
  • 实例名称:hexo
  • 启动命令:hexo s
  • 工作目录:/data/blog/

测试实例后,修改端口:

  1. 进入 /data/blog/node_modules/hexo-server
  2. 编辑 index.js 文件,将 4000 改为 80
  3. 启动实例

hexo admin配置(若选择admin管理后台需要配置)

访问:

1
http://[IP]/admin/

设置用户名、密码、加密文本,复制下方代码:

关闭实例,编辑 _config.yml,在最下方粘贴保存的代码,保存并启动实例。

根据文档配置主题:Solitude 文档

安装 Qexo

1
2
3
4
5
6
7
sudo apt install python3-pip
cd /data
sudo mkdir qexo
cd qexo
wget https://github.com/Qexo/Qexo/archive/refs/tags/3.4.1.tar.gz
tar -zxf 3.4.1.tar.gz
mv Qexo-3.4.1 qexo

安装 MySQL 数据库:

1
2
3
4
5
sudo apt-get install mysql-server
sudo service mysql start
sudo service mysql status
sudo mysqladmin -u root -p password
# 输入a123456!

设置数据库:

1
2
3
4
5
6
7
8
9
10
mysql -uroot -p'a123456!' <<EOF
CREATE DATABASE IF NOT EXISTS qexo CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;
CREATE USER IF NOT EXISTS 'root'@'%' IDENTIFIED BY 'a123456!';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%';
CREATE USER IF NOT EXISTS 'qexo'@'localhost' IDENTIFIED BY 'a123456!';
GRANT ALL PRIVILEGES ON qexo.* TO 'qexo'@'localhost';
CREATE USER IF NOT EXISTS 'qexo'@'%' IDENTIFIED BY 'a123456!';
GRANT ALL PRIVILEGES ON qexo.* TO 'qexo'@'%';
SET GLOBAL log_bin_trust_function_creators = 1;
EOF

覆盖配置文件,注意修改 IP 地址:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# configs.py
vim /data/qexo/qexo/configs.py
覆盖内容如下

import pymysql
pymysql.install_as_MySQLdb()

DOMAINS = ["127.0.0.1", "192.168.163.30"]

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'qexo',
'USER': 'qexo',
'PASSWORD': 'a123456!',
'HOST': '127.0.0.1',
'PORT': '3306',
'OPTIONS': {
"init_command": "SET sql_mode='STRICT_TRANS_TABLES'",
"charset": "utf8mb4",
}
}
}

激活虚拟环境并安装依赖:

1
2
3
4
5
cd /data/qexo/qexo/
apt install python3.12-venv
python3 -m venv myenv
source myenv/bin/activate
pip install -r requirements.txt

运行数据库迁移和服务器:

1
2
3
python3 manage.py makemigrations
python3 manage.py migrate
python3 manage.py runserver 0.0.0.0:8000 --noreload

配置Qexo 管理面板: [IP]:8000

设置用户名、密码
服务商:本地
使用配置:Hexo
博客路径:/data/blog
自动部署:hexo cl && hexo g

访问地址

  • Hexo: [IP]
  • MCSManager Panel: [IP]:23333
  • Qexo 管理面板: [IP]:8000
1
请将 `[IP]` 替换为实际的 IP 地址。

重启后需要操作

如果安装admin面板则进入启动实例

如果安装qexo则

1
2
3
4
5
6
cd /data/qexo/qexo/
python3 -m venv myenv
source myenv/bin/activate
python3 manage.py makemigrations
python3 manage.py migrate
python3 manage.py runserver 0.0.0.0:8000 --noreload

失败则尝试

1
2
3
4
cd /data/qexo/qexo/
python3 -m venv myenv
source myenv/bin/activate
pip install -r requirements.txt
1
2
3
python3 manage.py makemigrations
python3 manage.py migrate
python3 manage.py runserver 0.0.0.0:8000 --noreload

ssl安全证书 (需要将hexo端口改回4000)

1
2
3
4
5
hexo blog为hexo安装路径
cd /hexo blog/node_modules/hexo-server/lib
npm install express --save
npm install connect --save
cp server.js server.js.back

将证书放在指定目录
如/data、/etc 确保有.key与.crt

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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
vim /hexo blog/node_modules/hexo-server/lib/server.js

'use strict';

const connect = require('connect');
const https = require('https');
const chalk = require('chalk');
const Promise = require('bluebird');
const open = require('open');
const net = require('net');
const url = require('url');

const fs = require('fs');
const express = require('express');

const httpApp = express();

// 设置 HTTP 重定向到 HTTPS
httpApp.all('*', (req, res, next) => {
let host = req.headers.host;
host = host.replace(/\:\d+$/, ''); // 移除端口号
if (req.protocol === 'http') {
res.redirect(307, `https://${host}${req.url}`);
} else {
next(); // 对于 HTTPS 请求,传递给下一个中间件
}
});

// 读取 SSL 证书,请使用自己存放的路径
const options = {
key: fs.readFileSync('/rx90.cn.key'),
cert: fs.readFileSync('/rx90.cn_bundle.crt')
};

// 检查端口是否可用
function checkPort(ip, port) {
if (port > 65535 || port < 1) {
return Promise.reject(new RangeError(`Port number ${port} is invalid. Try a number between 1 and 65535.`));
}

const server = net.createServer();

return new Promise((resolve, reject) => {
server.once('error', reject);
server.once('listening', resolve);
server.listen(port, ip);
}).then(() => {
server.close(); // 关闭临时创建的服务器
});
}

// 启动服务器
function startServer(server, port, ip) {
return new Promise((resolve, reject) => {
server.listen(port, ip, resolve);
server.on('error', reject);
}).then(() => server);
}

// 格式化地址
function formatAddress(ip, port, root) {
let hostname = ip;
if (ip === '0.0.0.0' || ip === '::') {
hostname = 'localhost';
}

return url.format({protocol: 'http', hostname: hostname, port: port, path: root});
}

module.exports = function(args) {
const app = connect();
const { config } = this;
const ip = args.i || args.ip || config.server.ip || undefined;
const defaultPort = 4000; // 默认端口
const port = parseInt(args.p || args.port || config.server.port || process.env.port || defaultPort, 10);
const { root } = config;

// 确保端口可用
return checkPort(ip, port)
.then(() => {
// 启动 HTTPS 服务器
const httpsServer = https.createServer(options, app);

// 启动 HTTP 重定向服务器
const httpRedirectServer = httpApp.listen(80, () => {
console.log(`HTTP redirect server listening on port 80, redirecting to HTTPS`);
});

return this.extend.filter.exec('server_middleware', app, {context: this})
.then(() => {
if (args.s || args.static) {
return this.load();
}

return this.watch();
})
.then(() => startServer(httpsServer, 443, ip)) // 启动 HTTPS 服务器,监听 443 端口
.then(server => {
const addrString = formatAddress(ip || 'localhost', 443, root);
this.log.info('Hexo is running at %s . Press Ctrl+C to stop.', chalk.underline(addrString));
this.emit('server');

if (args.o || args.open) {
open(addrString);
}

return server;
});
})
.catch(err => {
switch (err.code) {
case 'EADDRINUSE':
this.log.fatal(`Port ${port} has been used. Try other port instead.`);
break;

case 'EACCES':
this.log.fatal(`Permission denied. You can't use port ${port}.`);
break;
}

this.unwatch();
throw err;
});
};

在hexo安装目录执行 hexo s

ssl配置文件.zip


hexo部署
http://example.com/2024/09/20/f847114c3d1b40f1ad1bc410683715e5/
作者
John Doe
发布于
2024年9月20日
许可协议