mcheah
mcheah

Reputation: 1326

node doesn't work when run through PHP

I'm trying to create a simple PHP script that runs webpack. It runs fine when I run it in the terminal, but gives me memory errors when I run it from PHP. This is what my code looks like:

PHP:

shell_exec('webpack -p --config /home/[username]/config/config.production.js 2>&1');

results in this:

<--- Last few GCs --->

[20920:0x405ede0]      786 ms: Scavenge 40.6 (63.1) -> 31.1 (66.6) MB, 3.4 / 0.0 ms  allocation failure 
[20920:0x405ede0]      867 ms: Scavenge 45.2 (66.6) -> 35.6 (67.1) MB, 8.5 / 0.0 ms  allocation failure 


<--- JS stacktrace --->
Cannot get stack trace in GC.
FATAL ERROR: NewSpace::Rebalance Allocation failed - process out of memory
 1: node::Abort() [node]
 2: 0x8d04bc [node]
 3: v8::Utils::ReportOOMFailure(char const*, bool) [node]
 4: v8::internal::V8::FatalProcessOutOfMemory(char const*, bool) [node]
 5: 0xa7006b [node]
 6: v8::internal::MarkCompactCollector::Evacuate() [node]
 7: v8::internal::MarkCompactCollector::CollectGarbage() [node]
 8: v8::internal::Heap::MarkCompact() [node]
 9: v8::internal::Heap::PerformGarbageCollection(v8::internal::GarbageCollector, v8::GCCallbackFlags) [node]
10: v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [node]
11: v8::internal::Factory::NewByteArray(int, v8::internal::PretenureFlag) [node]
12: v8::internal::TranslationBuffer::CreateByteArray(v8::internal::Factory*) [node]
13: v8::internal::compiler::CodeGenerator::PopulateDeoptimizationData(v8::internal::Handle) [node]
14: v8::internal::compiler::CodeGenerator::FinalizeCode() [node]
15: v8::internal::compiler::PipelineImpl::FinalizeCode() [node]
16: v8::internal::compiler::PipelineCompilationJob::FinalizeJobImpl() [node]
17: v8::internal::Compiler::FinalizeCompilationJob(v8::internal::CompilationJob*) [node]
18: v8::internal::OptimizingCompileDispatcher::InstallOptimizedFunctions() [node]
19: v8::internal::StackGuard::HandleInterrupts() [node]
20: v8::internal::Runtime_StackGuard(int, v8::internal::Object**, v8::internal::Isolate*) [node]
21: 0x24ffb0c842fd

free -m gives me this:

                 total       used       free     shared    buffers     cached
Mem:         31956      29146       2810         51        947      24470
-/+ buffers/cache:       3728      28228
Swap:         5999          0       5999

vmstat gives me this:

    procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 0  0     64 2877076 969764 25058456    0    0     0    10    0    0  0  0 99  0  0

It doesn't seem there are any glaring memory issues to me? If I put the webpack command into a npm script, and run that from PHP instead, I'll get this error: (but running the npm script from the terminal is fine too.)

npm ERR! path /home/[username]
npm ERR! code ENOMEM
npm ERR! errno -12
npm ERR! syscall scandir
npm ERR! ENOMEM: not enough memory, scandir '/home/[username]'
glob error { Error: ENOMEM: not enough memory, scandir '/home/[username]'
  errno: -12,
  code: 'ENOMEM',
  syscall: 'scandir',
  path: '/home/[username]' }

I'm assuming this is just the same error just thrown by Node instead. When I try to run any other NPM command from PHP (like npm list) I'll also get the ENOMEM error, which leads me to believe it's only a node issue and not anything else.

Does anyone have any idea what's going on or why this might be happening?

Versions:

nvm ls

