Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save alexventuraio/aa109db9509c574317a3ec5d9f17da8d to your computer and use it in GitHub Desktop.

Select an option

Save alexventuraio/aa109db9509c574317a3ec5d9f17da8d to your computer and use it in GitHub Desktop.
Deploy Node.js app on Ubuntu as Upstart Service - instead of using Forever

Deploying a node app with Forever is great...until your server restarts unexpectedly. Then your app stops running and you have to redeploy.

To get around this, we're going to run our node app as a service. Services are great, because, once started, the system auto-restarts them if they fail, or if the server restarts.

###Step 1: Create a service for your node app

  • ssh in as root ssh root@youripaddress
  • Create a node-app.conf file in /etc/init - whatever name you pick here is what you will use to start|stop|restart your service
cd /etc/init
vim node-app.conf
  • Give it the following contents
start on filesystem and started networking
respawn
chdir /home/deploy/node-app
env NODE_ENV=production
env PORT=3000
exec /usr/local/bin/node bin/www
  • Now your app will always start on reboot!
  • You can also now you can start | stop | restart your app with these commands
service node-app start
service node-app stop
service node-app restart

###Step 2: give your deploy user permission to run services without a password

echo "deploy ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers

###Step 3: on Adjust your flightplan.js file View full file, minus these changes here

remote.log('Reload application');
remote.sudo('ln -snf ~/' + tmpDir + ' ~/'+appName, {user: username});
remote.sudo('service node-app restart', {user: 'root'});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment