Skip to content

Instantly share code, notes, and snippets.

Embed
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
respawn
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');
@aHumanBeing

This comment has been minimized.

Copy link

@aHumanBeing 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?

@oddoye-david

This comment has been minimized.

Copy link

@oddoye-david oddoye-david commented Feb 14, 2016

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

@sasijp

This comment has been minimized.

Copy link

@sasijp 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

@matt212

This comment has been minimized.

Copy link

@matt212 matt212 commented Mar 14, 2016

Hi,

Can anyone help me configure below script on machine restart on ubuntu 14.04 lts
https://gist.github.com/jobsamuel/6d6095d52228461f3c53

@joepie91

This comment has been minimized.

Copy link

@joepie91 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.

@paradoxall

This comment has been minimized.

Copy link

@paradoxall paradoxall commented Sep 30, 2016

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

log to root
su

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

and write this

[Unit]
Description=Start <appname> node.js app

[Service]
ExecStart=/usr/local/bin/node /home/deploy/<app name>/bin/www
Restart=always

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 multi-user.target <servicename>.service

@manuelgomezo

This comment has been minimized.

Copy link

@manuelgomezo manuelgomezo 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