新贵 env 节没有为 Node.js 应用程序设置环境变量(如 NODE_ENV)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8902856/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:02:20  来源:igfitidea点击:

Upstart env stanza not setting environment variables (like NODE_ENV) for Node.js application

node.jsubuntuenvironment-variablesupstart

提问by Chris F

I have an Upstart script for my server that looks like this:

我的服务器有一个 Upstart 脚本,如下所示:

description "myapp node.js server"

start on runlevel [2345]
stop on shutdown

env NODE_ENV=production
env CUSTOM=somevalue
exec sudo -u nodejs /usr/local/bin/node /opt/myapp/app.js >> /var/log/nodejs/myapp.log 2>&1

post-start script
    NODE_PID=`status myapp | egrep -oi '([0-9]+)$' | head -n1` 
    echo $NODE_PID > /var/run/nodejs/myapp.pid
end script

However, the app doesn't see NODE_ENV set to production. In fact, if I console.log(process.env) within the app, I don't see NODE_ENV or CUSTOM. Any ideas what's happening?

但是,应用程序没有看到 NODE_ENV 设置为生产。事实上,如果我在应用程序中使用 console.log(process.env),我看不到 NODE_ENV 或 CUSTOM。任何想法发生了什么?

By the way, NODE_ENV=production node app.js works just fine.

顺便说一句, NODE_ENV=production node app.js 工作得很好。

回答by Peter Lyons

From the sudo man page(Ubuntu version of sudo)

sudo 手册页(sudo 的 Ubuntu 版本)

There are two distinct ways to deal with environment variables. By default, the env_reset sudoers option is enabled. This causes commands to be executed with a minimal environment containing TERM, PATH, HOME, SHELL, LOGNAME, USER and USERNAME in addition to variables from the invoking process permitted by the env_check and env_keep sudoers options. There is effectively a whitelist for environment variables.

有两种不同的方式来处理环境变量。默认情况下,启用 env_reset sudoers 选项。这会导致命令在包含 TERM、PATH、HOME、SHELL、LOGNAME、USER 和 USERNAME 的最小环境下执行,以及来自 env_check 和 env_keep sudoers 选项允许的调用过程的变量。环境变量有一个有效的白名单。

Sudo is resetting the environment. This is a frustrating aspect of using suand sudoin upstart or init scripts. Recent versions of upstart support specifying uid/gid without the use of sudo via the setuid/setgiddirectives as in the example below. Also note the use of chdir.

Sudo 正在重置环境。这是在 upstart 或 init 脚本中使用su和令人沮丧的方面sudo。最近版本的 upstart 支持通过setuid/setgid指令指定 uid/gid 而不使用 sudo ,如下例所示。还要注意使用chdir.

start on filesystem and started networking
respawn
chdir /var/www/yourapp
setuid yourapp
setgid yourapp
env NODE_ENV=production
env PATH=/usr/local/bin:/usr/bin:/bin
env CUSTOM=somevalue
exec /usr/local/bin/node app.js | /usr/bin/multilog s1024000 /var/log/yourapp 2>&1

For older versions of upstart, here's what I used to do to work around it.

对于旧版本的暴发户,这是我过去用来解决它的方法。

description "start and stop the example.com node.js server"

start on filesystem and started networking
respawn

chdir /path/to/your/code
exec su -c 'PATH=$PWD/node/bin NODE_ENV=$(cat node_env.txt) ./node/bin/node app/server.js' www-data  >> tmp/stdout.log 2>&1

Note that I just put a node_env.txtfile in my app root that sets production mode, because I hate environment variables. You can just do NODE_ENV=productionright there if you prefer.

请注意,我只是node_env.txt在我的应用程序根目录中放置了一个设置生产模式的文件,因为 我讨厌环境变量NODE_ENV=production如果你愿意,你可以直接在那里做。

回答by C2H5OH

Just for the record. The Upstart Cookbookrecommends the usage of start-stop-daemoninstead of suor sudowhen your Upstart version does notimplement setuid.

只是为了记录。该暴发户食谱建议的使用start-stop-daemon,而不是susudo当你的暴发户版本并没有实现setuid

But, unless you are still using 10.04 LTS(Lucid Lynx) which only has Upstart version 0.6.5, you should be using the setuid/setgiddirectives.

但是,除非您仍在使用只有 Upstart 版本 0.6.5 的10.04 LTS( Lucid Lynx) ,否则您应该使用这些setuid/setgid指令。

回答by Sean McClory

This has been working for me to set node env variables in upstart.

这对我来说在 upstart 中设置节点环境变量一直有效。

#!upstart

start on runlevel [2345]
stop on runlevel [016]

respawn

script
    echo $$ > /var/run/app.pid
    exec sudo NODE_ENV=production /opt/node/bin/node /opt/myapp/app.js >> /var/log/app.sys.log 2>&1
end script

回答by keos

visudohas a line to define environment variables to be kept.

visudo有一行定义要保留的环境变量。

sudo visudo

and add your env to:

并将您的 env 添加到:

Defaults        env_keep="YOUR_ENV ..."

and reboot.

并重新启动。