我一直在关注这个
answer
我不知道我到底做错了什么。当我调用我的api端点(通过postman)时,当我的脚本试图生成和编译脚本c脚本时,我得到以下错误。
我的php代码
exec('
cat > wrapper.c <<CONTENT
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int
main (int argc, char *argv[])
{
setuid (0);
system ("/bin/sh /var/www/html/myapp/script.sh");
return 0;
}
CONTENT
gcc wrapper.c -o php_root 2>&1
', $output, $returnVar);
var_dump($output);
var_dump($returnVar);
die();
我得到的错误是
gcc: error trying to exec 'cc1': execvp: No such file or directory'
什么时候发生
gcc wrapper.c -o php_root 2>&1
命令已执行
在bash中运行所有这些命令都很好,但是当它们通过PHP exec执行时,我得到了一个错误。
gcc -v
(venv) certify-dev: /var/www/html/certify/public $ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/5/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 5.4.0-6ubuntu1~16.04.10' --with-bugurl=file:///usr/share/doc/gcc-5/README.Bugs --enable-languages=c,ada,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-5 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-5-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-5-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-5-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.10)
你们能帮我找出问题出在哪里吗。
如果您有任何其他问题,请让我知道,我会提供答案。谢谢您!
更新
更新的行
/usr/bin/gcc wrapper.c -o php_root 2>&1
错误
0 => string 'collect2: fatal error: cannot find 'ld'' (length=39)
1 => string 'compilation terminated.' (length=23)
在检查了我的php路径(由@ChrisTurner建议)之后,我发现我的shell缺少php可执行文件。。。
我的php代码
$phpFinder = new PhpExecutableFinder;
if (!$phpPath = $phpFinder->find()) {
throw new \Exception('The php executable could not be found, add it to your PATH environment variable and try again');
}
var_dump($phpPath); /usr/bin/php
var_dump(PHP_BINDIR); /usr/bin/php
还有贝壳
dev: /var/www/html/myapp $ sudo -H -u www-data bash -c 'echo $PATH'
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/local/go/bin
所以我加了一句
export PATH="$PATH:/usr/bin/php"
myApp: /var/www/html/certify $ sudo -H -u www-data bash -c 'echo $PATH'
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/local/go/bin:/usr/bin/php
代码仍在抛出错误