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 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