Cannot start node from web server

I tried to start npmfrom a PHP web page, but it will never start. I always got exit code 127 and did not output. After some testing, I narrowed the problem down to shebang in npm, which looks like this:

#!/usr/bin/env node

Pretty standard, but I did some test codes:

<?php
$result = exec("/usr/bin/env node --version", $output, $exit);
var_dump($result);
var_dump($exit);
$result = exec("node --version", $output, $exit);
var_dump($result);
var_dump($exit);
$result = exec("/usr/bin/env gzip --version", $output, $exit);
var_dump($result);
var_dump($exit);

And got this output in my browser:

string(0) ""
int(127)
string(6) "v8.4.0"
int(0)
string(28) "Written by Jean-loup Gailly."
int(0)

I included catch_workers_outputPHP-FPM in the configuration and saw this in the PHP log:

[18-Aug-2017 15:15:35] WARNING: [pool web] child 27872 said into stderr: "/usr/bin/env: "
[18-Aug-2017 15:15:35] WARNING: [pool web] child 27872 said into stderr: "node"
[18-Aug-2017 15:15:35] WARNING: [pool web] child 27872 said into stderr: ": No such file or directory"
[18-Aug-2017 15:15:35] WARNING: [pool web] child 27872 said into stderr: ""

I also tried starting exec("which node")from a web server and saw this in the PHP log:

[18-Aug-2017 15:31:12] WARNING: [pool web] child 27873 said into stderr: "which: no node in ((null))"

I tried to run var_dump(exec('echo $PATH'))and got this output:

string(28) "/usr/local/bin:/bin:/usr/bin"

This is similar to how the team envprocesses the path. I tried to install it manually using the directive fastcgi_param PATHin nginx.conf, but it did not change the above output.

env : PATH:

Array
(
    [0] => USER=nginx
    [1] => PWD=/var/www/html
    [2] => SHLVL=1
    [3] => HOME=/var/cache/nginx
    [4] => _=/bin/env
)

RHEL SELinux, PHP-FPM 5.6.31 UNIX Nginx 1.12.1. /usr/local/bin/node /usr/local/nodejs/bin/node, 775 . nginx /usr/local/nodejs . ?


, PHP- CLI ( nginx), , , CGI/FPM.

$ su -s "/bin/sh" -c "/var/www/html/test.php" nginx
string(6) "v8.4.0"
int(0)
string(6) "v8.4.0"
int(0)
string(28) "Written by Jean-loup Gailly."
int(0)
+4
2

PATH /etc/php5/fpm/pool.d/www.conf, /usr/local/bin/node.

ubuntu :

;env[PATH] = /usr/local/bin:/usr/bin:/bin 

env[PATH] = /usr/local/bin/node 

. RHEL , , .

php-fpm.


, npm:

exec('PATH=$PATH:/usr/local/bin/node npm');

, .

+2

, ... script, ngnix ?

, ngnix php-fpm , PHP-FPM script. , , PHP-FPM- - , /usr/bin/env node.

, ngnix , PHP-FPM. PHP-FPM .

, , , PHP-FPM (www-data?) , node, ngnix .

: https://serverfault.com/a/52703

# 1:

.

/usr/local/bin/node /usr/local/nodejs/bin/ node, 775 . nginx /usr/local/nodejs

/usr/local tree withoux -x . , /usr/local .

, script ( CLI, FPM, as ngnix, etx) :

<?php

$myuid = getmyuid();
$mygid = getmygid();

// you should change to check /usr/local/nodejs/bin/node
$path = '/usr/local/n/versions/node/7.2.0/bin/node'; 

$patharray = array_filter(explode('/', $path));

$dump = "%s \t %s \t %s%s%s \t %s" . PHP_EOL;

$fileOrFolder = __FILE__;
printf("uid \t gid \t rwx \t file" . PHP_EOL);
printf($dump, $myuid, $mygid, (is_readable($fileOrFolder) ? 'r' : '-'), (is_writable($fileOrFolder) ? 'w' : '-'), (is_executable($fileOrFolder) ? 'x' : '-'), $fileOrFolder);

$fileOrFolder = '';
while(count($patharray) > 0) {
    $fileOrFolder .= ('/' .  array_shift($patharray));

    $myuid = posix_getpwuid(fileowner($fileOrFolder));
    $mygid = posix_getgrgid(filegroup($fileOrFolder));

    printf($dump, $myuid['name'], $mygid['name'], (is_readable($fileOrFolder) ? 'r' : '-'), (is_writable($fileOrFolder) ? 'w' : '-'), (is_executable($fileOrFolder) ? 'x' : '-'), $fileOrFolder);

    if (is_dir($fileOrFolder) && !is_executable($fileOrFolder)) {
        echo PHP_EOL . "Ouh, cant go further cause of privelages" . PHP_EOL;
        break;
    }
}

Mine output proofs, , , , PHP :

@riddick:~/temp$ php phpowner.php
uid      gid     rwx     file
1000     1000    rw-     /home/yergo/temp/phpowner.php
root     root    r-x     /usr
root     root    r-x     /usr/local
root     root    r-x     /usr/local/n
root     root    r-x     /usr/local/n/versions
root     root    r-x     /usr/local/n/versions/node
root     root    r-x     /usr/local/n/versions/node/7.2.0
                 r-x     /usr/local/n/versions/node/7.2.0/bin
                 r-x     /usr/local/n/versions/node/7.2.0/bin/node
0

Source: https://habr.com/ru/post/1683984/


All Articles