PHP exec() 通过浏览器执行时不会执行 shell 命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/538939/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:01:51  来源:igfitidea点击:

PHP exec() will not execute shell command when executed via browser

phpsystemexec

提问by Alex

I have a certain PHP script that calls exec() to execute a command to convert a PDF to JPG. This command works fine in bash.

我有一个特定的 PHP 脚本,它调用 exec() 来执行将 PDF 转换为 JPG 的命令。此命令在 bash 中运行良好。

To preempt your initial troubleshooting guesses, note the following:

要抢占您最初的故障排除猜测,请注意以下事项:

  • safe_mode = Off
  • Permission on the directory containing the PDF and the script is set to 777, and this directory is also where the JPG is being written.
  • The command I am passing to exec() explicitly points to the binary being used (e.g. /usr/local/bin/convert).
  • display_errors = On
  • error_reporting = E_ALL
  • disable_functions = [blank]
  • I am echoing exec()'s output and it returns nothing. The command being run by default returns nothing.
  • 安全模式 = 关闭
  • 包含 PDF 和脚本的目录的权限设置为 777,该目录也是写入 JPG 的位置。
  • 我传递给 exec() 的命令明确指向正在使用的二进制文件(例如 /usr/local/bin/convert)。
  • display_errors = 开
  • error_reporting = E_ALL
  • disable_functions = [空白]
  • 我正在回显 exec() 的输出,但它什么都不返回。默认情况下运行的命令不返回任何内容。

When I call this PHP script from the browser (visiting http://www.example.com/script.php), exec() does not execute its argument.

当我从浏览器(访问http://www.example.com/script.php)调用这个 PHP 脚本时, exec() 不执行它的参数。

IMPORTANT: I know that there are no issues with my script or the way I have constructed the bash command, because from bash, I can execute the script with 'php' and it works (e.g. 'php script.php' converts the file)

重要提示:我知道我的脚本或我构建 bash 命令的方式没有问题,因为从 bash 中,我可以使用 'php' 执行脚本并且它可以工作(例如,'php script.php' 转换文件)

I have also tried switching out exec() with system().

我也试过用 system() 切换 exec()。

Last, I have had this issue once before in the past but cannot remember how I fixed it.

最后,我过去曾经遇到过这个问题,但不记得我是如何解决它的。

I know there is something I am missing, so I hope someone else has experienced this as I have and remembers how to fix it!

我知道我遗漏了一些东西,所以我希望其他人也像我一样经历过这种情况并记得如何解决它!

Thank you in advance for any assistance you can provide.

预先感谢您提供的任何帮助。

Alex

亚历克斯

回答by Greg

Add 2>&1to the end of your command to redirect errors from stderr to stdout. This should make it clear what's going wrong.

添加2>&1到命令的末尾以将错误从 stderr 重定向到 stdout。这应该说明出了什么问题。

回答by TomHastjarjanto

Just some guess, it might be that your webserver process user does not have privileges to do so.

只是一些猜测,可能是您的网络服务器进程用户没有这样做的权限。

回答by Chad Birch

Since it works when from the command-line (which would be under your own user account), it sounds to me like the account the web server is running under (often "www-data") does not have execute permissions on the conversion program.

由于它在命令行(将在您自己的用户帐户下)工作时,在我看来,网络服务器运行的帐户(通常是“www-data”)没有转换程序的执行权限.

回答by Amy Anuszewski

Have you considered file permissions? In the browser, php is running under one user, but when you run it in bash, it is likely running with your user permissions.

你考虑过文件权限吗?在浏览器中,php 在一个用户下运行,但是当您在 bash 中运行它时,它很可能以您的用户权限运行。

It's the first thing I would check.

这是我要检查的第一件事。

Amy

艾米

回答by Thusitha Sumanadasa

The Apache's user www-dataneed to be granted privileges to execute certain applications using sudo.

Apache 的用户www-data需要被授予权限才能使用sudo.

  1. Run the command sudo visudo. Actually we want to edit the file in etc/sudoers.To do that, by using sudo visudoin terminal ,it duplicate(temp) sudoersfile to edit.
  2. At the end of the file, add the following ex:-if we want to use command for restart smokeping and mount command for another action,
  1. 运行命令sudo visudo。实际上我们想编辑文件etc/sudoers。为此,通过sudo visudo在终端中使用,它重复(临时)sudoers文件进行编辑。
  2. 在文件末尾,添加以下示例:-如果我们想使用命令重新启动吸烟和安装命令进行另一个操作,

www-data ALL=NOPASSWD: /etc/init.d/smokeping/restart, /bin/mount

www-data ALL=NOPASSWD: /etc/init.d/smokeping/restart, /bin/mount

(This is assuming that you wish to run restartand mountcommands using super user (root) privileges.)

(这是假设您希望使用超级用户(root)权限运行restartmount命令。)

However, if you wish to run every application using super user privileges, then add the following instead of what's above.You might not want to do that, not for ALLcommands, very dangerous.

但是,如果您希望使用超级用户权限运行每个应用程序,则添加以下内容而不是上面的内容。您可能不想这样做,而不是ALL命令,非常危险。

www-data ALL=NOPASSWD: ALL

3.After edit the sudoers file(by visudowe edit the temp file of sudoersso save and quit temp file(visudo) to write in sudoersfile.(wq!)

3.编辑sudoers文件后(通过visudo我们编辑sudoersso保存并退出临时文件(visudo)的临时文件写入sudoers文件。(wq!

4.That's it, now use exec()in the following manner inside your xxx.phpscript.keep remember to use sudobefore the command use in the php script.

4.就是这样,现在exec()在您的xxx.php脚本中以以下方式使用。记得sudo在php脚本中使用命令之前使用。

ex:-

前任:-

exec ("sudo /etc/init.d/smokeping restart 2>&1");

So in your problem,add the commands that you wish to use in to the step no (2.)as I add and change your php script as what you want.

因此,在您的问题中,将您希望使用的命令添加到step no (2.)我添加并根据需要更改您的 php 脚本中。

回答by ConroyP

Does your Apache/webserver user have the necessary rights to run the shell command?

您的 Apache/webserver 用户是否具有运行 shell 命令的必要权限?

When you run from the cl you are likely running as a different user, which may explain which cl works but via browser doesn't.

当您从 cl 运行时,您可能以不同的用户身份运行,这可以解释哪个 cl 有效,但通过浏览器无效。

回答by ConroyP

It may be due to the different users running the script through the web server and the script through bash.

这可能是由于不同的用户通过 web 服务器运行脚本和通过 bash 运行脚本。

Normally the scripts/exec invoked through server as with user 'www' and this user dont have any write access to your area. but when you run the script in bash then you do have write permissions.

通常,scripts/exec 与用户“www”一样通过服务器调用,并且该用户对您的区域没有任何写访问权限。但是当您在 bash 中运行脚本时,您确实拥有写入权限。

回答by Rudger

Default output device is changed.

默认输出设备已更改。

login as www (after enabling) gives output through shell, but not through php.

以 www 身份登录(启用后)通过 shell 提供输出,而不是通过 php。

回答by Alex

I have determined that this is an issue with ImageMagick, not PHP. I am attempting a few fixes, and if they don't work, I'm going to end up using some PHP shared library (probably imagick) to do the work instead.

我已经确定这是 ImageMagick 的问题,而不是 PHP。我正在尝试一些修复,如果它们不起作用,我最终将使用一些 PHP 共享库(可能是 imagick)来完成这项工作。