由于购买的阿里云 CentOS 配置太低(1CPU 1G)导致在编译安装 PHP (执行 make )时出现如下错误

virtual memory exhausted: Cannot allocate memory
make: *** [ext/fileinfo/libmagic/apprentice.lo] Error 1

解决办法就是使用 swap 内存
使用命令 free -m 查看当前 swap 内存大小,下面的结果表示没有启用 swap 内存。

free -m
              total        used        free      shared  buff/cache   available
Mem:            990         607         288           0          94         265
Swap:             0           0           0

创建 2G 的虚拟内存文件 /opt/swap

dd if=/dev/zero of=/opt/swap bs=1024 count=2048000

mkswap /opt/swap

swapon /opt/swap

再次查询 swaq 内存

free -m
              total        used        free      shared  buff/cache   available
Mem:            990         609          78           0         302         244
Swap:          1999           0        1999

2G 的虚拟内存启用完成!

标签: CentOS, PHP