Search code examples
phplinuxbashexeciptables

run bash script from php exec()


how to run bash script from php with sudo

index.php :

<?php
$arg;
exec("sudo /var/www/script", $arg);
?>

script :

!#/bin/bash
echo YOUR ARG IS $1;
sudoers www-data ALL=NOPASSWD /var/www/my 
chmod 755 /var/www/my 

Solution

  • Firstly, you can't sudo it directly like that. So you have to change some configurations in sudoers file.

    Run sudo visudo in console, add the following line at the end of file

    nobody ALL = NOPASSWD: /var/www/script
    

    P.S.: It's a security risk to use your script like that which would sometime perform breach in data security in a great extent so my suggestion is don't call your script like that.