简体   繁体   English

如何在 PHP 中执行 shell 脚本?

[英]How to execute a shell script in PHP?

I have a script in /var/www/myscript.sh which creates folders and runs the command svn update for my projects.我在/var/www/myscript.sh中有一个脚本,它创建文件夹并为我的项目运行命令svn update I need to execute this script by calling it in a PHP file in the browser (ie Localhost/test.php ).我需要通过在浏览器的 PHP 文件中调用它来执行这个脚本(即Localhost/test.php )。 I tried using functions shell_exec() and exec() but those did not work.我尝试使用函数shell_exec()exec()但它们不起作用。 I ran my shell script in terminal with su www-data &&./myscript.sh and it worked.我使用su www-data &&./myscript.sh在终端中运行了我的 shell 脚本,它工作正常。 What else am I missing?我还缺少什么?

<?php
$output = shell_exec("./myscript.sh");
?>

Update 5/4/2011: 2011 年 5 月 4 日更新:

I added www-data ALL=(ALL) NOPASSWD:ALL to /etc/sudoers and it works, but this is very insecure.我将www-data ALL=(ALL) NOPASSWD:ALL添加到 /etc/sudoers 并且它可以工作,但这非常不安全。 Is there another way to do this?还有另一种方法可以做到这一点吗?

Several possibilities:几种可能:

  • You have safe mode enabled.您已启用安全模式。 That way, only exec() is working, and then only on executables in safe_mode_exec_dir这样,只有exec()工作,然后只在safe_mode_exec_dir中的可执行文件上工作
  • exec and shell_exec are disabled in php.ini execshell_exec在 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 文件在同一目录下,请尝试exec(dirname(__FILE__). '/myscript.sh');

You might have disabled the exec privileges, most of the LAMP packages have those disabled.您可能禁用了 exec 权限,大多数 LAMP 包都禁用了这些权限。 Check your php.ini for this line:检查您的 php.ini 是否有此行:

disable_functions = exec

And remove the exec, shell_exec entries if there are there.并删除 exec、shell_exec 条目(如果有)。

Good Luck!祝你好运!

Residuum did provide a correct answer to how you should get shell exec to find your script, but in regards to security, there are a couple of points. Residuum 确实为您应该如何让 shell exec 找到您的脚本提供了正确的答案,但在安全性方面,有几点。

I would imagine you don't want your shell script to be in your web root, as it would be visible to anyone with web access to your server.我想您不希望您的 shell 脚本位于您的 web 根目录中,因为任何拥有 web 访问您服务器的人都可以看到它。

I would recommend moving the shell script to outside of the webroot我建议将 shell 脚本移到 webroot 之外

    <?php
      $tempFolder = '/tmp';
      $webRootFolder = '/var/www';
      $scriptName = 'myscript.sh';
      $moveCommand = "mv $webRootFolder/$scriptName $tempFolder/$scriptName";
      $output = shell_exec($moveCommand);
    ?>

In regards to the:关于:

i added www-data ALL=(ALL) NOPASSWD:ALL to /etc/sudoers works我添加 www-data ALL=(ALL) NOPASSWD:ALL 到 /etc/sudoers 作品

You can modify this to only cover the specific commands in your script which require sudo.您可以将其修改为仅涵盖脚本中需要 sudo 的特定命令。 Otherwise, if none of the commands in your sh script require sudo to execute, you don't need to do this at all anyway.否则,如果您的 sh 脚本中的任何命令都不需要 sudo 执行,那么您根本不需要执行此操作。

Try running the script as the apache user (use the su command to switch to the apache user) and if you are not prompted for sudo or given permission denied, etc, it'll be fine.尝试以 apache 用户身份运行该脚本(使用 su 命令切换到 apache 用户),如果没有提示您输入 sudo 或授予权限被拒绝等,那就没问题了。

ie: IE:

sudo su apache (or www-data)
cd /var/www
sh ./myscript

Also... what brought me here was that I wanted to run a multi line shell script using commands that are dynamically generated.另外......让我来到这里的是我想使用动态生成的命令运行多行 shell 脚本。 I wanted all of my commands to run in the same shell, which won't happen using multiple calls to shell_exec().我希望我的所有命令都在同一个 shell 中运行,这不会在多次调用 shell_exec() 时发生。 The answer to that one is to do it like Jenkins - create your dynamically generated multi line of commands, put it in a variable, save it to a file in a temp folder, execute that file (using shell_exec in() php as Jenkins is Java), then do whatever you want with the output, and delete the temp file对此的答案是像 Jenkins 那样做 - 创建动态生成的多行命令,将其放入变量中,将其保存到临时文件夹中的文件中,执行该文件(使用 shell_exec in() php as Z2ADAE54334C0A5CE2ABE3E5is Java),然后用 output 做任何你想做的事,并删除临时文件

... voila ...瞧

If you are having a small script that you need to run (I simply needed to copy a file), I found it much easier to call the commands on the PHP script by calling如果您有一个需要运行的小脚本(我只需要复制一个文件),我发现通过调用来调用 PHP 脚本上的命令要容易得多

exec("sudo cp /tmp/testfile1 /var/www/html/testfile2");

and enabling such transaction by editing (or rather adding) a permitting line to the sudoers by first calling sudo visudo and adding the following line to the very end of it并通过首先调用sudo visudo并将以下行添加到 sudoers 的最后一行来编辑(或者更确切地说添加)允许行来启用此类事务

www-data ALL=(ALL) NOPASSWD:/bin/cp /tmp/testfile1 /var/www/html/testfile2

All I wanted to do was to copy a file and I have been having problems with doing so because of the root password problem, and as you mentioned I did NOT want to expose the system to have no password for all root transactions.我想做的就是复制一个文件,但由于 root 密码问题,我一直遇到问题,正如你提到的,我不想让系统公开所有 root 事务都没有密码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM