I have a script in /var/www/myscript.sh which creates folders and runs the command svn update
for my projects. I need to execute this script by calling it in a PHP file in the browser (i.e. Localhost/test.php). I tried using functions shell_exec()
and exec()
but those did not work. I ran my shell script in terminal with su www-data && ./myscript.sh
and it worked. What else am I missing?
<?php $output = shell_exec("./myscript.sh"); ?>
I added www-data ALL=(ALL) NOPASSWD:ALL
to /etc/sudoers and it works, but this is very insecure. Is there another way to do this?
The shell_exec() function is an inbuilt function in PHP which is used to execute the commands via shell and return the complete output as a string. The shell_exec is an alias for the backtick operator, for those used to *nix.
PHP Shell is a shell wrapped in a PHP script. It's a tool you can use to execute arbitrary shell-commands or browse the filesystem on your remote webserver. This replaces, to a degree, a normal telnet connection, and to a lesser degree a SSH connection.
Several possibilities:
exec()
is working, and then only on executables in safe_mode_exec_dir
exec
and shell_exec
are disabled in php.iniexec(dirname(__FILE__) . '/myscript.sh');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With