仅在服务器重启时自动启动后,PM2 才能到达 port:80

PM2 can't reach port:80 only after autostart on server reboot

我在 Ubuntu 21.04 服务器上有一个 node.js Express 应用程序 运行ning。 我 运行 非 root 用户使用 pm2 的应用程序,该应用程序侦听端口 80 和 443,我有错误 Error: bind EACCES null:80。 正如 pm2 documentation 中提到的那样,我执行了端口 80 和 443 的所有指令,它运行得非常棒。 我还 运行 pm2 savepm2 startup systemd 让应用程序在服务器重启时自动启动 .

但是当服务器重启时: pm2 list 显示 myApp 在线状态,但我再次遇到错误:Error: bind EACCES null:80 并且应用程序无法运行。

然后,如果我再次 pm2 killpm2 start myApp,那么效果很好...

你知道为什么在服务器重启时pm2启动不好但是如果我手动启动它就可以了吗? 谢谢

我在 this post from the pm2 team on github, this is because of the authbind when listening on port 80 中找到了解决方案。

编辑由pm2 startup procedure创建的配置文件(该文件应位于/etc/systemd/system/pm2-<your-user>.service,并在3个命令ExecStart、ExecReload和ExecStop的开头添加/usr/bin/authbind --deep ,留下命令的其余部分。例如在我的配置文件中:

ExecStart=/usr/bin/authbind --deep /usr/lib/node_modules/pm2/bin/pm2 resurrect
ExecReload=/usr/bin/authbind --deep /usr/lib/node_modules/pm2/bin/pm2 reload all
ExecStop=/usr/bin/authbind --deep /usr/lib/node_modules/pm2/bin/pm2 kill

然后应用程序在服务器重新启动后自动重新启动非 root 用户!