1

**I have seen a similiar question here, with no proper answer

I want both Apache and Node.js to run on port 80, so that it won't be necessary to type port number at the address bar. But my Node.js app won't work when I listen on port 80 instead of 3000, I guess that's because Apache is already on 80.

Or, the solution requires to actually use other port than 80 and somehow hide it?

  • 1
    Possible duplicate of [how to put nodejs and apache in the same port 80](https://stackoverflow.com/questions/11172351/how-to-put-nodejs-and-apache-in-the-same-port-80) – stdunbar May 02 '18 at 21:31

1 Answers1

1

In TCP, you can only run 1 service per port. As soon the port is assigned to a service, it becomes unavailable to anybody else.

However there is a way to share port betwen NODE and APACHE, proxying the connections using an Apache 2 Module (mod_proxy and mod_proxy_http). You can get more details here: how to put nodejs and apache in the same port 80

Apache configuration example to use Apache in requests to http://example.com/ and Node.js for requests to http://example.com/node/:

<VirtualHost *:80>
    ServerName example.com
    DocumentRoot /var/www/example/
    <Location /node>
        ProxyPass http://127.0.0.1:8124/
        ProxyPassReverse http://127.0.0.1:8124/
    </Location>
</VirtualHost>
colxi
  • 7,640
  • 2
  • 45
  • 43
  • Then how can I integrate a node.js project with an existing website, so that it will look like part of the domain,like domain/app and not with domain:3000/app instead? –  May 02 '18 at 21:15
  • 1
    You can use a subdomain, in another IP, for example. – colxi May 02 '18 at 21:16
  • Got it, so if I want a node.js chat app for example, I will have to be using a different ip when I host my website at some hosting service, and point both ip's to the same domain? (instead of a sub-domain, if I want it to be on the same address) –  May 02 '18 at 21:18
  • 1
    In the solution i propose you, you should create a subdomain (chat.yourdomain.com) and point it with DNS to another different IP (not your main Apache server), where you would have your Node server. This is a simple solution, to resolve your problem. A more advanced solution, could be proxying the connections. I personally prefer the subdomain one. Keep it simple. – colxi May 02 '18 at 21:23
  • 2
    That isn't needed @colxi - Apache can proxy by path. – stdunbar May 02 '18 at 21:24
  • So there's a solution without a different ip? (So that I can test it on localhost) - Is it possible to write it as an answer, or that's too detailed for an answer here? –  May 02 '18 at 21:26
  • i updated my answer, with the apache config needed to proxify your connections – colxi May 02 '18 at 21:31