3

I am working on project using symfony 3, the project run on nginx server and i am trying to reload the configuration files with the following command: "systemctl reload nginx" from the controller.

/**
* @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,
    ]);
}

The response:
{"result":1,"output":["Failed to reload nginx.service: Interactive authentication required.","See system logs and \u0027systemctl status nginx.service\u0027 for details."]}

user3870075
  • 141
  • 1
  • 3
  • 10

1 Answers1

12

Most probably nginx doesn't run with user root which is what you need to use systemctl. Therefore you should execute your command using sudo.

Now there are different ways of running a command from web-server with root privileges:

  • Run nginx with user root which is highly discouraged for obvious security reasons
  • Add only this specific command to sudoers as described here in order to run it with root privileges without any password prompt
  • Pass your root password through echo as described here
  • Write a bash script with limited functions as described here

But of course you might find something else searching on for it.

DrKey
  • 3,365
  • 2
  • 29
  • 46