0

How can I allow these commands in PHP:

$output = exec('sudo nginx -t 2>&1');
$output2 = exec('sudo /usr/sbin/service nginx reload 2>&1');

I've looked into sudo visudo in the terminal , but it seems like that will give access to all sudo commands, when I only need the two above.

I guess the reload one isn't so important, as I can just run a cron job to do that.

I am using NGINX, with Ubuntu 20 + PHP 7

1 Answers1

0

You can put the following lines in /etc/sudoers.d/nginx:

www-data ALL = (root) NOPASSWD: /usr/sbin/nginx -t
www-data ALL = (root) NOPASSWD: /usr/sbin/service nginx reload

This disables password prompt when running sudo /usr/sbin/service nginx reload or the other command when logged in as www-data.

This assumes that your PHP is running under www-data user.

Tero Kilkanen
  • 37,584
  • Appreciate it, this works! Also, do you think it's much of an issue to allow these commands from a security point? It shouldn't matter that much right, since it's just an nginx reload – Tom Tucker Feb 16 '21 at 15:39
  • This in itself isn't a security issue. However, if you allow modifying nginx configuration via PHP scripts, then there can be security issues. – Tero Kilkanen Feb 16 '21 at 16:00