简体   繁体   English

Apache下的PHP exec()在运行脚本时返回null和255

[英]PHP exec() under Apache returns null and 255 when running a script

Ok, after having bashed my head on this for hours I decided to ask for help. 好吧,经过几个小时的讨论,我决定寻求帮助。 I have a Windows Server 2008 running Apache 2.4 and PHP 7.1. 我有一个运行Apache 2.4和PHP 7.1的Windows Server 2008。 My application must run a PHP script on the server when the user clicks a button on the browser. 当用户单击浏览器上的按钮时,我的应用程序必须在服务器上运行PHP脚本。

This is working fine on my desktop with Windows 10. However, on the server, exec() returns "null" and an exit code of 255. 这在使用Windows 10的桌面上运行正常。但是,在服务器上,exec()返回“null”,退出代码为255。

I read all I could find on exec() issues and tried the following: 我阅读了exec()问题上的所有内容并尝试了以下内容:

exec("C:\\PHP7\\php.exe -v", $output);

I got the proper response containing PHP's version information. 我得到了包含PHP版本信息的正确响应。

Then I decided to check the configuration files: 然后我决定检查配置文件:

exec("C:\\PHP7\\php.exe --ini", $output);

All files were in place. 所有文件都已到位。

Then I decided to perform a syntax check on my script: 然后我决定对我的脚本执行语法检查:

exec("C:\\PHP7\\php.exe -d display_errors=1 -l C:\\Apache24\\htdocs\\script.php", $output);

No errors were found. 没有发现错误。

Finally I decided to check the user account: 最后我决定检查用户帐户:

exec("whoami", $output);

Got "NT Authority\\SYSTEM" as expected. 按预期获得“NT Authority \\ SYSTEM”。 To make sure that the script was able to run under the SYSTEM account I used SysInternals psexec: 为了确保脚本能够在SYSTEM帐户下运行,我使用了SysInternals psexec:

psexec -s C:\PHP7\php.exe C:\Apache24\htdocs\script.php

Everything ran smoothly. 一切顺利。

In other words, the script shows no problems when executed from the command line, either under a user account or the system account. 换句话说,在从用户帐户或系统帐户下的命令行执行时,脚本不会显示任何问题。 I have also proven that PHP is being properly invoked by exec(). 我还证明了exec()正在调用PHP。

So, then I decided to check for "hidden" errors in my code adding the following two lines to the very beginning of the script: 那么,我决定在我的代码中检查“隐藏”错误,在脚本的最开头添加以下两行:

error_reporting(E_ALL);
ini_set('display_errors', 1);

But, no joy. 但是,没有快乐。 And I'm out of ideas. 而且我没有想法。

Can any good soul help me on this? 有没有好心灵可以帮助我呢?

Thanks a bunch, 谢谢一堆,

Miguel. 米格尔。

Finally! 最后! The key to the answer was here: PHP exec() git fetch failing with return value 255 . 答案的关键在于: PHP exec()git fetch失败,返回值为255

I was unable to see any errors, even adding the "2>&1" pipe redirection to my commands. 我无法看到任何错误,甚至将“2>&1”管道重定向添加到我的命令中。 After reading that article, I learned that proc_open() is way better than exec(). 阅读完那篇文章后,我了解到proc_open()比exec()更好。 Quoting from PHP's documentation: 引用PHP的文档:

proc_open() is similar to popen() but provides a much greater degree of control over the program execution proc_open()类似于popen(),但提供了对程序执行的更大程度的控制

So, I replaced my exec() for a few lines of code (refer to the example in the manual) and found that the problem was being caused by the Zend Opcache, which was enabled for CLI. 因此,我将exec()替换为几行代码(请参阅手册中的示例),并发现问题是由为CLI启用的Zend Opcache引起的。 The quickier solution for me was to disable it in the command line: 对我来说更快的解决方案是在命令行中禁用它:

php.exe -d opcache.enable_cli=0 myscript.php

Voilà! 瞧! Problem solved! 问题解决了!

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

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