【问题标题】:Php script is working properly but in the browser its not workingPHP 脚本工作正常,但在浏览器中它不工作
【发布时间】:2020-12-03 23:17:22
【问题描述】:

这是代码,它的目的是运行一个 bash 脚本。 (index.php)

<html>
<title>
        hello
</title>


<b>hello</b>
<body>
<?php
exec('bash /var/www/html/test.sh');


?>
</body>
</html>

什么时候做 php index.php 它会执行 test.sh 但是当我用浏览器运行它时它不会运行 bash 脚本。

考虑 index.php 在 test.sh 的同一目录下 我已经完成了 chmod 和 chown。

这是 test.sh

#!/bin/bash
python3 /var/www/html/test.py

这是 test.py

import logging
a = "Hello world "

LOG_FILENAME = '/var/www/html/test.txt'
logging.basicConfig(filename=LOG_FILENAME,level=logging.DEBUG)
logging.debug(a)

【问题讨论】:

  • test.sh 有什么作用?你怎么确定它没有被执行?剧本应该有答案吗?因为您没有回显也没有保存结果。有任何错误信息吗?
  • 不能在浏览器中直接运行php。必须有一个服务器来解析 php
  • 这很可能是没有“bash test.sh”的完整路径的问题
  • @AmbrishPathak 我有一个网络服务器,我在 /var/www/html/index.php 中使用它
  • @DefinitelynotRafal 主要目的是运行一些 python 应用程序,但 shell 只是在执行它们。其中之一是一个hello word python,它可以登录test.txt,然后我跟踪那个txt文件。当我在终端中运行 index.php 时,它会写入日志,但是当我使用 www.example.com/index.php 执行它时,它会显示“hello”但没有登录 test.txt

标签: php html bash


【解决方案1】:

可能是因为网络服务器正在尝试运行 sudo 的命令 (sudo python3 /var/www/html/test.py) 并且正在等待一些用户输入?

或者可能是因为 bash 的路径应该是完全限定的,因为您系统上的 www-user 帐户可能缺少某些配置。

或者您的 shell 脚本可能缺少执行位?

【讨论】:

    【解决方案2】:

    在更改了一些权限后,它起作用了:

    chown -R www-data:www-data /var/www
    chmod go-rwx /var/www
    chmod go+x /var/www
    chgrp -R www-data /var/www
    chmod -R go-rwx /var/www
    chmod -R g+rx /var/www
    chmod -R g+rwx /var/www
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-27
      • 1970-01-01
      • 2014-03-27
      • 2015-11-14
      • 2014-09-02
      • 2015-08-26
      • 2013-04-28
      相关资源
      最近更新 更多