0

I have an express node server running on aws ec2 instance. When I run

sudo node server.js

the server runs on port 80 and works fine when I make an http request to the server. However, when I run

sudo pm2 start server.js

I get the following error whenever I make a request

{ Error: listen EACCES: permission denied 0.0.0.0:80
at Server.setupListenHandle [as _listen2] (net.js:1242:19)
at listenInCluster (net.js:1307:12)
at Server.listen (net.js:1395:7)
at Object.<anonymous> (/home/ubuntu/evercampus-server/server.js:19:8)
at Module._compile (internal/modules/cjs/loader.js:816:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:827:10)
at Module.load (internal/modules/cjs/loader.js:685:32)
at Function.Module._load (internal/modules/cjs/loader.js:620:12)
at Object.<anonymous> (/usr/lib/node_modules/pm2/lib/ProcessContainerFork.js:32:23)
at Module._compile (internal/modules/cjs/loader.js:816:30)
code: 'EACCES',
errno: 'EACCES',
syscall: 'listen',
address: '0.0.0.0',
port: 80 }

I tried using pm2 to run on port 5000 and it works fine.

I am very confused on why sudo node server.js works but sudo pm2 start server.js does not.

I saw on another post (Running Node app via PM2 on port 80) about running the server on port 5000 and forwarding requests from port 80 to 5000. However, I want to understand why pm2 won't work on port 80.

Punisher
  • 654
  • 5
  • 21

1 Answers1

1

Because PM2 will invoke Node without sudo. Yes, PM2 will run under root but that does not necessarily mean the actual Node process will run under root also.

D. Richard
  • 460
  • 3
  • 12