【问题标题】:Set ENV PATH of php and ffmpeg to use it inside PHP scripts with CLI like shell_exec()设置 php 和 ffmpeg 的 ENV PATH 以在 PHP 脚本中使用 shell_exec() 等 CLI
【发布时间】:2015-04-25 22:22:54
【问题描述】:

我想在这样的 php 脚本中执行命令:

<?php
shell_exec(php myfile.php)

<?php
shell_exec(ffmpeg -i ...)

我的问题,我认为 php 和 ffmpeg 路径在我的 apache 环境中没有正确配置,因为当我执行此操作时:

<?php
var_dump(shell_exec("which php"));
var_dump(shell_exec("which ffmpeg"));

我得到这个答案:

string '/usr/bin/php' (length=13)
null

但在终端输入时:

which php
which ffmpeg

我得到这个答案:

/usr/local/opt/php55/bin/php
/usr/local/bin/ffmpeg

那么我怎样才能正确设置 php 和 ffmpeg 环境路径而不总是重新输入完​​整路径?

我在 Mac OsX 10.10 下,我用 brew 安装了 php 和 ffmpeg。

【问题讨论】:

    标签: php apache path ffmpeg environment-variables


    【解决方案1】:

    您可以像在 shell 中一样将环境变量放入命令中。

    示例:

    shell_exec('PATH=/usr/local/bin:/usr/local/opt/php55/bin:$PATH which ffmpeg');
    

    替代方案:

    如果你不想弄脏代码,你可以配置你的 Apache 环境变量如下。

    苹果机:

    编辑

    /System/Library/LaunchDaemons/org.apache.httpd.plist
    

    并添加

    <key>EnvironmentVariables</key>
    <dict>
        <key>PATH</key>
        <string>/usr/local/bin:/usr/local/opt/php55/bin:/usr/bin:/bin:/usr/sbin:/sbin</string>
    </dict>
    

    (从$PATH environment variable for apache2 on mac复制的答案)

    其他平台:(供您参考)

    https://serverfault.com/questions/151328/setting-apache2-path-environment-variable

    【讨论】:

    • 谢谢你解决了我的问题 ;) 现在当我做 var_dump(shell_exec("which php"));我得到了正确的道路。但是 var_dump(getenv('PATH'));再给我字符串'/usr/bin:/bin:/usr/sbin:/sbin'(长度=29)。正常吗?
    【解决方案2】:

    ffmpeg 添加到您的 PATH 环境变量中,重新启动 Apache 服务器并重试

    【讨论】:

    • 我已经在 .bash_profile 的环境变量中添加了 ffmpeg 和 php,如下所示:export PATH="$(brew --prefix homebrew/php/php55)/bin:/usr/local/mysql /bin:/usr/local/sbin:$PATH"。所以这就是为什么当我输入 which php 或 which ffmpeg 时我可以在我的终端中访问正确的路径,但这不会传播到我的 apache 服务器
    • 在那之后你重启过你的 apache 吗?
    • 是的,我做了,但没有任何改变
    • var_dump(getenv('PATH'));
    • 字符串 '/usr/bin:/bin:/usr/sbin:/sbin' (长度=29)
    猜你喜欢
    • 2011-08-13
    • 1970-01-01
    • 2012-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-13
    相关资源
    最近更新 更多