I'm working on a web application that uses both C++ and PHP. The issue at hand revolves around calling a CPP executable from PHP.
Environment:
- CentOS7
- PHP 7.4 (Remi)
- Apache 2.4.6
- C++ 11
Apache runs as apache:[appgroup], and all files are owned by the same. Executable files are executable by owner and group.
However, calling the executable from php -- whether using exec(), shell_exec(), or system() -- doesn't work, despite the code working when called from the command line.
I've done some testing by running
echo exec("whoami");
var_dump(file_exists('/var/www/html/project/bin/main'));
var_dump(is_executable('/var/www/html/project/bin/main'));
(With main being a compiled C++ file, that exists, that works when called from the command line). PHP can see the file, but "is_executable" returns false. "Whoami" returns apache, as expected.
I've messed with httpd.conf, php.ini, and tried calling the file several different ways. I've created shell and "hello world" C++ files, placed them in the same directory as the php script, and still had no luck.
No matter what, the following line will not do anything (nor will any call to any other C++ executable or shell script).
exec("/var/www/html/project/bin/main > /dev/null 2>&1");
What is stopping this from working?
/dev/null, return it to the PHP script so you can report the error./var/www/ ... etc.may not exist from your PHP code's perspective, or may have a physical location different than you expect. Try running it from a relative path within the Document Root. E.g., assuming it is possible to visithttp://yourdomain.com/project/index.phpbecause file/var/www/html/project/index.phpexists, try running exec for./project/bin/main.