这基本上是对 maerics 答案的补充,但由于我想添加一个代码示例,我编写了另一个答案,其中包含如何防止安全问题的附加信息。
通常 Web 服务器以 root 身份启动,因为这需要绑定到低于 1024 的端口,但随后它们会将运行它们的用户更改为非特权用户。
您可以使用 node.js 执行此操作process.setuid("username")
。
以下示例启动服务器,然后在以 root 身份启动时将自身绑定到端口 80 后将 root 权限删除给用户“www-data”:
function StartServer() {
console.log("Starting server...");
// Initalizations such as reading the config file, etc.
server = http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
});
try {
server.listen(80, "0.0.0.0", function(){
process.setuid("www-data");
});
}
catch(err) {
console.error("Error: [%s] Call: [%s]", err.message, err.syscall);
process.exit(1);
}
}