Allowing node.js applications to run on port 80

Solution 1:

in order to avoid this error, you can resolve the non-symlink executable with which node, as full example:

sudo apt-get install libcap2-bin
sudo setcap cap_net_bind_service=+ep `readlink -f \`which node\``

the "which" command shows the full path of shell commands.

Solution 2:

Figured it out. Turns out however I installed node, created a sym-link in /usr/bin/node which pointed to another sym-link in /etc/alternatives/node which pointed to another sym-link in /usr/bin/nodejs.

Running the command against /usr/bin/nodejs worked.


Solution 3:

FWIW, another option is to use authbind. Authbind uses a slightly different mechanism to achieve similar ends to CAP_NET_BIND_SERVICE. I.e. allows non-privileged apps to use privileged ports.

Install from apt:

sudo apt-get update && sudo apt-get install authbind

Assuming the desired app.js is running under non-privileged user "user" and you wish to bind to port 80:

sudo touch /etc/authbind/byport/80
sudo chown user:user /etc/authbind/byport/80
sudo chmod 500 /etc/authbind/byport/80

Then run your app like this:

authbind node app.js

If you instead wish to use something like "forever" (essentially daemonizes node apps), then this is the go:

authbind --deep forever app.js