本文最后更新于:2024年1月14日 晚上

Waline 本可以部署在 Vercel 等平台上,但有一天突然就不能用了,本文记录自己部署基于 MongoDB 数据库的 Waline 服务。

背景

  • Vercel 不香了之后,自己建 Waline 服务器
  • 数据库之所以选择 MongoDB,主要在于安装使用方便,对于网站存取性能更优

自己动手,丰衣足食,数据安全,速度还快,无人可挡!

配置环境

安装 MongoDB

创建 MongoDB 用户

  • 进入 mongosh
  • 切换到新数据库 waline
1
2
3
test> use foo
switched to db foo
waline>
  • 创建用户

    在数据库 admin (官方建议使用 admin 数据库) 中创建用户 vvd 密码 xxxxxx,赋予他在 admin 数据库上的 root 权限

1
2
3
4
5
6
7
8
9
db.createUser({
user: 'vvd',
pwd: 'xxxxxx',
roles:[{
role: 'root',
db: 'admin'
}]
})

成功运行命令返回结果:

1
{ ok: 1 }
  • 运行 show users 命令查看结果
1
2
3
4
5
6
7
8
9
10
11
foo> show users
[
{
_id: 'admin.vvd',
userId: UUID("91cab2c3-5fae-42fb-9cd4-3c971ff58e3f"),
user: 'vvd',
db: 'admin',
roles: [ { role: 'root', db: 'admin' } ],
mechanisms: [ 'SCRAM-SHA-1', 'SCRAM-SHA-256' ]
}
]

创建环境变量

  • 根据官方文档

  • 如果和我一样是本地 MongoDB 的话,我们最主要需要配置: MONGO_DBMONGO_USERMONGO_PASSWORD 三个变量

  • 修改 ~/.bashrc,按照之前的配置加入上述变量

    此处三个是最基本的变量,如果需要配置其他内容依法炮制即可

1
2
3
export MONGO_DB=admin
export MONGO_USER=vvd
export MONGO_PASSWORD=123456

至此环境配置完毕

本地安装 Waline

安装、运行

  • 按照官方文档(真的很方便啊)
  • 在服务器找一个没人的空地方,运行命令:
1
2
npm install @waline/vercel
node node_modules/@waline/vercel/vanilla.js
  • 正常运行返回信息
1
2
3
4
5
6
waline$ node node_modules/@waline/vercel/vanilla.js
[2022-08-31T17:17:30.583] [296133] [INFO] - Server running at http://127.0.0.1:8360
[2022-08-31T17:17:30.585] [296133] [INFO] - ThinkJS version: 3.2.14
[2022-08-31T17:17:30.586] [296133] [INFO] - Environment: production
[2022-08-31T17:17:30.586] [296133] [INFO] - Workers: 1
[2022-08-31T17:22:07.683] [296140] [INFO] -

测试

  • 此时可以访问 http://IP:8360 访问 waline 页面:

测试评论

  • 此时后台信息输出
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[2022-08-31T17:22:25.285] [296140] [DEBUG] - Post Comment Start!
[2022-08-31T17:22:25.285] [296140] [DEBUG] - Post Comment initial Data: {
link: 'www.zywvvd.com',
mail: 'zywvvd@mail.ustc.edu.cn',
nick: 'VVD',
pid: 'undefined',
rid: 'undefined',
ua: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36',
url: '/',
comment: '测试?<img class="wl-emoji" src="//unpkg.com/@waline/emojis@1.0.1/weibo/weibo_joy.png" alt="weibo_joy">',
ip: '::ffff:180.171.132.122',
insertedAt: '2022-08-31T09:22:25.285Z',
user_id: 'undefined'
}
  • 说明我们 Waline 配置成功

创建 systemd 服务后台运行

直接运行 node 命令是可以顺利启动 waline 服务的,但是用了后台启动的方式就会报错 Not Initialized,原因未知,做错误记录

  • 将刚刚成功运行 waline 的文件夹移到 /usr/local/waline
  • 在此处创建运行脚本 setup.sh
1
2
3
4
5
6
7
#!/bin/bash

export MONGO_DB=admin
export MONGO_USER=vvd
export MONGO_PASSWORD=123456

