Skip to content

Latest commit

 

History

History
64 lines (40 loc) · 1.69 KB

run-node-app-on-ubuntu.md

File metadata and controls

64 lines (40 loc) · 1.69 KB

Run node web app on Ubuntu server

Short version of: http://stackoverflow.com/questions/18687850/setting-up-node-js-on-webfaction

Install steps

Remember: You should run node process as an unprivileged user!

  • Install node and npm

      sudo apt-get install node npm
    
  • Create web user

  • Link node to nodejs to make node and nodejs available in bash

      sudo ln -s /usr/bin/nodejs /usr/bin/node
    
  • Install forever. Which will keep the node app script running.

  • Place start-nodeapp.sh, stop-nodeapp.sh and restart-nodeapp.sh to /home/web. Scripts are below. Remember to add correct PATH, otherwise crontab will fail to start forever. WARNING: Running start script twice will spawn two processes! You should always run stop before start.

  • Setup starting of the node process when server starts

    Setup crontab as web user to make sure the nodejs does not have too much rights

    export EDITOR=nano
    crontab -e

    And add line: @reboot bash /home/web/start-nodeapp.sh

    Debug crontab by looking mails with web user using mail command.

start-nodeapp.sh

#!/bin/bash
HOME="/home/web"
DIRECTORY="/home/web/nodeapp"
export NODE_ENV="production"
PATH=/usr/local/bin:$PATH

forever start --uid "nodeapp" --minUptime 5000 --spinSleepTime 2000 --fifo --sourceDir $DIRECTORY/dist --workingDir $DIRECTORY/dist --append -l $HOME/nodeapp.forever.log -o $HOME/nodeapp.stdout.log -e $HOME/nodeapp.stderr.log server/app.js

stop-nodeapp.sh

#!/bin/bash
PATH=/usr/local/bin:$PATH

forever stop nodeapp

restart-nodeapp.sh

#!/bin/bash
PATH=/usr/local/bin:$PATH

forever restartall