随笔 - 172  文章 - 52  评论 - 6  阅读 - 17万

安装fileinfo | 编译安装PHP组件出现错误提示内存不足:virtual memory exhausted Cannot allocate memory

1 去官方去下载最新的PHP7.0(这一步是关键 否则你可能因为版本不匹配遇到各种未知错误)

http://sg2.php.net/get/php-7.0.23.tar.gz/from/a/mirror

下载完解压

解压后的ext目录里面有fileinfo文件夹 将这个文件夹上传到Linux服务器上 ,可以上传到任意的一个目录

 

然后在linux上进入fileinfo目录 顺序执行如下命令

 

phpize (如果提示命令不存在 就输入完整的路径名 不知道在哪里就用whereis phpize)


./configure --with-php-config=/usr/local/php/bin/php-config (注意等号后面的为你自己的php-config的地址 php-config是一个文件)

make && make install

修改php的配置文件php.ini   在里面任意位置添加下面这一行
extension=fileinfo.so

 

编译安装PHP组件出现错误提示内存不足:virtual memory exhausted Cannot allocate memory

 

背景

前面一篇我们介绍了宝塔面板的PHP默认不安装fileinfo组件,需要手动编译安装。在php5.6上是没有出现问题,但是在编译php7.1的版本的make && make install这一步出现错误,提示:

virtual memory exhausted: Cannot allocate memory
Makefile:197: recipe for target 'libmagic/apprentice.lo' failed

下面是make编译后的详细信息:

/www/server/php/71/src/ext/fileinfo# make && make install /bin/sh /www/server/php/71/src/ext/fileinfo/libtool --mode=compile cc -I/www/server/php/71/src/ext/fileinfo/libmagic -I. -I/www/server/php/71/src/ext/fileinfo -DPHP_ATOM_INC -I/www/server/php/71/src/ext/fileinfo/include -I/www/server/php/71/src/ext/fileinfo/main -I/www/server/php/71/src/ext/fileinfo -I/www/server/php/71/include/php -I/www/server/php/71/include/php/main -I/www/server/php/71/include/php/TSRM -I/www/server/php/71/include/php/Zend -I/www/server/php/71/include/php/ext -I/www/server/php/71/include/php/ext/date/lib  -DHAVE_CONFIG_H  -g -O2   -c /www/server/php/71/src/ext/fileinfo/libmagic/apprentice.c -o libmagic/apprentice.lo
cc -I/www/server/php/71/src/ext/fileinfo/libmagic -I. -I/www/server/php/71/src/ext/fileinfo -DPHP_ATOM_INC -I/www/server/php/71/src/ext/fileinfo/include -I/www/server/php/71/src/ext/fileinfo/main -I/www/server/php/71/src/ext/fileinfo -I/www/server/php/71/include/php -I/www/server/php/71/include/php/main -I/www/server/php/71/include/php/TSRM -I/www/server/php/71/include/php/Zend -I/www/server/php/71/include/php/ext -I/www/server/php/71/include/php/ext/date/lib -DHAVE_CONFIG_H -g -O2 -c /www/server/php/71/src/ext/fileinfo/libmagic/apprentice.c  -fPIC -DPIC -o libmagic/.libs/apprentice.o
virtual memory exhausted: Cannot allocate memory
Makefile:197: recipe for target 'libmagic/apprentice.lo' failed
make: *** [libmagic/apprentice.lo] Error 1

原因

这个意思是内存不足,无法完成编译。

本次使用的是AWS t2.micro配置的EC2实例,实际上其内存有1GB,但是还是出现本次的错误,应该是同时运行的其他程序导致内存不足。

解决办法

既然这样,物理内存不足我们没办法,但是可以通过自行增加虚拟内存的方法来解决。

通过free -m来查看下内存使用状况

# free -m
               total        used        free      shared  buff/cache   available
Mem:            990         466         447           3          76         401
Swap:             0           0           0

创建一个目录/opt/images/

你可以自己定路径

# mkdir /opt/images/
# rm -rf /opt/images/swap

创建一个2GB大小的文件

# dd if=/dev/zero of=/opt/images/swap bs=1024 count=2048000
2048000+0 records in
2048000+0 records out
2097152000 bytes (2.1 GB, 2.0 GiB) copied, 30.3635 s, 69.1 MB/s

把创建的文件变成SWAP分区

# mkswap /opt/images/swap
Setting up swapspace version 1, size = 2 GiB (2097147904 bytes)
no label, UUID=dd2fa2db-f8bd-41db-9e1a-5d9257924c6f

启用这个SWAP文件

# swapon /opt/images/swap
swapon: /opt/images/swap: insecure permissions 0644, 0600 suggested.

复制代码

看看SWAP是否生效



#
free -m total used free shared buff/cache available Mem: 990 467 64 3 458 356 Swap: 1999 0 1999
复制代码

可以看到的确有2GB的SWAP内存

然后回到原来的作业

使用cd -回到原来的/www/server/php/71/src/ext/fileinfo目录

继续编译fileinfo

# make && make install

执行成功

See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
Build complete.
Don't forget to run 'make test'.
Installing shared extensions:     /www/server/php/71/lib/php/extensions/no-debug-non-zts-20160303/

完成后关闭SWAP

# swapoff swap
# rm -f /opt/images/swap

以后再出现内存不足可以通过增加SWAP虚拟内存来解决~

posted on   共感的艺术  阅读(595)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示