如何在PHP中执行Shell脚本? [英] How to execute a shell script in PHP?
问题描述
我在/var/www/myscript.sh 中有一个脚本,该脚本创建文件夹并为我的项目运行命令svn update
.我需要通过在浏览器中的PHP文件(即 Localhost/test.php )中调用该脚本来执行该脚本.我尝试使用功能shell_exec()
和exec()
,但是这些功能不起作用.我用su www-data && ./myscript.sh
在终端中运行了我的shell脚本,它起作用了.我还想念什么?
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");
?>
更新5/4/2011:
我在/etc/sudoers中添加了www-data ALL=(ALL) NOPASSWD:ALL
,它可以工作,但这是非常不安全的.还有另一种方法吗?
Update 5/4/2011:
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?
推荐答案
多种可能性:
- You have safe mode enabled. That way, only
exec()
is working, and then only on executables insafe_mode_exec_dir
exec
andshell_exec
are disabled in php.ini- The path to the executable is wrong. If the script is in the same directory as the php file, try
exec(dirname(__FILE__) . '/myscript.sh');
这篇关于如何在PHP中执行Shell脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!