Short version of: http://stackoverflow.com/questions/18687850/setting-up-node-js-on-webfaction
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
andnodejs
available in bashsudo 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
andrestart-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 rightsexport 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