无法重新加载 nginx.service:需要交互式身份验证

use*_*075 3 php command-line nginx symfony

我正在使用 symfony 3 处理项目,该项目在 nginx 服务器上运行,我正在尝试使用以下命令重新加载配置文件:来自控制器的“systemctl reload nginx”。

/**
* @Route("/testReloadConfig")
*/
public function testReloadConfigAction(Request $request){

    $output = [];
    $result = null;

    $cmd = 'systemctl reload nginx 2>&1';

    exec($cmd, $output, $result);

    return new JsonResponse([
        'result' => $result,
        'output' => $output,
    ]);
}
Run Code Online (Sandbox Code Playgroud)

响应:
{"result":1,"output":["Failed to reload nginx.service: Interactive authentication required.","有关详细信息,请参阅系统日志和 \u0027systemctl status nginx.service\u0027。"]}

DrK*_*Key 10

很可能nginx不会与用户一起运行,root而这正是您需要使用的systemctl。因此,您应该使用sudo.

现在有不同的方式从具有root特权的web-server 运行命令:

  • 出于明显的安全原因,强烈建议不要nginx与用户一起运行root
  • sudoers此处所述添加此特定命令,以便在root没有任何密码提示的情况下以特权运行它
  • 按照此处所述传递您的root密码echo
  • 编写具有有限功能的 bash 脚本,如here所述

但当然,您可能会发现其他东西正在搜索它。