本文基于群晖 DSM 7.4.1-90080 实现
在群晖 docker 中安装 NPM 时,希望使用 host 占用 80、443 端口,但默认情况下这两个端口被 DSM 占用,需要解除占用。
本文主要介绍群晖自带 Nginx 占用 80、443 端口的解除方式。
实现思路
修改 /usr/syno/share/nginx 目录下的 server.mustache、DSM.mustache、WWWService.mustache 三个文件。
将其中的 80、443 端口修改为其他端口以解除占用。
实现步骤
使用 SSH 登录群晖 NAS,按顺序执行以下命令:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
# 1. 切换至 root 用户
sudo -i
# 2. 检查端口占用情况(确保是自带的 Nginx 占用 80、443 端口)
netstat -anp | grep 80
netstat -anp | grep 443
# 3. 如果是自带的 Nginx 占用 80、443 端口,则进行下一步
cd /usr/syno/share/nginx
# 4. 备份三个文件
cp server.mustache server.mustache.bak
cp DSM.mustache DSM.mustache.bak
cp WWWService.mustache WWWService.mustache.bak
# 5. 修改三个文件的占用端口配置 (新端口自行定义,此处以 8080 替换 80, 8443 替换 443)
sed -i 's/\b80\b/8080/' server.mustache DSM.mustache WWWService.mustache
sed -i 's/\b443\b/8443/' server.mustache DSM.mustache WWWService.mustache
# 6. 重启 Nginx (待重启完后可以使用步骤 2 指令查看端口占用情况)
synosystemctl restart nginx
|
后续维护
DSM 系统更新等情况可能会还原这三个 mustache 模板文件,将端口变回 80、443,需要重新执行上述逻辑。
我们可以在第一次执行后,将该指令添加到任务计划里。
控制面板 -> 任务计划 -> 新增 -> 触发的任务 -> 用户定义的脚本:
常规:
- 任务名称:
Change Nginx Port
- 用户账号:
root
- 事件:
开机
任务设置:
运行指令:
1
2
3
|
sed -i -e 's/\b80\b/8080/' -e 's/\b443\b/8443/' /usr/syno/share/nginx/server.mustache /usr/syno/share/nginx/DSM.mustache /usr/syno/share/nginx/WWWService.mustache
synosystemctl restart nginx
|
回滚指令
当希望回滚操作时可执行如下指令恢复:
1
2
3
4
5
6
7
8
9
10
11
|
# 1. 切换至 root 用户
sudo -i
# 2. 恢复三个文件
cd /usr/syno/share/nginx
cp server.mustache.bak server.mustache
cp DSM.mustache.bak DSM.mustache
cp WWWService.mustache.bak WWWService.mustache
# 3. 重启 Nginx
synosystemctl restart nginx
|