Linux 将 node.js + expressjs 应用程序的 NODE_ENV 设置为 ubuntu 下的守护进程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7022742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
setting NODE_ENV for node.js + expressjs application as a daemon under ubuntu
提问by pkyeck
i got the daemon working alright with these instructions: http://kevin.vanzonneveld.net/techblog/article/run_nodejs_as_a_service_on_ubuntu_karmic/
我让守护进程按照以下说明正常工作:http: //kevin.vanzonneveld.net/techblog/article/run_nodejs_as_a_service_on_ubuntu_karmic/
but because this starts the application in DEVELOPMENT mode, the log file gets spammed with socket.io debug logs.
但是因为这会在 DEVELOPMENT 模式下启动应用程序,所以日志文件会收到 socket.io 调试日志的垃圾邮件。
i tried setting the NODE_ENV to production in the upstart-conf-file but had no success.
我尝试在 upstart-conf 文件中将 NODE_ENV 设置为生产,但没有成功。
script
export HOME="/root"
export NODE_ENV=production
exec /usr/local/bin/node /where/yourprogram.js >> /var/log/node.log 2>&1
end script
didn't work.
没有用。
采纳答案by Yuval
Try
尝试
exec NODE_ENV=production /usr/local/bin/node /where/yourprogram.js >> /var/log/node.log 2>&1
In my setup I'm sudoing as a lesser user, so it's
在我的设置中,我 sudoing 作为一个较小的用户,所以它是
exec sudo -u some-user NODE_ENV=production /usr/local/bin/node /where/yourprogram.js >> /var/log/node.log 2>&1
and since it's spawning off another user it probably has another environment. I'm a newbie here, but it works for me.
并且由于它是由另一个用户产生的,因此它可能有另一个环境。我是这里的新手,但它对我有用。
回答by TiansHUo
If you are using node.js in production, I recommend you use forever.js to daemonize your program https://github.com/nodejitsu/forever
如果您在生产中使用 node.js,我建议您使用forever.js 来守护您的程序 https://github.com/nodejitsu/forever
Install using npm: [sudo] npm install forever -g
使用 npm 安装: [sudo] npm install forever -g
export NODE_ENV=production
and run forever start app.js
You can also specify where to put error and stdout logs.
export NODE_ENV=production
并运行forever start app.js
您还可以指定放置错误和标准输出日志的位置。
回答by Peter Lyons
Here's a simpler upstart script you can use. Upstart now supports everything you need to do directly without script sections or too much embedded shell syntax. This includes environment variables (env
), working directory (chdir
), user/group (setuid
, setgid
), log handling (console log
), etc. Your log files will be handled and rotated into /var/log/upstart/your_app.log
这是您可以使用的更简单的新贵脚本。Upstart 现在支持您无需脚本部分或太多嵌入式 shell 语法即可直接执行的所有操作。这包括环境变量 ( env
)、工作目录 ( chdir
)、用户/组 ( setuid
, setgid
)、日志处理 ( console log
) 等。您的日志文件将被处理并轮换到/var/log/upstart/your_app.log
description "start and stop the example express.js/node.js server"
author "John Doe <[email protected]>"
start on filesystem and started networking
respawn
console log
chdir /opt/your_app
setuid your_app_user
setgid your_app_user
env PATH=./node_modules/.bin:./node/bin:/usr/bin
env NODE_ENV=production
exec app/server.js
回答by Connor Leech
to set NODE_ENV in heroku use:
在 heroku 中设置 NODE_ENV 使用:
heroku config:set NODE_ENV="production"
回答by jayjfadd
Ubuntu/Upstart are listed in the question, but I got here while looking for answers for a FreeBSD/system shell daemon.
问题中列出了 Ubuntu/Upstart,但我是在寻找 FreeBSD/系统 shell 守护程序的答案时来到这里的。
The line below started the app in "development" environment:
下面的行在“开发”环境中启动了应用程序:
exec node path/to/start/script.js
The line below started the app in "production" environment:
下面的行在“生产”环境中启动了应用程序:
NODE_ENV=production exec node path/to/start/script.js
It took me a while to figure this out, so I thought I'd share.
我花了一段时间才弄清楚这一点,所以我想我会分享。