Hosting a Node.js application with Apache

nodejsSeveral months ago, I was very excited by the annouce of the establishment of one of the biggest if not the biggest data center in Canada near Montreal. In its offer, OVH proposes VPS at a very low entry price. It is very interesting to use thoses VPS for various applications among them hosting a web site is the most obvious one. As a node.js hobbyist, running my applications on Heroku is interesting, but hosting them on my own server would be event better ! However, how could I run theses applications on port 80 next to a main website. Well, there are two solutions. First one, I could use node.js as the main server and route eventually to the main server, or for convenience (and it is the solution proposed here) I could run Apache2 and route to local node.js applications.

So first of all, we need to install Apache2 and node.js on our Ubuntu 14.04 server :

The versions you will get are 2.4 for Apache2 and 0.10.x for node.js (at this date).

Next thing we'll have to do is proxy all request incoming on port 80 through the URL of a node.js application to the running local node.js process. For this, we need to install/enable mod_proxy and mod_proxy_http modules on the Apache server :

So know the fun begins. We need to configure the Apache server to proxy requests to node.js applications. We'll configure then a VirtualHost for this :

In this configuration, we first see that every request will be routed to our web site which code pages is found in the directory  /var/www/html/mySite . Next we have defined a reverse proxy so that a request to the  http://www.mySite.com/nodejsAppli will be proxy to a local node.js application running on port  8080 . And that's all ! Well, almost because we'll have to enable this new site configuration and we'll disable the default one

The last thing to do is to code a node.js application and launch it

 

 

and to run it

Leave a Reply

%d bloggers like this: