萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> PHP 程式設計

PHP-FPM 啟動報錯「Could not allocate shared memory」解決方案:調整 kernel.shmmax 參數

PHP-FPM 啟動失敗,錯誤訊息 `eAccelerator: Could not allocate 67108864 bytes, the maximum size the kernel allows is 33554432 bytes`,核心原因是系統共享記憶體上限 (`kernel.shmmax`) 小於 PHP 擴充套件(如 eAccelerator、OPcache、APCu)請求的共享記憶體大小。

錯誤訊息完整內容

[root@abc lnmp]# service php-fpm start
Starting php-fpm eAccelerator: Could not allocate 67108864 bytes, the maximum size the kernel allows is 33554432 bytes. Lower the amount of memory request or increase the limit in /proc/sys/kernel/shmmax.
[04-Dec-2013 19:06:44] NOTICE: PHP message: PHP Warning: [eAccelerator] Can not create shared memory area in Unknown on line 0
[04-Dec-2013 19:06:44] NOTICE: PHP message: PHP Fatal error: Unable to start eAccelerator module in Unknown on line 0
failed

根因分析

  • PHP 擴充套件 (eAccelerator/OPcache) 啟動時申請 64MB 共享記憶體 (`shm_size=64M`)
  • Linux 核心參數 `kernel.shmmax` 預設僅 32MB (33554432 bytes)
  • 申請失敗 → 模組載入失敗 → PHP-FPM 啟動中止

解決方案:永久調整 kernel.shmmax

方法一:修改 /etc/sysctl.conf(永久生效)

# 編輯系統參數配置
vi /etc/sysctl.conf

# 追加或修改(建議設為 256MB 或更大,視記憶體而定)
kernel.shmmax = 268435456  # 256MB = 256 * 1024 * 1024
kernel.shmall = 2097152    # 總共享頁數 (shmmax / PAGE_SIZE,通常 PAGE_SIZE=4KB)

方法二:臨時生效(重啟失效)

# 立即生效
sysctl -w kernel.shmmax=268435456
sysctl -w kernel.shmall=2097152

# 或直接寫入 procfs
echo 268435456 > /proc/sys/kernel/shmmax
echo 2097152 > /proc/sys/kernel/shmall

方法三:套用配置並驗證

# 套用 sysctl.conf
sysctl -p

# 驗證
sysctl kernel.shmmax kernel.shmall
# 輸出:
# kernel.shmmax = 268435456
# kernel.shmall = 2097152

重啟 PHP-FPM 驗證

[root@abc lnmp]# service php-fpm start
Starting php-fpm done
[root@abc lnmp]# /usr/bin/memcached start

相關核心參數說明

參數含義預設值建議值
kernel.shmmax單一共享記憶體段最大位元組數33554432 (32MB)268435456 (256MB) 或更大
kernel.shmall系統總共享記憶體頁數 (頁=4KB)2097152 (8GB)shmmax/4096 * 段數
kernel.shmmni全系統最大共享記憶體段數4096通常無需調整

PHP 擴充套件共享記憶體配置對照

; php.ini / zend_extension=opcache.so
opcache.memory_consumption=128        ; 128MB,需 < kernel.shmmax
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=10000

; eAccelerator (舊專案)
eaccelerator.shm_size = "64"          ; 64MB

; APCu
apc.shm_size = 64M

現代環境注意事項

  • PHP 7.0+ 內建 OPcache:取代 eAccelerator/APC,配置 `opcache.memory_consumption` 即可
  • Docker 容器:需在宿主機調整 `sysctl`,或容器啟動時 `--sysctl kernel.shmmax=268435456`
  • systemd 服務:可在 `[Service]` 區塊加入 `ExecStartPre=/sbin/sysctl -w kernel.shmmax=268435456`
  • 檢查當前使用量:`ipcs -m` 查看共享記憶體段

快速檢查腳本

<?php
// check_shm.php
$shmmax = (int)@file_get_contents('/proc/sys/kernel/shmmax');
$opcache = ini_get('opcache.memory_consumption') * 1024 * 1024;
$apcu = ini_get('apc.shm_size');

echo "kernel.shmmax: " . number_format($shmmax) . " bytes (" . round($shmmax/1024/1024, 1) . " MB)\n";
echo "opcache.memory_consumption: " . number_format($opcache) . " bytes\n";
if ($apcu) echo "apc.shm_size: $apcu\n";

if ($opcache > $shmmax) {
    echo "⚠️ 警告: OPcache 申請記憶體 ($opcache) 超過 kernel.shmmax ($shmmax)\n";
    exit(1);
}
echo "✅ 共享記憶體配置正常\n";
copyright © 萬盛學電腦網 all rights reserved