/usr/bin/node node_modules/@waline/vercel/vanilla.js

之所以要创建脚本,是因为要将数据库的环境变量添加进去,否则可能链接不上数据库,报错 Not Initialized

  • /lib/systemd/system 文件夹创建 waline.service 文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[Unit]
Description = Service for waline backend
After = docker.service

[Service]
ExecStart = /bin/bash setup.sh
WorkingDirectory = /usr/local/waline/
StandardOutput = inherit
StandardError = inherit
Restart = always
User = lighthouse

[Install]
WantedBy=multi-user.target

  • 设置 waline 开机自启动
1
sudo systemctl enable waline.service
  • 开启 waline 服务
1
sudo systemctl start waline.service
  • waline 就可以随开机自动启动后台服务了

脚本后台运行

  • 使用 tmux \ screen 等工具在服务器后台运行也是可以的

  • 只不过坏处是服务器重启了还得进去运行一下 waline

使用大佬的仓库

也可以使用大佬的仓库一键部署,确实很方便,只要环境变量配置成其他数据库的也可以支持非 MySQL 的数据库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
MONGO_DB=admin
MONGO_USER=vvd
MONGO_PASSWORD=xxxxxx
SC_KEY=SCT70260Tsda5ikj9v8XZ7YcSU1o6qas
MAIL_SUBJECT_ADMIN=又见苍岚新评论通知
MAIL_SUBJECT=又见苍岚回复通知
SENDER_NAME=又见苍岚 - 通知
SENDER_EMAIL=xxxxx@qq.com
SMTP_PORT=465
SMTP_HOST=smtp.qq.com
AUTHOR_EMAIL=zywvvd@mail.ustc.edu.cn
SITE_URL=https://www.zywvvd.com
SITE_NAME=又见苍岚
SMTP_PASS=kdfgy5frzebjge
SMTP_USER=xxxxx@qq.com
SMTP_SERVICE=qq

Nginx 反向代理

  • 我用的 Docker 部署的 Nginx
  • 目标是将 Docker 的 4443 端口映射到服务器的端口,以 6666 为例
  • 配置好 ssl 证书
  • 在 Nginx 配置文件目录 /etc/nginx/conf.d 中加入文件 waline.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
server {
listen 4443 ssl;
listen [::]:4443 ssl;
# server_name localhost;

ssl_certificate /ssl/server.crt;
ssl_certificate_key /ssl/server.key;

location / {
proxy_set_header X-FORWARDED-FOR $remote_addr;
proxy_set_header X-FORWARDED-PROTO $scheme;
proxy_set_header Host $http_host;
proxy_pass http://<your_ip>:6666;
}
}

  • your_ip 填入服务器的公网 IP 地址即可

  • 之后可以 ssl 访问 waline

部署到 Fluid 主题 Hexo 博客

  • 记住自己的 waline url https://IP:port
  • 修改主题配置文件 _config.fluid.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# Waline
# 从 Valine 衍生而来,额外增加了服务端和多种功能
# Derived from Valine, with self-hosted service and new features
# See: https://waline.js.org/
waline:
serverURL: 'https://IP:port'
path: window.location.pathname
meta: ['nick', 'mail', 'link']
requiredMeta: ['nick']
lang: 'zh-CN'
emoji: ['https://cdn.jsdelivr.net/gh/walinejs/emojis/weibo']
dark: 'html[data-user-color-scheme="dark"]'
wordLimit: 0
pageSize: 10
locale:
placeholder: 山高路远,常来玩吖 ~
  • 部署博客,测试评论

  • 新版 Waline placeholder 需要加在 local 下面,感谢 Gary “Noob” Z. 同学的分享。

    加了新 placeholder 后

参考资料



文章链接:
https://www.zywvvd.com/notes/hexo/theme/fluid/fluid-my-waline/my-waline/


“觉得不错的话,给点打赏吧 ୧(๑•̀⌄•́๑)૭”

微信二维码

微信支付

支付宝二维码

支付宝支付

Fluid -25- 独立部署基于 MongoDB 的 Waline
https://www.zywvvd.com/notes/hexo/theme/fluid/fluid-my-waline/my-waline/
作者
Yiwei Zhang
发布于
2022年8月31日
许可协议