Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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 re-deploy.

To get around this, we're going to run our node app as an Upstart service. Upstart 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
    IMPORTANT: whatever filename you pick is what you will use to start|stop|restart your service i.e. service node-app start
nano /etc/init/node-app.conf
  • Give the file the following contents
start on filesystem and started networking
chdir /home/deploy/node-app
env NODE_ENV=production #change this to staging if this is a staging server
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
start node-app
stop node-app
restart node-app #performs a stop and start. This is all we need for deployments

###Step 2: give your deploy user permission to restart the node-app service without requiring a password You can make changes to this entry later by running visudo as root, but for now, just run this.

echo "deploy ALL=(root) NOPASSWD: /sbin/restart node-app" >> /etc/sudoers

###Step 3: Adjust your flightplan.js file - remove the forever lines, replacing them with the last line here View full file, minus these changes here

remote.log('Reload application');
remote.sudo('ln -snf ~/' + tmpDir + ' ~/'+appName, {user: username});
remote.exec('sudo restart node-app');
Copy link

aHumanBeing commented Sep 1, 2015

stop and restart always result in "Unknown instance". Therefore, fly production always fails in the last step. If I run sudo start node-app manually, it works. Any advice?

Copy link

oddoye-david commented Feb 14, 2016

Now that forever is out, does the process restart if it crashes?

Copy link

sasijp commented Feb 24, 2016

stop and restart always result in "Unknown instance", if the instance is not already running. so replace the last line with following

echo "deploy ALL=(root) NOPASSWD: /sbin/stop node-app, /sbin/start node-app" >> /etc/sudoers

Copy link

matt212 commented Mar 14, 2016


Can anyone help me configure below script on machine restart on ubuntu 14.04 lts

Copy link

joepie91 commented Jun 22, 2016

This Gist is DANGEROUS.

If you use this Gist as-is, it will run your application as root, which is a serious security issue. You will want to use setuid to run it under a limited user (you can use the adduser command to create one), like demonstrated in this post.

Copy link

paradoxall commented Sep 30, 2016

in ubuntu dist > 15.04 they uses systemd instead of upstart so my solution was

log to root

nano /lib/systemd/system/<service name>.service

and write this

Description=Start <appname> node.js app

ExecStart=/usr/local/bin/node /home/deploy/<app name>/bin/www

to start and stop service use

service <service name> start
service <service name> stop

to make it start with boot

cd /lib/systemd/system/
sudo systemctl enable <service name>
systemctl add-wants <servicename>.service

Copy link

manugo-dev commented Oct 8, 2017

Really works @paradoxall Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment