"executing a shell script from a php script" Code Answer

5

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

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

edit:

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

By Zlatko Michailov - MSFT on October 10 2022

Answers related to “executing a shell script from a php script”

Only authorized users can answer the Search term. Please sign in first, or register a free account.