Создаем в /etc/systemd/system/nodeserver.service
[Unit]
Description=My node Server
[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=/home/user/.nvm/versions/node/v14.15.3/bin/node ./src/index.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
Теперь можно запускать, останавливать и перезапускать приложение с помощью:
service nodeserver start
service nodeserver stop
service nodeserver restart
Чтобы сообщить systemd автоматически запускать узел при загрузке, просто введите:
systemctl enable nodeserver