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

查看:17
本文介绍了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=production node 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 脚本中使用 susudo 的一个令人沮丧的方面.最近版本的新贵支持通过 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天全站免登陆