NodeJS application and Systemd service for ordinary users
The task to setup some applications to be managed by systemd can be tricky. Specially because systemd has some restrictions that can not be grasp to understand at first glance.
In that example, it was tried to configure an application build with Express + NodeJs. The main issue related with the port that the app use, 70.
If you try to configurate systemd:
Jan 11 09:31:06 opi systemd[1]: Started nodejs_app web app.
Jan 11 09:31:14 opi node[9410]: events.js:174
Jan 11 09:31:14 opi node[9410]: throw er; // Unhandled 'error' event
Jan 11 09:31:14 opi node[9410]: ^
Jan 11 09:31:14 opi node[9410]: Error: listen EACCES: permission denied 0.0.0.0:70
Jan 11 09:31:14 opi node[9410]: at Server.setupListenHandle [as _listen2] (net.js:1263:19)
Jan 11 09:31:14 opi node[9410]: at listenInCluster (net.js:1328:12)
Jan 11 09:31:14 opi node[9410]: at Server.listen (net.js:1415:7)
Jan 11 09:31:14 opi node[9410]: at Function.listen (/home/webapp/opi-app/node_modules/express/lib/application.js:618:24)
Jan 11 09:31:14 opi node[9410]: at Object.<anonymous> (/home/webapp/opi-app/servidor-express.js:11:5)
Jan 11 09:31:14 opi node[9410]: at Module._compile (internal/modules/cjs/loader.js:778:30)
Jan 11 09:31:14 opi node[9410]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
Jan 11 09:31:14 opi node[9410]: at Module.load (internal/modules/cjs/loader.js:653:32)
Jan 11 09:31:14 opi node[9410]: at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
Jan 11 09:31:14 opi node[9410]: at Function.Module._load (internal/modules/cjs/loader.js:585:3)
Jan 11 09:31:14 opi node[9410]: Emitted 'error' event at:
Jan 11 09:31:14 opi node[9410]: at emitErrorNT (net.js:1307:8)
Jan 11 09:31:14 opi node[9410]: at process._tickCallback (internal/process/next_tick.js:63:19)
Jan 11 09:31:14 opi node[9410]: at Function.Module.runMain (internal/modules/cjs/loader.js:834:11)
Jan 11 09:31:14 opi node[9410]: at startup (internal/bootstrap/node.js:283:19)
Jan 11 09:31:14 opi node[9410]: at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)
Jan 11 09:31:14 opi systemd[1]: opi.service: Main process exited, code=exited, status=1/FAILURE
Jan 11 09:31:14 opi systemd[1]: opi.service: Failed with result 'exit-code'.
Only root user can bind ports bellow 1024 and even not using systemd it probably would be an issue. However that are some approaches to dealing with:
- Using libcap2
sudo apt install libcap2-bin
sudo setcap cap_net_bind_service=+ep `readlink -f \`which node\``
- Using IPTables
sudo sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 70 -j REDIRECT --to-port 3000
Peace!
Read other posts