如何通过sudo在生产模式下运行node.js Express?

时间:2021-09-01 10:57:14

I'm using the npm package express version 2.5.2 with node version .0.6.5. I appear to be running bash version 4.1.5 on Debian 4.4.5.

我正在使用节点版本为.0.6.5的npm package express版本2.5.2。我似乎在Debian 4.4.5上运行bash版本4.1.5。

I'm trying to run my server in production mode but it still runs in development mode.

我正在尝试在生产模式下运行我的服务器,但它仍然在开发模式下运行。

I run these commands in my bash shell:

我在我的bash shell中运行这些命令:

$ export NODE_ENV=production
$ echo $NODE_ENV
production
$ sudo echo $NODE_ENV
production
$ sudo node bootstrap.js

I have this code inside bootstrap.js:

我在bootstrap.js中有这个代码:

var bootstrap_app = module.exports = express.createServer();
//...
console.log(bootstrap_app.settings.env);

and here's what I see printed to standard out:

这是我看到标准打印出来的:

development

Is this a problem with my usage, or my system?

这是我的用法或系统的问题吗?

EDIT: Thanks to ThiefMaster for his properly identifying that this issue stems from my running node as root. ThiefMaster suggested using iptables to forward from port 80 to an unprivileged port, but my system gives me an error. Moving this discussion to superuser.com or serverfault.com (link to follow)

编辑:感谢ThiefMaster正确识别此问题源于我作为root运行节点。 ThiefMaster建议使用iptables从端口80转发到非特权端口,但是我的系统给了我一个错误。将此讨论发送到superuser.com或serverfault.com(链接到后面)

2 个解决方案

#1


9  

Most environment variables are unset when using sudo for security reasons. So you cannot pass that environment variable to node without modifying your sudoers file to allow that variable to passt through.

出于安全原因使用sudo时,大多数环境变量都未设置。因此,您无法在不修改sudoers文件的情况下将该环境变量传递给节点,以允许该变量通过。

However, you shouldn't run node as root anyway. So here's a good workaround:
If you just need it for port 80, run node on an unprivileged port and setup an iptables forward to map port 80 to that port:

但是,您不应该以root身份运行节点。所以这里有一个很好的解决方法:如果您只需要端口80,请在非特权端口上运行节点并设置iptables以将端口80映射到该端口:

iptables -A PREROUTING -d 1.2.3.4/32 -i eth0 -p tcp -m tcp --dport 80 -j DNAT --to-destination 2.3.4.5:1234

Replace 1.2.3.4 with your public IP, 2.3.4.5 with the IP node runs on (could be the public one or 127.0.0.1) and 1234 with the port node runs on.

将1.2.3.4替换为您的公共IP,2.3.4.5,IP节点运行(可以是公共IP或127.0.0.1),1234运行端口节点。


With a sufficiently recent kernel that has capability support you could also grant the node executable the CAP_NET_BIND_SERVICE privilege using the following command as root:

使用具有功能支持的足够新的内核,您还可以使用以下命令以root身份向节点可执行文件授予CAP_NET_BIND_SERVICE权限:

setcap 'cap_net_bind_service=+ep' /usr/bin/node 

Note that this will allow any user on your system to open privileged ports using node!

请注意,这将允许系统上的任何用户使用节点打开特权端口!

#2


2  

sudo NODE_ENV=production /usr/local/bin/node  /usr/local/apps/test/app.js

#1


9  

Most environment variables are unset when using sudo for security reasons. So you cannot pass that environment variable to node without modifying your sudoers file to allow that variable to passt through.

出于安全原因使用sudo时,大多数环境变量都未设置。因此,您无法在不修改sudoers文件的情况下将该环境变量传递给节点,以允许该变量通过。

However, you shouldn't run node as root anyway. So here's a good workaround:
If you just need it for port 80, run node on an unprivileged port and setup an iptables forward to map port 80 to that port:

但是,您不应该以root身份运行节点。所以这里有一个很好的解决方法:如果您只需要端口80,请在非特权端口上运行节点并设置iptables以将端口80映射到该端口:

iptables -A PREROUTING -d 1.2.3.4/32 -i eth0 -p tcp -m tcp --dport 80 -j DNAT --to-destination 2.3.4.5:1234

Replace 1.2.3.4 with your public IP, 2.3.4.5 with the IP node runs on (could be the public one or 127.0.0.1) and 1234 with the port node runs on.

将1.2.3.4替换为您的公共IP,2.3.4.5,IP节点运行(可以是公共IP或127.0.0.1),1234运行端口节点。


With a sufficiently recent kernel that has capability support you could also grant the node executable the CAP_NET_BIND_SERVICE privilege using the following command as root:

使用具有功能支持的足够新的内核,您还可以使用以下命令以root身份向节点可执行文件授予CAP_NET_BIND_SERVICE权限:

setcap 'cap_net_bind_service=+ep' /usr/bin/node 

Note that this will allow any user on your system to open privileged ports using node!

请注意,这将允许系统上的任何用户使用节点打开特权端口!

#2


2  

sudo NODE_ENV=production /usr/local/bin/node  /usr/local/apps/test/app.js