Skip to content

Instantly share code, notes, and snippets.

@sandfox
Last active September 19, 2018 03:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sandfox/5419948 to your computer and use it in GitHub Desktop.
Save sandfox/5419948 to your computer and use it in GitHub Desktop.
Simple example upstart config for nodejs

This example assumes your running a recent Ubuntu with upstart installed and you have install n from npm.

To see an example use of this ina a wider context, look at this gist for deploying node.js + nginx

Adapt as required.

  1. Put the node.conf in /etc/init/
  2. Create a folder for individual site configs to live in /etc/node
  3. Put the node-test.conf inside /etc/node
  4. Create the folder for the logs /var/logs/node
  5. Put some the test code from index.js inside a folder at /var/www/node-test
  6. Start the app by running sudo start node NAME=node-test
  7. Stop/restart/status the app by running sudo [restart|stop|status] node NAME=node-test
  8. Profit

##cons

  • It requires making a new config every time you need a new site
  • It's config is pretty verbose
  • There could do to be more awesome-sauce in the upstart script.
  • port configurations for multiple sites is pain unless done automatically by some tool externally

##pros

  • doesn't use forever
  • is pretty flexible and un-opinionated
  • fits in with the rest of system process monitoring shizzle
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(process.env.PORT, '127.0.0.1');
console.log('Server running at http://127.0.0.1:' + process.env.PORT);
NODE_VERSION="0.8.23"
NODE_PATH="/var/www/node-test"
LOG_PATH="/var/log/node/node-test.log"
NODE_SCRIPT="index.js"
NODE_ENV="production"
PORT=8005
description 'node sites upstart script'
author 'sandfox'
start on (local-filesystems and net-device-up)
stop on shutdown
instance "Node - $NAME"
respawn
respawn limit 5 60
script
#Loads the config file the name provided
. /etc/node/$NAME.conf
chdir ${NODE_PATH}
exec sudo -u www-data NODE_ENV=${NODE_ENV} PORT=${PORT} /usr/bin/n use ${NODE_VERSION} ${NODE_SCRIPT} >> ${LOG_PATH} 2>&1
end script
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment