CHH-WANG

Development

主要想解决的问题

为了更好隔离,专门开了个 nginx 的账户来存储 nginx 使用的静态网页。本来想不允许登陆该账户,但从 目前资料来看,如果在 passwd 中配置了 nologin Shell,则会导致连 ssh 都会登陆失败。想对于 bash,使 用 rssh 并不能带来安全级别提升。故当前方案保留了 bash,通过配置 ssh 只允许 rsync 命令。

方案细节

创建 bash 脚本rsynconly.sh

脚本内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
#!/bin/bash

# make sure the command is rsync
set -- ${SSH_ORIGINAL_COMMAND}
if [ "$1" != "rsync" ] ; then
echo "Error: Only rsync command supportted"
exit 1
fi

# excute the command directly
${SSH_ORIGINAL_COMMAND}

exit 0

把该文件保存到路径/bin/rsynconly.sh,注意加上执行权限chmod +x /bin/rsynconly.sh.

修改sshd_config

找到 Match User 的地方,没有的话最后一行就可以,添加内容如下:

1
2
3
4
5
Match User nginx
X11Forwarding no
AllowTcpForwarding no
PermitTTY no
ForceCommand /bin/rsynconly.sh

这里其实就是对用户 nginx 的 ssh 连接进行限制,强制运行刚刚创建的脚本/bin/rsynconly.sh

重启 sshd 服务

systemctl restart sshd

到此为止,整个搭建就完成了。可以愉快的使用 rsync 通过 ssh 传文件,并同时禁用了 ssh 直接登陆和执 行其他命令。

一些细节

当使用 rsync 上传文件时,会先 ssh 登陆远程主机。可通过rsync -vvv source_file nginx@ip.address: 看到,其实调用了命令ssh -l nginx ip.address rsync --server -vvve.LsfxCIvu . ., 故在脚本 /bin/rsynconly.sh中的${SSH_ORIGINAL_COMMAND}是字符串:rsync --server -vvve.LsfxCIvu . .

选择

纠结了下,还是选择hexo-filter-mathjax + hexo-renderer-pandoc方案。主要原因:

  • hexo-filter-mathjax 可以直接用$, 相比hexo-math在书写起来会丝滑一点
  • hexo-renderer-pandoc 不用转义_

所以就这么选择了。

安装

按官方指引来,很是顺利.

奇怪的是我不安安装hexo-renderer-pandoc不转义_也可以生成成功, 如下面的Example。 那就先不安装hexo-reader-pandoc先吧, 后面有问题再说。

Example

代码:

1
2
3
- Inline: $\sum_{i=0}^{i<100}{x_i}$
- NewLine:
$$ \sum_{i=0}^{i<100}{x_i}$$

效果:

  • Inline:
  • NewLine:

过了一段时间没用了

背景

没有网络前后端开发经验,没接触过Vue,懒得去研究代码了。看到Next主题支持Waline,还是直接上干脆一点。

安装配置

经过了前面的折腾,Next的安装和配置就非常简单了,按官网一步步来就好了。

  1. 安装NexT主题: NexT Install
  2. 安装Waline-Next: Hexo Waline-Next

Nginx配置调整

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
	location / {
root /home/nginx/srv/http/hexo/public;
index index.html index.htm;
}

# proxy to 8360
location /waline/ {
proxy_pass http://127.0.0.1:8360/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header REMOTE-HOST $remote_addr;
add_header X-Cache $upstream_cache_status;
# cache
add_header Cache-Control no-cache;
expires 12h;
}

# proxy to 8360
# Solute auth redirect issue
# 如果没有这段,登陆会失败
location /api/ {
proxy_pass http://127.0.0.1:8360;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header REMOTE-HOST $remote_addr;
add_header X-Cache $upstream_cache_status;
# cache
add_header Cache-Control no-cache;
expires 12h;
}

最后

如果想快速的在新机器上用,就把waline的配置放到站点的根目录并存入仓库。

目标

通过初步比对,决定先利用当前VPS + Docker + Sqlite搭建Waline评论系统。选择Waline是因为很多人用,并且评论无需注册,暂无安全漏洞。

由于Docker和Sqlite对我来说都是新鲜事物,所以看官方文档相对比较抽象点,就写一写步骤了。所以站在docker小白视角,总结一下。

安装步骤

在VPS上安装Docker和Sqlite

这个按VPS操作系统的环境安装即可。记得启动Docker的Daemon。

随便找一个目录

按官方要求,执行下面3个动作:

1
2
3
git clone https://github.com/lizheming/waline.git
cd waline
docker build -t lizheming/waline -f packages/server/Dockerfile .

第1,2两条指令就不用多说了,对docker不了解的人看第3条指令就读天书一样(好在是执行不出状况),如果手敲的话不要漏了后面的’.'。

实际上第3条指令就是创建镜像了。-t选项是标签名,后面docker启动就靠它了。-f 指定dockerfile, dockerfile的内容就是构建镜像的指令和说明。

随便建一个目录

建一个目录为了干净一点,没其他目的,也就是说不是必要的。但要编辑一个docker-compose.yml文件:

1
2
3
mkdir tmp
cd tmp
vi docker-compose.yml

文件内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# docker-compose.yml
version: '3'

