Zoeken…


Invoering

In tegenstelling tot veel webservers wordt Node niet direct als een service geïnstalleerd. Maar in de productie is het beter om het als een daemon te laten draaien, beheerd door een init-systeem.

Node.js als een systemd dæmon

systemd is het feitelijke init-systeem in de meeste Linux-distributies. Nadat Node is geconfigureerd om te worden uitgevoerd met systemd, is het mogelijk om de service gebruiken om het te beheren.

Allereerst heeft het een configuratiebestand nodig, laten we het maken. Voor op Debian gebaseerde distributies zal dit in /etc/systemd/system/node.service

[Unit]
Description=My super nodejs app

[Service]
# set the working directory to have consistent relative paths
WorkingDirectory=/var/www/app

# start the server file (file is relative to WorkingDirectory here)
ExecStart=/usr/bin/node serverCluster.js

# if process crashes, always try to restart
Restart=always

# let 500ms between the crash and the restart
RestartSec=500ms

# send log tot syslog here (it doesn't compete with other log config in the app itself)
StandardOutput=syslog
StandardError=syslog

# nodejs process name in syslog
SyslogIdentifier=nodejs

# user and group starting the app
User=www-data
Group=www-data

# set the environement (dev, prod…)
Environment=NODE_ENV=production


[Install]
# start node at multi user system level (= sysVinit runlevel 3) 
WantedBy=multi-user.target

Het is nu mogelijk om de app respectievelijk te starten, stoppen en opnieuw te starten met:

service node start
service node stop
service node restart

Om systemd te vertellen dat de node automatisch moet opstarten tijdens het opstarten, typ je gewoon: systemctl enable node .

Dat is alles, knoop draait nu als een daemon.



Modified text is an extract of the original Stack Overflow Documentation
Licentie onder CC BY-SA 3.0
Niet aangesloten bij Stack Overflow