Upstart env节不为Node.js应用程序设置环境变量(如NODE_ENV) [英] Upstart env stanza not setting environment variables (like NODE_ENV) for Node.js application

查看:164
本文介绍了Upstart env节不为Node.js应用程序设置环境变量(如NODE_ENV)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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 =生产节点app.js工作正常。

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

推荐答案

sudo手册页(Ubuntu版本的sudo)

From the sudo man page (Ubuntu version of sudo)


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

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.

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

Sudo is resetting the environment. This is a frustrating aspect of using su and sudo in upstart or init scripts. Recent versions of upstart support specifying uid/gid without the use of sudo via the setuid/setgid directives as in the example below. Also note the use of 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

请注意,我只是把一个 node_env.txt 在我的应用程序根目录中设置生产模式,因为我讨厌环境变量。如果您愿意,可以直接在 NODE_ENV = production 之间。

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

这篇关于Upstart env节不为Node.js应用程序设置环境变量(如NODE_ENV)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