This is largely drawn from the excellent Digital Ocean guide, but with a little more detail around the
Using this method, there's no longer any need for
systemd will do the restarting automatically.
Put into (for example)
[Service] ExecStart=[node binary] [main file.js] # Example #ExecStart=/usr/bin/node app.js Restart=always StandardOutput=syslog StandardError=syslog SyslogIdentifier=node-webserver User=srv-node-sample Group=srv-node-sample # Could also be : #User=nginx #Group=nginx Environment=NODE_ENV=production WorkingDirectory=<FULL-PATH-TO-WORKING-DIRECTORY> [Install] WantedBy=multi-user.target
If you make changes to the .service file, you should to execute :
If everything is set up properly, the service can be controlled with :
# Do these for simple testing systemctl start node-webserver systemctl status node-webserver systemctl stop node-webserver # and, for across-reboot operations : systemctl enable node-webserver systemctl disable node-webserver
Other things to watch out for...
Also, remember to set permissions for the configured User/Group to access the
Also, note that to make a webserver (
nginx, say) talk to your backend server (
node, say), SELINUX needs :
/usr/sbin/setsebool -P httpd_can_network_connect true
where the '-P' option makes this work across reboots.