2015-04-03 11:55
浏览 436

nginx上的PHP exec函数无法识别系统环境变量

I try to use exec function in PHP to compile a source file with gcc with the following code.

exec("gcc -o hello hello.c 2>&1", $output, $return_value);
echo $output[0];

I got the following output when calling via web browser (I use nginx as a web server).

gcc: error trying to exec 'cc1': execvp: No such file or directory

However, if I run gcc -o hello hello.c on the command shell directly or call with php my_file.php on the shell directly, both ways compile successfully.

If I append the absolute path to gcc in my PHP code like this:

exec("/usr/bin/gcc -o hello hello.c 2>&1", $output, $return_value);
echo $output[0];

I got the following output.

collect2: fatal error: cannot find 'ld'

So, I think the problem is my webserver (nginx) doesn't know the system path environment variable to find /usr/bin which gcc and other gcc-dependencies resides in.

How can I let PHP exec function recognize system environment variables on nginx?

OS: Ubuntu 14.04

nginx: 1.6.2

PHP 5.5.9

图片转代码服务由CSDN问答提供 功能建议

我尝试在PHP中使用 exec 函数来编译的源文件 gcc ,代码如下。

exec(“gcc -o hello hello.c 2&gt;&amp; 1”,$ output,$  return_value); 
echo $ output [0]; 



但是, 如果我直接在命令shell上运行 gcc -o hello hello.c 或直接在shell上使用 php my_file.php 调用,则两种方式都可以成功编译。


exec(“/ usr  / bin / gcc -o hello hello.c 2&gt;&amp; 1“,$ output,$ return_value); 
echo $ output [0]; 

I 得到以下输出。


所以, 我认为问题是我的webserver(nginx)不知道系统路径环境变量来查找gcc和其他gcc-dependencies所在的/ usr / bin。

我怎么能让 PHP exec函数识别nginx上的系统环境变量?

OS:Ubuntu 14.04

nginx:1.6.2 < p> PHP 5.5.9

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongmi6102 2016-07-21 07:35

    I'm stuck on the same problem (exactly the same...) using nginx 1.10 and PHP 5.6 on Arch Linux. The same PHP code was working on Apache/Debian. When trying the Arch/Nginx server, I had the cc1 error... I replaced gcc by /usr/bin/gcc.... and I am now stuck on "collect2: fatal error: cannot find 'ld'"

    The same compilation works with a shell... and ld is in /usr/bin. It just does not work when using "exec" in PHP.

    Not satisfying (but working...) solution

    By running gcc -v .....(just add -v to your compilation line) I could see :

    COLLECT_GCC_OPTIONS='-v' '-D' 'exit=noexit' '-D' '_exit=noexit' ...
     /usr/lib/gcc/x86_64-pc-linux-gnu/6.1.1/collect2 -plugin ... (<= very long line)
    collect2: fatal error: cannot find 'ld'

    Then I did :

    cd /usr/lib/gcc/x86_64-pc-linux-gnu/6.1.1/  # <- path to collect2 (see above)
    ln -s /usr/bin/ld ld

    Now ld prog is available in the same dir as collect2. And it works.

    I am now looking for a better solution... :)

    点赞 评论
  • dpn68721 2016-08-01 04:27

    Set the PATH variable for PHP, as it may rely on its own environment variables and ignore the system's PATH variable.

    For example, I have the following line at the bottom of my .env file:


    With this, executing gcc (without giving the full path /usr/bin/gcc) works correctly.

    Duplicate of How compile GCC from php. Thought I'd post the answer here as well, since this question received a lot more attention than the other one.

    点赞 评论

相关推荐 更多相似问题