Skip to content
Advertisement

PHP exec sudo script not working despite having sudo rights

My php script:

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);


exec('sudo /path/to/script.sh');
?>

my visudo line

www-data        ALL=NOPASSWD: /path/to/script.sh

(I tripled checked every path)

If i remove the ‘sudo’ in the exec() it will execute (i put a touch aaa at the start, with sudo the file isn’t even created so the script doesn’t even begin to execute). The script has exec rights for everyone.

I can’t manage to get any error output. I can’t su into www-data to try to launch it manually with sudo (when I su I get “This account is currently not available. “, but I guess that’s normal behavior).

Advertisement

Answer

  1. If your system has selinux or some other policy-based access control engine enabled, your web server process will be running under a constrained context which limits rights further. Verify whether you have selinux enabled.

  2. Provide the full path to sudo. While your shell may have it in its path, perhaps your web server does not.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement