Search code examples
node.jsnginxpm2

nodejs, nginx: listen EACCES: permission denied 0.0.0.0:300


I am developing a Nodejs application and hosting on AWS ubuntu instances with pm2 and Nginx. Something went wrong with the application so I check pm2 logs and I found this

0|index    | Error: listen EACCES: permission denied 0.0.0.0:300
0|index    |     at Server.setupListenHandle [as _listen2] (node:net:1723:21)
0|index    |     at listenInCluster (node:net:1788:12)
0|index    |     at Server.listen (node:net:1876:7)
0|index    |     at Function.listen (/home/ubuntu/twitter-helper/nodejs/node_modules/express/lib/application.js:635:24)
0|index    |     at Object.<anonymous> (/home/ubuntu/twitter-helper/nodejs/index.js:353:5)
0|index    |     at Module._compile (node:internal/modules/cjs/loader:1254:14)
0|index    |     at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
0|index    |     at Module.load (node:internal/modules/cjs/loader:1117:32)
0|index    |     at Module._load (node:internal/modules/cjs/loader:958:12)
0|index    |     at Object.<anonymous> (/usr/lib/node_modules/pm2/lib/ProcessContainerFork.js:33:23) {
0|index    |   code: 'EACCES',
0|index    |   errno: -13,
0|index    |   syscall: 'listen',
0|index    |   address: '0.0.0.0',
0|index    |   port: 300
0|index    | }

The problem is that I don't use port 300 anywhere in my application so I don't know where this port number comes from an how to fix it?

My nodejs code:

const PORT = process.env.PORT || 3001
app.listen(PORT, ()=>{console.log(`listen on port ${PORT}`)})

Nginx config:

server {
        listen 80;
        listen [::]:80;

        root /home/ubuntu/app/react/frontend/build;
        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;

        server_name something something here;

        location / {
                try_files $uri /index.html;
        }

        location /api {
            proxy_pass http://127.0.0.1:3001;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
        }
}

Solution

  • Non-privileged user (not root) can't open a listening socket on ports below 1024.

    Unfortunately, unless you sign on as root, you’ll normally have to use a URL like

    http://ip:port - where port number > 1024.

    Check this https://www.digitalocean.com/community/tutorials/how-to-use-pm2-to-setup-a-node-js-production-environment-on-an-ubuntu-vps#give-safe-user-permission-to-use-port-80

    use this command

    sudo apt-get install libcap2-bin

    sudo setcap cap_net_bind_service=+ep `readlink -f \`which node\``