Running NodeJS as services in Debian or Ubuntu is quite easy, especially we are using tools like “runit”. The only requirements here is make sure your “node” located in “/usr/local/bin”, so it’s can be called globally.
We need to install Runit by :
1 | sudo apt-get install runit |
Now, we ready to configure nodejs applications running as services.
1. Create folder services in /etc
We need to make folder “/etc/sv” ownership into our user (not root). In debian we are using “admin” or in Ubuntu using “ubuntu”.
1 2 3 | sudo chown admin:admin /etc/sv/ cd /etc/sv && mkdir pongjs && cd pongjs touch run && vim run |
Now we setup run scripts :
1 2 3 | #!/bin/sh exec 2>&1 exec chpst -U admin:admin runsvdir /home/admin/service |
Now, we need to create “service” folder in “/home/admin”
1 2 | cd ~/ && mkdir service && cd service && mkdir pongjs && cd pongjs touch run && vim run |
Now, we set the “run” script that will running our “nodejs” script:
1 2 | #!/bin/sh exec sudo -i -u admin node /var/yodi/pongnodejs/pongserver.js |
1 2 3 4 5 | chmod a+x /home/admin/service/pongjs/run chmod a+x /etc/sv/pongjs/run sudo ln -s /etc/sv/pongjs /etc/service/ sudo chown -R admin:admin -R /etc/service/pongjs sv start pongjs |
Check if nodejs run by “ps aux | grep nodejs”:
1 2 3 4 | ps aux | grep nodejs root 14291 0.0 0.2 3520 1496 ? S 15:55 0:00 sudo -i -u admin node /var/yodi/pongnodejs/pongserver.js admin 14292 0.0 2.2 37616 13664 ? Sl 15:55 0:00 node /var/yodi/pongnodejs/pongserver.js admin 14364 0.0 0.1 3472 740 pts/0 S+ 17:00 0:00 grep nodejs |