Error: listen EACCES 0.0.0.0:80 OSx Node.js
Asked Answered
P

5

61

I'm following a tutorial in a angularJS book and have to setup a server. This is the server.js file:

 var express = require('express');
  var app = express();
   app.use('/', express.static('./'));
    app.listen(80);

I get this error:

$ node server.js
events.js:154
      throw er; // Unhandled 'error' event
      ^

Error: listen EACCES 0.0.0.0:80

I know already, that the Error EACCES means that i don't have access rights to the port 80, but i don't know how to fix this. Any help much appreciated!

Para answered 28/1, 2016 at 18:1 Comment(3)
change the port to something > 1024, e.g. app.listen(8080) or start the server as root, sudo node server.js.Claar
Yes that worked. Thank you @Claar i changed the port. Dow you know why i get this error with the port 80?Para
On many systems you need to be root (or similar) to open the ports below 1024. So your choices are pick a port above 1024 or, if you have the permissions, run your code as root.Penny
S
63

If you need to run the server on port 80 you should use a reverse proxy like nginx that will run using a system account on a privileged port and proxy the requests to your Node.js server running on an unprivileged port (> 1024).

When running in development environment you're pretty much free to run as root (ie. sudo node server.js), but that is rather dangerous in production environment.

Here's a sample nginx config that will see if the request is for a file that exists in the filesystem, and if not, proxy the request to your Node.js server running on port 9000

upstream yournodeapp {
  server localhost:9000 fail_timeout=0;
  keepalive 60;
}

server {
  server_name localhost;
  listen 80 default_server;

  # Serve static assets from this folder
  root /home/user/project/public;

  location / {
    try_files $uri @yournodeapp;
  }

  location @yournodeapp {
    proxy_pass http://yournodeapp;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
  }
}
Snuff answered 28/1, 2016 at 18:51 Comment(3)
ugh. isn't there some way to proxy using node itself?Pember
Nginx or Apache is not necessary. There is a great number of Node proxies: redbird, http-proxy-middleware. To fix the above issue you most likely need to SUDO script execution: sudo node server.jsViscid
@Pember see my answer below... using node itselfHumfrey
H
47

to give root access to node and start server on port 80 you can do

sudo node app.js

this will start the server giving permission on port 80

Hypothetical answered 4/9, 2016 at 19:21 Comment(1)
Advising to run as root introduces unnecessary security risks. This is ill-advised.Humfrey
H
20

Foremost, do not run as root. That's asking for 'it'. "It" is really bad. Go see the movie. Then, don't run your node web project as root.

// DO NOT DO THIS!
$ sudo node app.js

// DO NOT DO THIS EITHER!
$ sudo su -
# node app.js

Instead, use PM2 and authbind to do this:

// %user% is whatever user is running your code
sudo touch /etc/authbind/byport/80
sudo chown %user% /etc/authbind/byport/80
sudo chmod 755 /etc/authbind/byport/80

Next, add this alias to your ~/.bash_aliases or ~/.bashrc or ~/.bash_profile:

alias pm2='authbind --deep pm2'

Then, try it with pm2:

pm2 start app.js
Humfrey answered 10/5, 2018 at 4:20 Comment(1)
touch: cannot touch ‘/etc/authbind/byport/80’: No such file or directorySahara
J
2

On Windows I fixed this by setting Express to listen on port 8080 for HTTP and 8443 for HTTPS. It really doesn't like using those lower number ports. Also I have IIS installed and running so it might be some sort of port conflict there too.

Joan answered 22/10, 2017 at 15:15 Comment(1)
IIS usually grabs port 80 for the Default site.Perrins
W
0

there is

sudo ufw allow 80/tcp

and it should be port forwarded, worked for me atleast

Waine answered 1/7, 2020 at 9:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.