How do I allow www-data user to execute bash script with nginx
The result of shell_exec()
is returned as string. To display it in your browser, simply add echo.
<?php chdir('/path/to/my/files/'); echo shell_exec('./script_test.sh'); // ATTEMPT RUN SCRIPT echo shell_exec('/path/to/my/files/script_test.sh'); // ATTEMPT RUN SCRIPT echo 'test 123'; // SIMPLE ECHO IN THE PHP PAGE?>
See the Return Values in the manual:
The output from the executed command or NULL if an error occurred or the command produces no output.
Can you try to use passthru instead of shell_exec, and see the output anything?
Also try this, and see if it shows on the log file:
if(file_exists('/path/to/my/files/script_test.sh')) { die('File not found!'); }shell_exec("nohup /path/to/my/files/script_test.sh > /path/to/my/files/output.log &");
Also, are you running PHP with the www-data user (check your fpm pool)?Do you have any error on /var/log/syslog or /var/log/auth.log ?Have you restarted the server after changing the sudo permissions?
What does su - www-data -c "whoami"
and su - www-data -s /bin/bash -c "whoami"
outputs?
Does su - www-data -s /bin/bash -c "/path/to/my/files/script_test.sh"
output something?