services:
waline:
container_name: waline
image: lizheming/waline:latest
restart: always
ports:
- 127.0.0.1:8360:8360
volumes:
- /home/nginx/data:/app/data # ':'前面的是实际路径,后面的是映射到容器中的路径
environment:
TZ: 'Asia/Shanghai'
SQLITE_PATH: '/app/data' # 容器中的路径
SQLITE_DB: 'waline' # 只用文件名,不用带后缀
JWT_TOKEN: 'Just Random 9834 token' # 随机值就行,还没深入理解怎么用好
SITE_NAME: 'chhwang'
SITE_URL: 'http://www.chhwang.uk'
SECURE_DOMAINS: 'chhwang.uk'
AUTHOR_EMAIL: 'chh.wang@hotmail.com'

把sqlite文件放到刚刚配置的目录

按官网要求,把刚刚clone目录里面的../asset/waline.sqlite拷贝到自己配置的目录:完整路径是/home/nginx/data/waline.sqlite.

启动docker

为了减少不必要的问题,直接用root启动吧:sudo docker compose up. 这时候,评论系统没什么问题的话就可以工作了,输入 http://localhost:8360 可以看到效果。

NGINX反向代理

这个就没啥坑了, 先把网站暂停下,直接拷配置就可以看到效果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
	# proxy to 8360
location / {
proxy_pass http://127.0.0.1:8360;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header REMOTE-HOST $remote_addr;
add_header X-Cache $upstream_cache_status;
# cache
add_header Cache-Control no-cache;
expires 12h;
}
}

遗漏问题

如何将评论与文章关联起来?

遇到的问题

SQLITE

问题提示: SQLITE_ERROR: no such table: wl_Comment

错误配置如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# docker-compose.yml
version: '3'

services:
waline:
container_name: waline
image: lizheming/waline:latest
restart: always
ports:
- 127.0.0.1:8360:8360
volumes:
- ${PWD}/data:/home/nginx/app/data
environment:
TZ: 'Asia/Shanghai'
SQLITE_PATH: '/home/nginx/app/data'
SQLITE_DB: 'waline.sqlite'
JWT_TOKEN: 'Just Random 9834 token'
SITE_NAME: 'chhwang'
SITE_URL: 'http://www.chhwang.uk'
SECURE_DOMAINS: 'chhwang.uk'
AUTHOR_EMAIL: 'chh.wang@hotmail.com'

这里面有两个坑,主要是对Docker不熟悉引起:

  • 坑1

我的配置是在SQLITE_DB后的名字用了’waline.sqlite’, 多加了sqlite后缀,去掉就好.

  • 坑2

volumesSQLITE_PATH含义没理解清楚,配置错误。volumes的值的意思是将本地目录和容器目录映射。 SQLITE_PATH是容器内部路径。

把坑1和坑2填平后,配置如上一节描述。

参考链接

在搭建过程中参考了 https://waline.js.org/en/guide/deploy/vps.html https://dzyx.uk/2023/01/22/39/ https://vickey.fun/2022/08/09/Waline-Migration-From-CloudBase-to-Docker/ https://www.zdynb.cn/2022/shi-yong-docker-bu-shu-waline.html

问题

Git 和 Nginx不是同一个用户,现在需要他们联系上,以便在Git有更新后Nginx可自动以更新网站内容。第一个方案是Git通过访问ngnix服务器,ngnix通过cgi运行shell脚本更新。

CGI搭建

阅读全文 »

查看下当前Nginx进程情况

1
2
3
4
$ ps -aux | grep nginx
root 2290 0.0 0.2 13136 2148 ? Ss Jul17 0:00 nginx: master process /usr/bin/nginx -g pid /run/nginx.pid; error_log stderr;
http 2291 0.0 0.4 13576 4204 ? S Jul17 0:01 nginx: worker process
user 7093 0.0 0.2 6580 2304 pts/2 S+ 10:24 0:00 grep nginx

nginx 的work线程默认用http用户在跑。

1
2
$ cat /etc/passwd | grep http
http:x:33:33::/srv/http:/usr/bin/nologin

单独给Nginx分配个账户

对安全不是很懂,就按照能分就分的原则,把Nginx和http用户解耦一下,毕竟用Nginx不一定就是http。

1
2
3
4
5
$ sudo useradd -d /home/nginx -s /usr/bin/nologin nginx
$ sudo mkdir /home/nginx
$ sudo mkdir -p /home/nginx/srv/http
$ sudo chown -R nginx /home/nginx
$ sudo chgrp -R nginx /home/nginx/*

账户和目录建好了,把网页拷到http目录下就好了。

配置Nginx

...表示省略一些东西。注意不要忘了语句以;结尾(要不然服务启动不成功)。

文件: /etc/nginx/nginx.conf

1
2
3
4
5
6
7
8
9
...
user nginx;
...
location / {
root /home/nginx/srv/http;
index index.html index.htm;
}
...

改好后运行 systemctl restart nginx, 搞定。

1
2
3
4
$ ps -ef | grep nginx
root 7573 1 0 14:32 ? 00:00:00 nginx: master process /usr/bin/nginx -g pid /run/nginx.pid; error_log stderr;
nginx 7574 7573 0 14:32 ? 00:00:00 nginx: worker process
user 7778 6959 0 14:41 pts/2 00:00:00 grep nginx

好, worker process是用nginx 跑了。

小结

到此为止,git和nginx可以用各自账户跑了。接下来把git和nginx搭上线吧。

0%