从PHP脚本执行bash脚本 [英] Executing bash script from php script

查看:252
本文介绍了从PHP脚本执行bash脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个PHP脚本执行系统上的bash脚本present。我有两个脚本在系统上present。其中之一是所谓的client.php present位于/ var / www / html等一个PHP脚本,另一种是所谓的testscript present通过/ home / testuser的bash脚本。

I want to execute the bash script present on the system from a php script. 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.

我client.php脚本看起来像

My client.php script looks like

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

我testscript看起来像

My testscript looks like

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

当我做终端以下

php client.php 

我得到终端下面的输出

I get the following output on terminal

Testscript run successful

但是,当我在

http://serverdomain/client.php

我得到以下输出

sh: /home/testuser/testscript: Permission denied 

我得到我没有搭配chmod + X testscript即使此错误。结果
我如何得到它从浏览器的工作?请帮助。

I get this error even after i did chmod +x testscript.
How do i get it to work from the browser? Please help.

推荐答案

我想有一个目录的地方称为脚本的WWW文件夹下,这样它不是从网站访问,但访问是由PHP。

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.

例如。 /无功/网络/脚本/ testscript

请确保用户/组为您 testscript 是与您的webfiles。举例来说,如果你的 client.php 拥有阿帕奇:阿帕奇,改变bash脚本到同一个用户/组使用 CHOWN 。你可以找出你的是什么 client.php 和网页文件做 LS -al 拥有。

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.

然后运行

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

编辑:

如果你真的想从网络服务器上运行一个文件作为根你可以试试下面这个二元包装。看看这个解决方案,你想要做同样的事情。

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.

通过PHP 执行根命令

这篇关于从PHP脚本执行bash脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