Buscar..


Introducción

A diferencia de muchos servidores web, Node no se instala como un servicio fuera de la caja. Pero en producción, es mejor tenerlo ejecutado como un demonio, gestionado por un sistema init.

Node.js como un sistema de demonio

systemd es el sistema inicial de facto en la mayoría de las distribuciones de Linux. Después de que Node se haya configurado para ejecutarse con systemd, es posible usar el comando de service para administrarlo.

En primer lugar, necesita un archivo de configuración, vamos a crearlo. Para las distribuciones basadas en Debian, estará en /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

Ahora es posible iniciar, detener y reiniciar la aplicación respectivamente con:

service node start
service node stop
service node restart

Para indicarle a systemd que inicie automáticamente el nodo en el arranque, simplemente escriba: systemctl enable node .

Eso es todo, el nodo ahora se ejecuta como un demonio.



Modified text is an extract of the original Stack Overflow Documentation
Licenciado bajo CC BY-SA 3.0
No afiliado a Stack Overflow