I need to launch a command with sudo rights out of a php file (user: www-data), explicitly as user www-data
:
<?php
$command = 'sudo -u www-data /usr/bin/python3 /var/www/html/script.py';
shell_exec($command);
?>
to be able to use sudo for www-data
I want to put the command in sudoers (sudo visudo
), like:
www-data ALL=NOPASSWD: sudo -u www-data /usr/bin/python3 /var/www/html/script.py
or
www-data ALL=NOPASSWD: -u www-data /usr/bin/python3 /var/www/html/script.py
but the syntax is wrong (error message from visudo). The following is working with sudoers (correct syntax)
www-data ALL=NOPASSWD: /usr/bin/python3 /var/www/html/script.py
but doesn't work for my script (apache error in log file):
Sorry, user www-data is not allowed to execute '/usr/bin/python3 /var/www/html/script.py' as www-data on raspberrypi.
it seems it needs sudo -u www-data
. How can I solve this?