- Published on
Running node.js (or other) services under systemd
- Authors
- Name
- Martin Andrews
- @mdda123
This is largely drawn from the excellent Digital Ocean guide, but with a little more detail around the systemd
specifics.
Using this method, there's no longer any need for forever
since systemd
will do the restarting automatically.
systemd
setup
Put into (for example) /etc/systemd/system/node-webserver.service
:
[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 :
systemctl daemon-reload
systemd
control
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 WorkingDirectory
.
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.