Greetings NSS noders...
This is a simple solution to hosting multiple node.js applications on one Ubuntu box. I haven't tested this with web sockets yet, but I'm sure it can be worked out. Email me with questions... enjoy.
INSTALLATION
-
Of course, point the DNS for your domain (example.com) to your server ip.
-
ssh into your server (this may vary depending on your setup)
$ ssh ubuntu@example.com
- Install the node forever module, globally
$ npm install forever -g
- Install the http-proxy module, globally
$ npm install http-proxy -g
*** This is a very powerful module, and can be configured many different ways...
https://github.com/nodejitsu/node-http-proxy
- Make a new folder in your sites folder called PROXY
$ mkdir /path to sites folder/PROXY
- Switch to that folder and create a package.json file, and an app.js file
$ cd /path to sites folder/PROXY $ touch package.json $ touch app.js
- Copy the contents of
app.js
andpackage.json
from this gist and paste into the corresponding files.
*** In app.js, change any instance of example.com to your host name. *** Change the router object to route domains or paths to their corresponding port, defined in your node app's config.js
- run an npm install in the PROXY folder
Now that everything is installed, let's configure forever to run your node apps and your proxy on server reboot...
CONFIGURATION
- Switch to your init directory
$ cd /etc/init
*** All commands here must be run as sudo
- Create the proxy init file and copy the contents of the attached
node-proxy.conf
file.
$ sudo touch /etc/init/node-proxy.conf
*** Be sure to change the /path-to-sites/
to match your setup.
- Create a .conf file for each app you'd like to run. Use the attached file
node-app1.conf
as an example.
$ sudo touch /etc/init/node-app1.conf
*** Be sure to change the path-to-sites
and path-to-app
to match your setup.
- Once these files are created, reboot your server.
$ sudo reboot
If everything has been configured properly, the proxy and your node app should be running in the background, using forever...
LOGGING
Logs are stored in the home directory for the user running forever, in this case ubuntu
$ ssh ubuntu@example.com $ cd ~/.forever
Running forever list
will show you a list of all your currently running forever processes.
$ forever list
Returns the following...
info: Forever processes running
data: uid command script forever pid logfile
data: [0] UFRS /path to node/node /path to app/app.js 18759 18832 /home/ubuntu/.forever/UFRS.log
Find the log file, in this case /home/ubuntu/.forever/UFRS.log
$ tail -f /home/ubuntu/.forever/UFRS.log
This will give you a realtime view of the log file as it updates, very much like running node app.js on your local machine
UPDATING
Certainly we don't want to reboot the machine every time we pull from github.
$ forever stop /path-to-app/app.js
Pull from github
$ forever start /path-to-app/app.js
It's that simple. Add as many apps as you like. Essentialy, this setup removes the need to mess with firewall settings, as it takes all incoming HTTP requests on port 80 and routes them to specific ports. It hasn't failed me yet!