一、问题
网站绑定域名后直接通过域名访问使用的是80端口,因此tomcat须监听80端口,而为了安全起见tomcat一般不用root身份运行,综上,需要以普通用户来运行监听80端口的tomcat。此时就会启动失败,报没有权限,因为只有root身份才能监听1024以下的熟知端口。
二、解决
(以下未经验证)
There are a few different solutions to work around this:
- Install and configure Apache or nginx as a reverse proxy server, which can be started as root to open the port, and then downgrade its privileges back to a normal user.
- Set up a firewall on the server using
iptables
or an alternative, so that the lower port number is forwarded internally to a higher port number listened by Confluence.- Use jsvc, which is able to open ports as root, and then downgrade privileges.
- Use authbind to grant privileges for a non-root user to open a privileged port.
1、通过iptables进行端口转发
- tomcat监听8080(其他非熟知端口皆可)端口,直接执行 sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080 将对80端口的请求转发到8080端口。
- iptables规则设置后都是即时生效的,但在机器重启后iptables中的配置信息会被清空。因此可以将这些配置保存下来,让iptables在interface启动时自动被加载:
(1)保存防火墙规则: sudo iptables-save > /etc/zsmiptables.rules
(2)编辑/etc/network/interfaces,在末尾加一行:pre-up iptables-restore < /etc/zsmiptables.rules
参考资料:
(前者言将iptables-restore < /etc/zsmiptables.rules放到一脚本里置于/etc/network/if-pre-up.d/下,但一直不成功;改用后者所言将iptables-restore < /etc/zsmiptables.rules加到/etc/network/interfaces末尾成功了)
2、通过isvc
jsvc能以root角色使用端口,因此借助之即可。另外,这种方式也把tomcat做成了服务,能够开机自己启动。