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
# 編輯系統參數配置
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
[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.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
// 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";