I installed PhantomJS via npm on my remote server and added this to .bashrc: PATH=~/node/bin:$PATH
If I run phantomjs -v in ssh console then I get the expected result: 1.9.7
Now when I'm trying to use it in PHP script:
<?
putenv("PATH=~/node/bin:$_ENV[PATH]");
echo shell_exec('echo $PATH');
echo shell_exec('phantomjs -v 2>&1');
I get this error (and it's not sh: phantomjs: command not found):
~/node/bin:/bin:/usr/bin
/usr/bin/env: node: No such file or directory
I can use the full path ~/node/lib/node_modules/phantomjs/lib/phantom/bin/phantomjs to get the correct response.
But the question is: why do php exec() and console behave differently?
Is that because some sh/bash? And maybe they are using different ENV?
PHP runs as different user, apache, or nginx. Those users hasn't homedir, so path ~/node/bin doesn't exists. Please add to PATH global path to nodejs, and run your script after.
Scripts for node.js or PHP or even CRON, use FULL PATH names to avoid these errors. Depending on your config, PHP runs as user "www-data" (eg: ubuntu) or "nobody". Hence the paths doesn't exists.
Note: if you look into /etc/ folder, you can see examples of scripts which uses FULL PATHS
shell_exec does not get ENV vars from the current process.
You can use passthru for this though
putenv("HELLO=world");
passthru("echo \$HELLO");
// world
It's worth noting that passthru also receives STDIN and writes directly to STDOUT and STDERR