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

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

 &lt;?php 
exec(“gcc -o hello hello.c 2&gt;&amp; 1”,$ output,$ return_value);
echo $ output [0];
</ code> </ pre>

通过Web浏览器调用时,我得到了以下输出(我使用nginx作为Web服务器)。</ p>


gcc:尝试执行'cc1'时出错:execvp:没有这样的文件或目录</ p>
</ blockquote>

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

如果我在我的PHP代码中附加gcc的绝对路径,如下所示:</ p>

 &lt;?php 
exec(“/ usr / bin / gcc -o hello hello.c 2&gt;&amp; 1“,$ output,$ return_value);
echo $ output [0];
</ code> </ pre>

I 得到以下输出。</ p>


collect2:致命错误:找不到'ld'</ p>
</ blockquote>

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

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

OS:Ubuntu 14.04 </ p>

nginx:1.6.2 </ p>

< p> PHP 5.5.9 </ p>
</ div>

展开原文

原文

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个回答



我在Arch Linux上使用nginx 1.10和PHP 5.6时遇到了同样的问题(完全相同......)。
相同的PHP代码正在使用Apache / Debian。
当尝试使用Arch / Nginx服务器时,我遇到了cc1错误......我用/ usr / bin / gcc替换了gcc ....我现在卡在了“collect2: 致命错误:无法找到'ld'“</ p>

相同的编译适用于shell ...而ld位于/ usr / bin中。 它在PHP中使用“exec”时不起作用。</ p>

不满意(但工作......)解决方案</ h2>

运行gcc -v .....(只需在你的编译行中添加-v)我可以看到:</ p>

  ... 

COLLECT_GCC_OPTIONS =' - v'' - D''exit = noexit''-D''_exit = noexit'...
/usr/lib/gcc/x86_64-pc-linux-gnu/6.1.1/collect2 -plugin ...(&lt; =很长的行)\ ncollect2:致命错误:找不到'ld'
</ code> </ pre>

然后我做了:</ p>

  cd / usr / lib  /gcc/x86_64-pc-linux-gnu/6.1.1/#& lt;  -  collect2的路径(见上文)
ln -s / usr / bin / ld ld
</ code> </ pre>
\ n

现在ld prog在与collect2相同的目录中可用。
它可以工作。</ p>

我现在正在寻找更好的解决方案...... :) </ p> \ n </ div>

展开原文

原文

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



为PHP设置 PATH </ code>变量,因为它可能依赖于自己的环境变量并忽略系统的< 代码> PATH </ code>变量。 </ p>

例如,我的 .env </ code>文件底部有以下行:</ p>

PATH = / usr / local / sbin:/ usr / local / bin:/ usr / sbin:/ usr / bin:/ sbin:/ bin </ code> </ p>

有了这个,执行< code> gcc </ code>(不提供完整路径 / usr / bin / gcc </ code>)正常工作。</ p>

如何从PHP编译GCC 。 我以为我也会在这里发布答案,因为这个问题比其他问题更受关注。</ p>
</ div>

展开原文

原文

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:

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

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.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问