Sök…


Introduktion

Till skillnad från många webbservrar installeras inte Node som en tjänst utanför rutan. Men i produktionen är det bättre att det körs som en dæmon som hanteras av ett init-system.

Node.js som ett systemdemon

systemd är de facto init-systemet i de flesta Linux-distributioner. När Node har konfigurerats för att köra med systemd är det möjligt att använda service att hantera det.

Först och främst behöver den en konfigurationsfil, låt oss skapa den. För Debian-baserade distrikt är det i /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

Det är nu möjligt att starta, stoppa och starta om appen med:

service node start
service node stop
service node restart

För att säga systemd att automatiskt starta nod på start, skriv bara: systemctl enable node .

Det är allt, noden körs nu som en dæmon.



Modified text is an extract of the original Stack Overflow Documentation
Licensierat under CC BY-SA 3.0
Inte anslutet till Stack Overflow