更新时间:2023-02-08 18:35:52
2016 答案:几乎每个 Linux 发行版都带有 systemd,这意味着 forever、monit、PM2 等都没有不再需要 - 您的操作系统已经可以处理这些任务.
2016 answer: nearly every Linux distribution comes with systemd, which means forever, monit, PM2, etc. are no longer necessary - your OS already handles these tasks.
制作一个 myapp.service
文件(显然,用您的应用程序名称替换myapp"):
Make a myapp.service
file (replacing 'myapp' with your app's name, obviously):
[Unit]
Description=My app
[Service]
ExecStart=/var/www/myapp/app.js
Restart=always
User=nobody
# Note Debian/Ubuntu uses 'nogroup', RHEL/Fedora uses 'nobody'
Group=nobody
Environment=PATH=/usr/bin:/usr/local/bin
Environment=NODE_ENV=production
WorkingDirectory=/var/www/myapp
[Install]
WantedBy=multi-user.target
请注意,如果您是 Unix 新手: /var/www/myapp/app.js
应该有 #!/usr/bin/env 节点
在第一行并打开可执行模式chmod +x myapp.js
.
Note if you're new to Unix: /var/www/myapp/app.js
should have #!/usr/bin/env node
on the very first line and have the executable mode turned on chmod +x myapp.js
.
将您的服务文件复制到 /etc/systemd/system
文件夹中.
Copy your service file into the /etc/systemd/system
folder.
使用 systemctl daemon-reload
告诉 systemd 新服务.
Tell systemd about the new service with systemctl daemon-reload
.
使用 systemctl start myapp
启动它.
使用 systemctl enable myapp
使其在启动时运行.
Enable it to run on boot with systemctl enable myapp
.
使用 journalctl -u myapp
这取自 我们如何在 Linux 上部署节点应用程序,2018 版,其中还包括生成 AWS/DigitalOcean/Azure CloudConfig 以构建 Linux/节点服务器的命令(包括 .service
文件).