->      v9.11.1
default -> node (-> v9.11.1)
node -> stable (-> v9.11.1) (default)
stable -> 9.11 (-> v9.11.1) (default)
iojs -> N/A (default)
lts/* -> lts/carbon (-> N/A)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.14.1 (-> N/A)
lts/carbon -> v8.11.1 (-> N/A)

PHP Version: 5.5.35

Linux Dist: CentOS release 6.9 (Final)

shell_exec('env'); gives me the following:

[0] => SERVER_SIGNATURE=
[1] => HTTP_X_HTTPS=1
[2] => SSL_TLS_SNI=www.[mydomain.com]
[3] => UNIQUE_ID=Wt5Y@K33-aQAAGEyFT8AAAAR
[4] => HTTP_USER_AGENT=Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36
[5] => SERVER_PORT=443
[6] => PHPRC=/home/[username]/public_html
[7] => HTTP_HOST=www.[mydomain.com]
[8] => DOCUMENT_ROOT=/home/[username]/public_html
[9] => SCRIPT_FILENAME=/home/[username]/public_html/template-files/scripts/run-webpack.php
[10] => HTTPS=on
[11] => REQUEST_URI=/template-files/scripts/run-webpack.php
[12] => SCRIPT_NAME=/template-files/scripts/run-webpack.php
[13] => SCRIPT_URI=https://www.[mydomain.com]/template-files/scripts/run-webpack.php
[14] => HTTP_CONNECTION=keep-alive
[15] => REMOTE_PORT=7941
[16] => PATH=/bin:/home/[username]/.nvm/versions/node/v9.11.1/bin:/usr/local/cpanel/3rdparty/lib/path-bin:/usr/local/jdk/bin:/home/[username]/perl5/bin:/usr/local/cpanel/3rdparty/lib/path-bin:/usr/kerberos/bin:/home/[username]/node_modules/.bin:/usr/local/cpanel/3rdparty/lib/path-bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/cpanel/composer/bin:/usr/local/bin:/usr/X11R6/bin:/home/[username]/bin
[17] => SCRIPT_URL=/template-files/scripts/run-webpack.php
[18] => CONTEXT_PREFIX=
[19] => PWD=/home/[username]/public_html/template-files/scripts
[20] => SERVER_ADMIN=webmaster@[mydomain.com]
[21] => REQUEST_SCHEME=https
[22] => REDIRECT_STATUS=200
[23] => TZ=America/Los_Angeles
[24] => HTTP_ACCEPT_LANGUAGE=en-US,en;q=0.9
[25] => HTTP_ACCEPT=text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
[26] => REMOTE_ADDR=[myIP]
[27] => SHLVL=1
[28] => SERVER_NAME=www.[mydomain.com]
[29] => SERVER_SOFTWARE=Apache
[30] => QUERY_STRING=
[31] => SERVER_ADDR=[myIP]
[32] => GATEWAY_INTERFACE=CGI/1.1
[33] => HTTP_UPGRADE_INSECURE_REQUESTS=1
[34] => SERVER_PROTOCOL=HTTP/1.1
[35] => HTTP_CACHE_CONTROL=max-age=0
[36] => HTTP_ACCEPT_ENCODING=gzip, deflate, br
[37] => REQUEST_METHOD=GET
[38] => HTTP_COOKIE=_ga=[cookieID…etc]
[39] => CONTEXT_DOCUMENT_ROOT=/home/[username]/public_html
[40] => _=/bin/env

echo shell_exec('bash -c "ulimit -a"'); Results in this:

core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 127717
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 16384
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 10240
cpu time               (seconds, -t) unlimited
max user processes              (-u) 400
virtual memory          (kbytes, -v) 1048576
file locks                      (-x) unlimited

Please let me know if i can provide any more information about this issue. Thanks for the help!

Upvotes: 3

Views: 894

Answers (1)

cdhowie
cdhowie

Reputation: 169143

Based on the diagnostic information you've given, the web server environment is operating with a memory limit (likely via the setrlimit() system call). This call allows a process to constrain how much memory it or its children are allowed to allocate.

You will need to locate and change the relevant setting to raise or disable the memory limit.

Upvotes: 1

Related Questions