简体   繁体   English

从PHP脚本执行Shell脚本

[英]Executing a shell script from a PHP script

I want to execute a Bash script present on the system from a PHP script. 我想从PHP脚本执行系统上存在的Bash脚本。 I have two scripts present on the system. 我的系统上有两个脚本。 One of them is a PHP script called client.php present at /var/www/html and the other is a Bash script called testscript present at /home/testuser . 其中一个是/var/www/html上的一个名为client.php的PHP脚本,另一个是/home/testuser一个名为testscript的Bash脚本。

My client.php script looks like 我的client.php脚本看起来像

<?php
  $message=shell_exec("/home/testuser/testscript 2>&1");
  print_r($message);
?>  

My testscript looks like 我的睾丸看起来像

#!/bin/bash
echo "Testscript run succesful"

When i do the following on terminal 当我在终端上执行以下操作时

php client.php

I get the following output on terminal 我在终端上得到以下输出

Testscript run successful

But when i open the page at 但是当我在打开页面时

http://serverdomain/client.php

I get the following output 我得到以下输出

sh: /home/testuser/testscript: Permission denied 

I get this error even after I did chmod +x testscript. 即使执行了chmod + x testscript,我仍然收到此错误。
How do I get it to work from the browser? 我如何从浏览器中获取它? Please help. 请帮忙。

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. 我将在WWW文件夹下的某个目录下有一个名为scripts的目录,以便无法从Web上访问它,但可以通过PHP来访问它。

eg /var/www/scripts/testscript 例如/var/www/scripts/testscript

Make sure the user/group for your testscript is the same as your webfiles. 确保您的测试testscript的用户/组与您的网络文件相同。 For instance if your client.php is owned by apache:apache , change the bash script to the same user/group using chown . 例如,如果您的client.phpapache:apache拥有,则使用chown将bash脚本更改为相同的用户/组。 You can find out what your client.php and web files are owned by doing ls -al . 您可以通过执行ls -al来查找client.php和Web文件所拥有的文件。

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. 如果您确实想从Web服务器以root用户身份运行文件,则可以尝试以下二进制包装器。 Check out this solution for the same thing you want to do. 签出此解决方案以完成您想做的相同事情。

Execute root commands via PHP 通过PHP执行root命令

Without really knowing the complexity of the setup, I like the sudo route. 在不真正了解设置复杂性的情况下,我喜欢sudo路由。 First, you must configure sudo to permit your webserver to sudo run the given command as root. 首先,您必须配置sudo以允许您的网络服务器以root身份运行给定命令。 Then, you need to have the script that the webserver shell_exec's(testscript) run the command with sudo. 然后,您需要使Web服务器shell_exec的(testscript)使用sudo运行命令的脚本。

