LEGO Shop at Home: The World’s Biggest LEGO Shop
Raspberry Pi is capable to host a NodeJS server. We want to use node’s forever module to keep our node server running, even when system restarts.
Install forever
Install forever and use the node app as root:
sudo -i npm install forever -g
Test run your nodeJS server app:
forever start /var/www/simple-server.js
Start node server app on boot
- To start editing run the following replacing the “pi” with your desired runtime user for the node process. If you choose a different user other then yourself, you will have to run this with sudo.
$ crontab -u pi -e
- It will ask you which editor you wish to edit with. Select nano.
- Once in the editor add the following line:
@reboot /usr/bin/sudo -u pi -H /usr/local/bin/forever start /var/www/simple-server.js
- Save the file. You should get some feedback that the cron had been installed.
- For further confirmation of the installation of the cron, execute the following (again replacing “pi” with your target username) to list the currently installed crons:
$ crontab -u pi -l
Reference:
- http://blog.nodejitsu.com/keep-a-nodejs-server-up-with-forever
- http://stackoverflow.com/questions/13385029/automatically-start-forever-node-on-system-restart
(Visited 13,807 times, 1 visits today)
2 thoughts on “Run NodeJS server on boot with forever on Raspberry Pi”
Comments are closed.
Not working mate. Manually running ‘forever start [my-app-file].js’ in my project dir with the pi user works though.
It depends on what version of node.js you have installed on the pi. This was written 2 years ago, versions may not be compatible with the current Pi settings or there could be issues with user permissions / ownership of the file. You could change that by:
sudo chmod 777 /var/www/simple-server.js
sudo chown pi /var/www/simple-server.js
What was the error you got?
Try this: https://gist.github.com/leommoore/5795606 and let me know how you go