Executing a shell script from a PHP script

I would have a directory somewhere called scripts under the WWW folder so that it’s not reachable from the web but is reachable by PHP.

e.g. /var/www/scripts/testscript

Make sure the user/group for your testscript is the same as your webfiles. For instance if your client.php is owned by apache:apache, change the bash script to the same user/group using chown. You can find out what your client.php and web files are owned by doing ls -al.

Then run

      $message=shell_exec("/var/www/scripts/testscript 2>&1");


If you really want to run a file as root from a webserver you can try this binary wrapper below. Check out this solution for the same thing you want to do.

Execute root commands via PHP

Leave a Comment