For A Debian box with Apache and sudo: 对于带有Apache和sudo的Debian盒子:

  1. Configure sudo: 配置sudo:

    • As root, run the following to edit a new/dedicated configuration file for sudo: 以root用户身份运行以下命令以编辑sudo的新/专用配置文件:

       visudo -f /etc/sudoers.d/Webserver 

      (or whatever you want to call your file in /etc/sudoers.d/ ) (或您想在/etc/sudoers.d/调用文件的/etc/sudoers.d/

    • Add the following to the file: 将以下内容添加到文件中:

       www-data ALL = (root) NOPASSWD: <executable_file_path> 

      where <executable_file_path> is the command that you need to be able to run as root with the full path in its name(say /bin/chown for the chown executable). 其中<executable_file_path>是您需要能够以root身份运行且其名称带有完整路径的命令(例如/bin/chownchown可执行文件)。 If the executable will be run with the same arguments every time, you can add its arguments right after the executable file's name to further restrict its use. 如果可执行文件每次都使用相同的参数运行,则可以在可执行文件的名称之后添加其参数,以进一步限制其使用。

      For example, say we always want to copy the same file in the /root/ directory, we would write the following: 例如,假设我们总是要复制/ root /目录中的相同文件,则将编写以下内容:

       www-data ALL = (root) NOPASSWD: /bin/cp /root/test1 /root/test2 
  2. Modify the script(testscript): 修改脚本(testscript):

    Edit your script such that sudo appears before the command that requires root privileges(say sudo /bin/chown ... or sudo /bin/cp /root/test1 /root/test2 ). 编辑脚本,使sudo出现在需要root特权的命令之前(例如sudo /bin/chown ...sudo /bin/cp /root/test1 /root/test2 )。 Make sure that the arguments specified in the sudo configuration file exactly match the arguments used with the executable in this file. 确保sudo配置文件中指定的参数与该文件中的可执行文件所使用的参数完全匹配。 So, for our example above, we would have the following in the script: 因此,对于上面的示例,脚本中将包含以下内容:

     sudo /bin/cp /root/test1 /root/test2 

If you are still getting permission denied, the script file and it's parent directories' permissions may not allow the webserver to execute the script itself. 如果仍然拒绝权限,则脚本文件及其父目录的权限可能不允许Web服务器执行脚本本身。 Thus, you need to move the script to a more appropriate directory and/or change the script and parent directory's permissions to allow execution by www-data(user or group), which is beyond the scope of this tutorial. 因此,您需要将脚本移动到更合适的目录和/或更改脚本和父目录的权限,以允许通过www-data(用户或组)执行,这超出了本教程的范围。

Keep in mind: 记住:

When configuring sudo, the objective is to permit the command in it's most restricted form. 配置sudo时,目的是允许以最受限制的形式使用命令。 For example, instead of permitting the general use of the cp command, you only allow the cp command if the arguments are, say, /root/test1 /root/test2 . 例如,仅允许在参数为/ root / test1 / / root / test2的情况下才允许cp命令,而不是允许通用使用cp命令。 This means that cp 's arguments(and cp's functionality cannot be altered). 这意味着cp的参数(和cp的功能不能更改)。

I was struggling with this exact issue for three days. 我一直在为这个确切的问题苦苦挣扎三天。 I had set permissions on the script to 755. I had been calling my script as follows. 我已将脚本的权限设置为755。我一直在按以下方式调用脚本。

<?php
   $outcome = shell_exec('/tmp/clearUp.sh');
   echo $outcome;
?>

My script was as follows. 我的脚本如下。

#!bin/bash
find . -maxdepth 1 -name "search*.csv" -mmin +0 -exec rm {} \;

I was getting no output or feedback. 我没有任何输出或反馈。 The change I made to get the script to run was to add a cd to tmp inside the script: 为了使脚本能够运行,我所做的更改是在脚本内的tmp中添加了cd:

#!bin/bash
cd /tmp;
find . -maxdepth 1 -name "search*.csv" -mmin +0 -exec rm {} \;

This was more by luck than judgement but it is now working perfectly. 这比运气更胜于判断,但它现在运行良好。 I hope this helps. 我希望这有帮助。

It's a simple problem. 这是一个简单的问题。 When you are running from terminal, you are running the php file from terminal as a privileged user. 从终端运行时,您以特权用户身份从终端运行php文件。 When you go to the php from your web browser, the php script is being run as the web server user which does not have permissions to execute files in your home directory. 当您从Web浏览器转到php时,php脚本将以Web服务器用户的身份运行,该用户无权执行主目录中的文件。 In Ubuntu, the www-data user is the apache web server user. 在Ubuntu中,www-data用户是apache Web服务器用户。 If you're on ubuntu you would have to do the following: chown yourusername:www-data /home/testuser/testscript chmod g+x /home/testuser/testscript 如果您使用的是ubuntu,则必须执行以下操作:chown yourusername:www-data / home / testuser / testscript chmod g + x / home / testuser / testscript

what the above does is transfers user ownership of the file to you, and gives the webserver group ownership of it. 上面的操作是将文件的用户所有权转让给您,并为它提供Web服务器组的所有权。 the next command gives the group executable permission to the file. 接下来的命令授予该组可执行文件权限。 Now the next time you go ahead and do it from the browser, it should work. 现在,下次您继续使用浏览器进行操作时,它应该可以工作了。

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

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