如果使用 PHP 运行编译,则无法分配内存

Cannot allocate memory if running the compilation with PHP

本文关键字:分配 内存 编译 PHP 运行 如果      更新时间:2023-10-16

我目前正在使用以下命令运行乘客编译的 PHP 脚本:

passenger-install-apache2-module --auto

如果我在 SSH 中运行该命令,它可以正常工作,但是当我从 PHP 使用 exec() 启动它时,我得到"虚拟内存耗尽:无法分配内存"。

c++ -o buildout/support-binaries/CoreMain.o  -Isrc/agent -Isrc/cxx_supportlib -Isrc/cxx_supportlib/vendor-copy -Isrc/cxx_supportlib/vendor-modified -Isrc/cxx_supportlib/vendor-modified/libev -Isrc/cxx_supportlib/vendor-copy/libuv/include -D_REENTRANT -I/usr/local/include -Wall -Wextra -Wno-unused-parameter -Wno-parentheses -Wpointer-arith -Wwrite-strings -Wno-long-long -Wno-missing-field-initializers -feliminate-unused-debug-symbols -feliminate-unused-debug-types -fvisibility=hidden -DVISIBILITY_ATTRIBUTE_SUPPORTED -Wno-attributes -ggdb -DHAS_ALLOCA_H -DHAVE_ACCEPT4 -DHAS_SFENCE -DHAS_LFENCE -DPASSENGER_DEBUG -DBOOST_DISABLE_ASSERTS -std=gnu++11 -Wno-unused-local-typedefs -DHASH_NAMESPACE="__gnu_cxx" -DHASH_MAP_HEADER="<hash_map>" -DHASH_MAP_CLASS="hash_map" -DHASH_FUN_H="<hash_fun.h>" -c src/agent/Core/CoreMain.cpp
virtual memory exhausted: Cannot allocate memory
rake aborted!
Command failed with status (1): [c++ -o buildout/support-binaries/CoreMain.o  -Isrc/agent -Isrc/cxx_supportlib -Isrc/cxx_supportlib/vendor-copy -Isrc/cxx_supportlib/vendor-modified -Isrc/cxx_supportlib/vendor-modified/libev -Isrc/cxx_supportlib/vendor-copy/libuv/include -D_REENTRANT -I/usr/local/include -Wall -Wextra -Wno-unused-parameter -Wno-parentheses -Wpointer-arith -Wwrite-strings -Wno-long-long -Wno-missing-field-initializers -feliminate-unused-debug-symbols -feliminate-unused-debug-types -fvisibility=hidden -DVISIBILITY_ATTRIBUTE_SUPPORTED -Wno-attributes -ggdb -DHAS_ALLOCA_H -DHAVE_ACCEPT4 -DHAS_SFENCE -DHAS_LFENCE -DPASSENGER_DEBUG -DBOOST_DISABLE_ASSERTS -std=gnu++11 -Wno-unused-local-typedefs -DHASH_NAMESPACE="__gnu_cxx" -DHASH_MAP_HEADER="<hash_map>" -DHASH_MAP_CLASS="hash_map" -DHASH_FUN_H="<hash_fun.h>" -c src/agent/Core/CoreMain.cpp]
/usr/local/rvm/gems/ruby-2.3.3/gems/passenger-5.1.2/build/support/cplusplus.rb:52:in `run_compiler'
/usr/local/rvm/gems/ruby-2.3.3/gems/passenger-5.1.2/build/support/cplusplus.rb:102:in `compile_cxx'
/usr/local/rvm/gems/ruby-2.3.3/gems/passenger-5.1.2/build/support/cplusplus.rb:160:in `block in define_cxx_object_compilation_task'
Tasks: TOP => apache2 => buildout/support-binaries/PassengerAgent => buildout/support-binaries/CoreMain.o
(See full trace by running task with --trace)

我尝试过在具有高达 8GB RAM 的服务器上。我在/var/log/messages 中没有得到任何 OOM,所以我认为内存不是问题所在。

知道为什么我只从exec()运行时会出现此错误吗?

我终于找到了问题。我的 PHP 脚本作为 WHM (cPanel) 插件运行,似乎 WHM 在运行脚本之前设置了一个限制。由于脚本以 root 身份运行,因此我只需要在运行 bash 之前修改 ulimit。

exec("ulimit -m unlimited -v unlimited; bash myscript.sh");