如何将 Node.js 应用程序作为自己的进程运行? [英] How do I run a Node.js application as its own process?
问题描述
部署 Node.js 的最佳方式是什么?
What is the best way to deploy Node.js?
我有一个 Dreamhost VPS(这就是他们所说的 VM),而且我已经能够安装 Node.js 并设置代理.只要我保持打开节点时的 SSH 连接,这就会很有效.
I have a Dreamhost VPS (that's what they call a VM), and I have been able to install Node.js and set up a proxy. This works great as long as I keep the SSH connection that I started node with open.
推荐答案
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
文件).
这篇关于如何将 Node.js 应用程序作为自己的进程运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!