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 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
cd /etc/init
vim node-app.conf- IMPORTANT: whatever filename you just picked is what you will use to start|stop|restart your service i.e.
service node-app start - Give the file 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'});