Serving Node.JS app on a existing server running page on port 80

node.jsvirtualhost

I have a server at www.example.com running a PHP-made webpage served by Apache, this page is listening to port 80.

Now I want to serve my Node.JS on the domain www.example2.com. Both pages should be on the same server, the Node.JS app should be running on port 3000. How do I achieve this?

From other answers and blog posts (like this one: http://garr.me/blog/running-node-js-and-apache-together-using-mod_proxy/) I have learnt that I can create a ReverseProxy to redirect www.example.com/app to my app, however this is not the intended behavior. What I want is this new domain www.example2.com to go the server's ip address at port 3000.

Side question: This question might sound stupid but, can't I redirect the whole domain to the server's IP address at port 3000 from the domain name configuration at GoDaddy/Route53???

Thanks.

Best Answer

I solved it. The page on the original question gives a good advice however it doesn't seems complete. To achieve this configuration do the following:

-Locate the file httpd.conf on your Apache folders (usually on apache2/conf).

-Add the following lines to the end:

LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
<VirtualHost *:80>
  ProxyPreserveHost On
  ProxyRequests Off
  ServerName example2.com
  ServerAlias www.example2.com
  ProxyPass / http://localhost:3000/
  ProxyPassReverse / http://localhost:3000/
</VirtualHost>

-Change the placeholder "example2" for your actual domain name (example2 is the domain name on the original question)

-Restart apache.

Note that you might want to tidy the code a little bit by moving the LoadModule lines to where the modules are actually loaded.

Related Topic