Search code examples
node.jsexpresspm2

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


I have a node.js express app running on an Ubuntu 21.04 server. I run the app with pm2 with a non root user, the app listens on ports 80 and 443, I had the error Error: bind EACCES null:80. As mentionned in the pm2 documentation, I performed all instructions for ports 80 and 443 and it works like a charm. I also ran pm2 save and pm2 startup systemd to make the app autostart when server reboot.

But when the server reboot: pm2 list shows myApp with an online status, but I encounter again the error: Error: bind EACCES null:80 and the app is not working.

Then if I pm2 kill and pm2 start myApp again, then it works well...

Do you know why on server reboot the pm2 starts not well but if I start it manually it's ok? Thanks


Solution

  • I found the solution in this post from the pm2 team on github, this is because of the authbind when listening on port 80.

    Edit the config file created by the pm2 startup procedure (the file should be at /etc/systemd/system/pm2-<your-user>.service, and add /usr/bin/authbind --deep at the begining of the 3 commands ExecStart, ExecReload and ExecStop, leaving the rest of the command. For example in my config file:

    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
    

    Then app auto-restarts well with non root user after server reboot!