【发布时间】:2015-06-08 12:24:32
【问题描述】:
我尝试在 PHP 中使用 exec 函数来编译带有 gcc 的源文件,代码如下。
<?php
exec("gcc -o hello hello.c 2>&1", $output, $return_value);
echo $output[0];
通过网络浏览器调用时得到以下输出(我使用 nginx 作为网络服务器)。
gcc:尝试执行 'cc1' 时出错:execvp:没有这样的文件或目录
但是,如果我直接在命令 shell 上运行 gcc -o hello hello.c 或直接在 shell 上使用 php my_file.php 调用,两种方式都编译成功.
如果我在我的 PHP 代码中添加 gcc 的绝对路径,如下所示:
<?php
exec("/usr/bin/gcc -o hello hello.c 2>&1", $output, $return_value);
echo $output[0];
我得到了以下输出。
collect2:致命错误:找不到'ld'
所以,我认为问题在于我的网络服务器(nginx)不知道系统路径环境变量来查找 gcc 和其他 gcc 依赖项所在的 /usr/bin。
如何让PHP exec函数识别nginx上的系统环境变量?
操作系统:Ubuntu 14.04
nginx:1.6.2
PHP 5.5.9
【问题讨论】:
标签: php gcc nginx environment-variables exec