At NodeSummit 2013, I first learned about Strider CD, an open source Continuous Deployment server built with Node.js.

After installing it, I decided that I wanted it running as a service and Upstart looked like the best way to go. On top of that I setup nginx as the proxy in front of the Strider server. After some research on upstart, I put together the script listed below, which will also work for other node.js apps.

Update (March 18, 2014): Updated to include creating and using a strider user. Since www-data does not have a home dir, if you need to use git and ssh to retreive npm dependencies, then it is a problem.

Update (March 26, 2014: The $HOME env var is required to be present and accurate in your upstart script. I updated the script to reflect this.

On Ubuntu, you might have to install a few packages before installation:

$ sudo apt-get install make build-essential
$ sudo apt-get install mongodb

Next install Strider:

$ sudo npm install -g strider
$ strider addUser

Open up a new file in the upstart config directory /etc/init/strider.conf.

#!upstart

description "Strider CD server."  
author "djensen47"

start on startup  
stop on shutdown

respawn  
#respawn limit 5 30

setuid strider  
setgid strider  
env HOME="/home/strider"  
env SERVER_NAME="<server name>"  
env NODE_ENV=production  
env PORT=8001  
env PLUGIN_BITBUCKET_APP_KEY="<bitbucket app key>"  
env PLUGIN_BITBUCKET_APP_SECRET="<bitbuket secret>"  
env PLUGIN_BITBUCKET_HOSTNAME="http://<host>"  
env SMTP_HOST="<SMTP hostname>"  
env SMTP_USER="<SMTP username>"  
env SMTP_PASS="<SMTP password>"  
env SMTP_FROM="Strider <noreply@yourdomainhere.com>"


script  
    echo $$ > /tmp/strider.pid
    exec strider >> /var/log/strider.sys.log 2>&1
end script


pre-start script  
    # Date format same as (new Date()).toISOString() for consistency
    echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Starting" >> /var/log/strider.sys.log
end script


pre-stop script  
    rm /tmp/strider.pid
    echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Stopping" >> /var/log/strider.sys.log
end script  

Now create the log file and give access to the www-data user.

$ sudo touch /var/log/strider.sys.log
$ sudo chown strider:strider /var/log/strider.sys.log

The following is the configuration that I use for nginx to proxy requests to the srider server:

server {  
    listen 80;
    server_name <hostname>;

    access_log  /var/log/nginx/<hostname>.access.log;
    error_log  /var/log/nginx/<hostname>.error.log;

    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header HOST $http_host;
        proxy_set_header X-NginX-Proxy true;

        proxy_pass http://127.0.0.1:8002;
        proxy_redirect off;
    }
}

Now start the strider upstart service:

$ sudo start strider

Upstart Resources

I found the following links useful for putting this script together.