Node.js application cannot run on port 80, even if there is no other process blocking the port

I am running a Debian instance on Amazon EC2 with Node.js installed. If I run the code below:

http = require('http'); http.createServer(function (request, response){ response.writeHead(200, {'Content-Type':'text/plain'}); response.end('Hello World\n'); }).listen(80); console.log("Running server at port 80"); 

I get the output below that tells me that another process is listening on port 80:

 Running server at port 80 events.js:72 throw er; // Unhandled 'error' event ^ Error: listen EACCES at errnoException (net.js:901:11) at Server._listen2 (net.js:1020:19) at listen (net.js:1061:10) at Server.listen (net.js:1127:5) at Object.<anonymous> (/home/admin/nodetests/nodetest.js:6:4) at Module._compile (module.js:456:26) at Object.Module._extensions..js (module.js:474:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:312:12) at Function.Module.runMain (module.js:497:10) 

Now, when I check if there is a process (as root, if something is hidden), listening on port 80 with:

 netstat -tupln 

I get the bottom output which tells me that it is not listening on port 80:

 Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1667/sshd tcp6 0 0 :::22 :::* LISTEN 1667/sshd 

I should note that debian has port 80 open as an inbound rule, if that matters.

My question is: what am I doing wrong? Why can't I determine the listening process on port 80? Why is it blocked in Debian? What steps should be taken for the code to work properly?

+89
javascript linux debian amazon-ec2
Sep 22 '13 at 18:31
source share
8 answers

The EACCES error EACCES means that you do not have the proper permissions to run applications on this port. On Linux systems, any port below 1024 requires root access.

+190
Sep 22 '13 at 18:53 on
source share

Instead of running on port 80, you can redirect port 80 to the application port (> 1024) using

 iptables -t nat -I PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 3000 

This will work if your application runs on port 3000.

+67
Sep 22 '13 at 22:48
source share

Short answer: You can allow host access to this port using:

setcap 'cap_net_bind_service=+ep'/path/to/nodejs

long answer

Edit:

May not work on newer versions of the site

+17
Nov 24 '14 at 1:22
source share

Note that if you are running apache , you can create a reverse proxy on vhost. If your node is running on port 8080 :

 <VirtualHost 127.0.0.1:80> ServerName myLocalServer ProxyPass / http://localhost:8080/ ProxyPassReverse / http://localhost:8080/ </VirtualHost> 

Of course, add the server to /etc/hosts :

 127.0.0.1 myLocalServer 

You will need to enable the appropriate apache modules:

 sudo a2enmod proxy_html sudo a2enmod proxy_http sudo a2enmod proxy_connect sudo a2enmod proxy_ajp sudo service apache2 restart 

... and now you can connect to http://myLocalServer .

+6
May 12 '16 at 19:50
source share

For those looking for a quick and easy solution for a development environment , port forwarding through ssh can be a good alternative:

 ssh -L 80:localhost:3000 yourusername@localhost -N 

This redirects port 80 to the local host to port 3000 to the local host.

It must be run as root (privileged port). To cancel it, just press Ctrl-C in the terminal. (You can add the -f flag so that the command runs in the background, but then you need to find it again to destroy).

This solution requires that you have an ssh server running locally . This can be done quickly , but please remember the security implications if you are on a shared network. You might want to apply at least some level of additional security (disable password and root access).

I personally only use this on my local machine. I'm not sure how this affects the speed of processing your requests, if you run it in production, maybe someone has an idea. In any case, you need to make sure that this command continues to be executed all the time, which causes more headache. For production environments, I suggest using a reverse proxy such as nginx .

+2
Feb 27 '18 at 9:08
source share

I got the same error and I tried to run my application using sudo and this worked for me.

no court

 mansi@mansi:~/NodePractice$ node myFirst.js events.js:141 throw er; // Unhandled 'error' event ^ Error: listen EACCES 0.0.0.0:80 at Object.exports._errnoException (util.js:870:11) at exports._exceptionWithHostPort (util.js:893:20) at Server._listen2 (net.js:1224:19) at listen (net.js:1273:10) at Server.listen (net.js:1369:5) at Object.<anonymous> (/home/mansi/NodePractice/myFirst.js:6:4) at Module._compile (module.js:410:26) at Object.Module._extensions..js (module.js:417:10) at Module.load (module.js:344:32) at Function.Module._load (module.js:301:12) 

and with sudo

 mansi@mansi:~/NodePractice$ sudo node myFirst.js ^C 
0
Jan 14 '18 at 0:15
source share

Using the PORT 80 requires some special permissions. Using sudo before running the application statement solved my problem. for example, if you use npm to run your application, you can type sudo npm start

-one
Jan 02 '19 at 21:05
source share

The EACCES error EACCES means that you do not have sufficient rights to run applications on this port. On Linux systems, any port below 1024 requires root access.

Run the program with sudo permission. Run the sudo su command before starting the program.

-2
Feb 24 '18 at 12:05
source share



All Articles