Running a NodeJS Server as a Daemon on Ubuntu

By Kip Lawrence, published 2010-09-24

So, you've got your new shiny node application coded up and working. Now what? How do you deploy it. I'm going to show how to make it run as a daemon on server start-up. It will store a pid file in /var/run and log and error files in /var/log/. First, we need to get daemon.node and put it in /usr/local/lib/node so our node applications can see it: [bash] mkdir ~/tmp cd ~/tmp git clone http://github.com/Slashed/daemon.node.git cd daemon.node node-waf configure build sudo cp build/default/* /usr/local/lib/node/ cd rm -rf ~/tmp [/bash] Second, let's create a directory were we will store our node application: [bash] sudo mkdir /usr/local/nodejs cd /usr/local/nodejs [/bash] Third copy the following file into /usr/local/nodejs/. It contains a simple server you can use to test. Just copy your application in to the file after the comment replacing the simple application. [javascript] #!/usr/local/bin/node var daemon = require('daemon'); var fs = require('fs'); var http = require('http'); var sys = require('sys'); var config = { lockFile: '/var/run/nodeapp.pid' }; var args = process.argv; var dPID; switch(args[2]) { case "stop": process.kill(parseInt(fs.readFileSync(config.lockFile))); process.exit(0); break; case "start": dPID = daemon.start(); daemon.lock(config.lockFile); break; default: sys.puts('Usage: [start|stop]'); process.exit(0); } // Your application goes below this comment! var express = require('express'); var app = express.createServer(express.logger()); app.get('/', function(req, res){ res.send('Hello World'); }); app.listen(9001); [/javascript] We need to make the file executable: [bash] sudo chmod +x nodeapp [/bash] Fourth, we need to create a startup script. Copy the following file and place it in /etc/init.d/nodeapp [bash] #! /bin/sh case "$1" in start) /usr/local/nodejs/nodeapp start > /var/log/nodeapp.log 2> /var/log/nodeapp.err ;; stop) /usr/local/nodejs/nodeapp stop ;; restart) /usr/local/nodejs/nodeapp stop /usr/local/nodejs/nodeapp start > /var/log/nodeapp.log 2> /var/log/nodeapp.err ;; force-reload) # No-op ;; *) echo "Usage: nodeapp {start|stop|restart|force-reload}" >&2 exit 3 ;; esac : [/bash] Make it executable: [bash] sudo chmod +x nodeapp [/bash] Now we simply need to tell Ubuntu to run it on startup. Execute the following bash commands: [bash] sudo update-rc.d nodeapp defaults [/bash] We are done! Let's test it! Exexute the following commands to startup your nodeapp and then watch the log: [bash] sudo /etc/init.d/nodeapp start tail -f /var/log/nodeapp.log [/bash] In a browser hit http://localhost:9001/ if you are using the simple application or your port number if you pasted your application in.

About the Author

Name:
Kip Lawrence
Location:
Lindon, UT
Github:
mightykip
Twitter:
mightykip

Kip Lawrence is a software developer who lives in Lindon, Utah. Computers started as a hobby for him and later turned in to a career. His first programming language was Basic on a Commodore 64. Since those early days he has dabbled in assembly, Pascal, C, C++, Python, Java, Objective-C, Perl and JavaScript. He currently works as a Senior Software Engineer at Domo Technologies, Inc.

About UtahJS.com

UtahJS is the website for the Utah JavaScript Users Group that meets to discuss and enjoy JavaScript!

JavaScript in the browser, on the server or just about anywhere else.

We love JavaScript but quite often talk about other topics such as CSS, HTML, cross browser coding, the internet… pretty much, anything that we find interesting.

Everyone is welcome. Please join us!