Nginx and PHP-FPM can’t invoke g++

I  ran into this weird problem when trying to get my sharif-judge fork running in laravel Homestead. Well it ran fine but when it invoke g++ to compile stuff I ended up with error

g++: error trying to exec 'cc1plus': execvp: No such file or directory

The weirdest thing is that I can run g++ just fine from command line, error only arise when invoke g++ with shell_exec. A small test.php file with single line:

echo shell_exec("g++ code.cpp -fno-asm -Dasm=error -lm -O2 -w >/dev/null 2>error");

has confirmed the problem.

2016 update: A fix has been found

A quick google around reveal many individual with similar problem but few answer and none successfully address the problem. When messing around I found that there was nothing wrong with my PHP settings, it turn out to be a environment problem. So I do a quick printenv to get a list of every variable that was set when I invoke g++ from command line and compare it to the output whenn invoke by php-fpm. Then it was time to call putenv PHP function to set each of those variables.

It turned out that the $PATH variable have to be set in order for g++ to execute, so putting


before calling shell_exec easily fix the problem