4

When I try to start node on port 80, the error tells me that the port is in use. I imagine that's Apache.

What is the proper way to "take over" port 80, and keep it that way after a server restart?

(Linux xxxx.__.com 2.6.32-5-amd64 #1 SMP Tue Jun 14 09:42:28 UTC 2011 x86_64 GNU/Linux)

dsp_099
  • 5,801
  • 17
  • 72
  • 128

5 Answers5

11

you can use ip tables to map port 80 to 8000

iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8000

to make it permanent

sudo sh -c "iptables-save > /etc/iptables.rules"

and add

pre-up iptables-restore < /etc/iptables.rules

to your /etc/network/interfaces

supernova
  • 3,814
  • 3
  • 22
  • 37
  • can you explain a little what's going on with the last two commands? after reboot, i can't seem to connect to the server from the browser – dsp_099 Sep 09 '13 at 19:17
  • it restores iptable rules on reboot, otherwise the iptables rule would just last until you reboot your server. have your node app listen on port 8000. – supernova Sep 09 '13 at 19:35
  • Can changing iptables in that fashion make it impossible to connect to the server via ssh? I've done it, rebooted via /sbin/reboot and now the server appears dead - times out on connection – dsp_099 Sep 09 '13 at 19:41
  • 1
    yea if you misconfigured your /etc/network/interface file, the interface won't come up again. test it with ifdown eth0 && ifup eth0, so you won't lock yourself out. – supernova Sep 09 '13 at 19:46
  • I've added the pre-up line line of interface file. Rebooted, server started fine. Rebooted again, dead. Server issue unrelated to iptables? – dsp_099 Sep 09 '13 at 19:53
  • well if it was still working after the reboot the interface file should be okay, otherwise the server would have been unresponsive after the first reboot. – supernova Sep 09 '13 at 19:57
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/37059/discussion-between-supernova-and-dsp-099) – supernova Sep 09 '13 at 19:59
2

To take over port 80 when another process is listening on it, you must kill the process (or somehow tell it to stop listening). To ensure that Apache doesn't try to listen on port 80 again the next time it starts, you need to edit its configuration or prevent it from starting up.

To see which process is listening on port 80, run sudo netstat -ntap and look for the row with Local Address ending in port :80. The PID of the process (and the name) is in the far right column.

sqs
  • 115
  • 1
  • 6
1

you can use node.js with node-http-proxy check this link How to use vhosts alongside node-http-proxy? and How do I run Node.js on port 80?

Thanks & Regards,
Alok

Community
  • 1
  • 1
linux_fanatic
  • 4,767
  • 3
  • 19
  • 20
1

A constantly running unused apache maybe a security hole, in any case no sense in running unused services.

On the chance you're on ubuntu, this what I used..

sudo service apache2 stop
sudo update-rc.d apache2 remove
C B
  • 12,482
  • 5
  • 36
  • 48
1

You can access port 80 once you stop the service currently using it.

In your case, follow these steps:

1) Use systemctl to stop apache2:

sudo systemctl stop apache2

2) Check apache2 status:

sudo systemctl status apache2

Or just by entering http://localhost in your browser. If you get an error, you are good to go.

ERR_CONNECTION_REFUSED

3) Now start your NodeJS server on port 80.

4) You can access your server at http://localhost

UPDATE

If you are seeing errors on your console, try node preceding with sudo For eg. sudo node server.js

Here are the errors

events.js:137
      throw er; // Unhandled 'error' event
      ^

Error: listen EACCES 0.0.0.0:80
    at Object._errnoException (util.js:1003:13)
    at _exceptionWithHostPort (util.js:1024:20)
    at Server.setupListenHandle [as _listen2] (net.js:1349:19)
    at listenInCluster (net.js:1407:12)
    at Server.listen (net.js:1495:7)
    at Object.<anonymous> (/home/abdus/Desktop/voice-recognition/test.js:7:4)
    at Module._compile (module.js:660:30)
    at Object.Module._extensions..js (module.js:671:10)
    at Module.load (module.js:573:32)
    at tryModuleLoad (module.js:513:12)
Abdus
  • 192
  • 3
  • 10