PHP shell_exec$PATH变量与终端$PATH变量不匹配

PHP shell_exec$PATH变量与终端$PATH变量不匹配,php,variables,path,env,Php,Variables,Path,Env,当我试图通过shell_exec在我的应用程序中使用一些系统包(如ImageMagick)时,我遇到了一些问题 如果我自己定义路径,包就可以正常工作。ImageMagick可以工作,但由于它使用Ghostscript包,所以此时出错,因为我的php path变量遗漏了一条路径。我可以得到ImageMagick等的版本 我的PHP path变量包含(通过shell_exec输出): 我的包使用“/usr/local/bin”,因此我想将此路径添加到shell_exec使用的php变量中 直接通过终

当我试图通过shell_exec在我的应用程序中使用一些系统包(如ImageMagick)时,我遇到了一些问题

如果我自己定义路径,包就可以正常工作。ImageMagick可以工作,但由于它使用Ghostscript包,所以此时出错,因为我的php path变量遗漏了一条路径。我可以得到ImageMagick等的版本

我的PHP path变量包含(通过shell_exec输出):

我的包使用“/usr/local/bin”,因此我想将此路径添加到shell_exec使用的php变量中

直接通过终端回显$PATH时的输出为:

/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
然后,当我通过终端“$php script.php”运行我的php脚本时,工作完成得很好。当我通过浏览器运行shell_exec的脚本时,它会失败。因为我认为这与在GhostScript需要执行任务时不匹配的env路径有关

我正在搜索以包括“/usr/local/bin”或同步整个路径。希望能得到一些建议,如何做到这一点。多谢各位

更新

php脚本:

var_dump(shell_exec('/usr/local/bin/gs -dNOPAUSE -sDEVICE=jpeg -r144 -sOutputFile=/www/sites/domain/public_html/test.jpg /www/sites/domain/public_html/test.pdf'));
错误日志

GPL Ghostscript 9.18: **** Could not open the file '/www/sites/domain/public_html/test.jpg'.
GPL Ghostscript 9.18: Unrecoverable error, exit code 1
浏览器输出

GPL Ghostscript 9.18 (2015-10-05)
Copyright (C) 2015 Artifex Software, Inc.  All rights reserved.
This software comes with NO WARRANTY: see the file PUBLIC for details.
Processing pages 1 through 18.
Page 1
Error: /invalidfileaccess in --showpage--
Operand stack:
   1   true
Execution stack:
   %interp_exit   .runexec2   --nostringval--   --nostringval--   --nostringval--   2   %stopped_push   --nostringval--   --nostringval--   --nostringval--   false   1   %stopped_push   1967   1   3   %oparray_pop   196'... (length=1044)

php/apache的默认用户是“www-data”。您可以尝试这样做:您是否可以更新您的问题以包括对
shell_exec
的调用?在2020年出现此问题但找不到答案:/出于任何原因编辑/etc/environment或/etc/apache2/envvars对我不起作用
GPL Ghostscript 9.18 (2015-10-05)
Copyright (C) 2015 Artifex Software, Inc.  All rights reserved.
This software comes with NO WARRANTY: see the file PUBLIC for details.
Processing pages 1 through 18.
Page 1
Error: /invalidfileaccess in --showpage--
Operand stack:
   1   true
Execution stack:
   %interp_exit   .runexec2   --nostringval--   --nostringval--   --nostringval--   2   %stopped_push   --nostringval--   --nostringval--   --nostringval--   false   1   %stopped_push   1967   1   3   %oparray_pop   196'... (length=1044)