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."]}

1 Answer 1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.