doujing1967 2015-04-03 11:55
浏览 702

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

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

<?php
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:

<?php
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

  • 写回答

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... :)

    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器