Skip to content

Instantly share code, notes, and snippets.

@brnacl
Last active July 27, 2023 20:03
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save brnacl/8007810 to your computer and use it in GitHub Desktop.
Save brnacl/8007810 to your computer and use it in GitHub Desktop.
Host multiple Node.js apps on single ubuntu server

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

  1. Of course, point the DNS for your domain (example.com) to your server ip.

  2. ssh into your server (this may vary depending on your setup)

$ ssh ubuntu@example.com

  1. Install the node forever module, globally

$ npm install forever -g

  1. 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
  1. Make a new folder in your sites folder called PROXY

$ mkdir /path to sites folder/PROXY

  1. 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

  1. Copy the contents of app.js and package.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

  1. 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

  1. Switch to your init directory

$ cd /etc/init

*** All commands here must be run as sudo

  1. 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.

  1. 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.

  1. 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!

var httpProxy = require('http-proxy');
var options = {
router: {
'example.com': '127.0.0.1:3000',
'example.com/site1': '127.0.0.1:3000'
}
};
var proxyServer = httpProxy.createServer(options);
proxyServer.listen(80);
# start node
start on runlevel [2345]
# stop on system halt or reboot
stop on runlevel [016]
# start the application with the user `ubuntu`
exec sudo -u ubuntu -i NODE_ENV=production forever start /path-to-sites/path-to-app/app.js
description "Start node.js PROXY SERVER on reboot"
author "Brian Arenz <brianarenz@gmail.com>"
# start node
start on runlevel [2345]
# stop on system halt or reboot
stop on runlevel [016]
# start node app.js
exec sudo forever start /path-to-sites/PROXY/app.js
{
"name": "PROXY-SERVER",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node app.js"
},
"dependencies": {
"http-proxy": "*"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment