I am trying to run a shell script from a PHP script.
PHP code :
<? php
$sss = escapeshellarg('virtualbox');
$result = shell_exec("/home/hani/Desktop/launchscript.sh '$sss' 2>&1 ");
echo "<pre>$result</pre>";
echo "<br />";
echo (shell_exec('whoami'));
?>
my shell script :
#!/bin/bash
sss=$1
echo 'the sudo password' |sudo -S service $1 restart
After I run the PHP code in a web server (Xampp), I got this output :
[sudo] password for daemon: Sorry, try again.
[sudo] password for daemon:
sudo: 1 incorrect password attempt
daemon
Although, I haven't set any password for the daemon user.
And after I checked the current user running the PHP code I found it is daemon.
After many researches here and in the net, I found that daemon can't run sudo
commands.
I also found that I can fix this by editing the sudoers file and giving permissions to the daemon user to run sudo commands. However this is not a secured solution.
so my question is : How to run that script via the PHP code but not as a daemon?
PS : I tried this in order to change the current user running the PHP file :
$result = shell_exec(" sudo -u hani /home/hani/Desktop/launchscript.sh '$sss' 2>&1 ");
But I got this output in the browser :
sudo: no tty present and no askpass program specified
and the user remains daemon.
I am using Xampp in Ubuntu 16.04
Another information, I run this command in the terminal to know the owner of the 'httpd' service :
ps -ef | egrep '(httpd)' | grep -v `whoami` | grep -v root | head -n1 | awk '{print $1}'
the output is : daemon