執行 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
eAccelerator(舊版 PHP 加速器)啟動時申請 64MB (67108864 bytes) 共享記憶體,但 Linux 核心參數 kernel.shmmax 限制單次共享記憶體段最大值預設僅 32MB (33554432 bytes),導致分配失敗,進而使 PHP-FPM 啟動中止。
kernel.shmmax/etc/sysctl.confkernel.shmmax = 134217728 (128MB,建議大於 eAccelerator 需求)sysctl -pecho 134217728 > /proc/sys/kernel/shmmax
sysctl kernel.shmmax
# 應輸出:kernel.shmmax = 134217728
[root@abc lnmp]# vi /etc/sysctl.conf
# ... 在檔案末尾加入 ...
kernel.shmmax = 134217728
[root@abc lnmp]# sysctl -p
kernel.shmmax = 134217728
[root@abc lnmp]# service php-fpm start
Starting php-fpm done
[root@abc lnmp]# /usr/bin/memcached start
# ... 其他服務啟動 ...
| 參數 | 說明 | 建議值 |
|---|---|---|
kernel.shmmax | 單一共享記憶體段最大值 (bytes) | 134217728 (128MB) 或更大 |
kernel.shmall | 系統總共享記憶體頁數 (pages,通常 4KB/頁) | shmmax / 4096 以上 |
kernel.shmmni | 全系統共享記憶體段最大數量 | 4096 |
可一次性寫入 /etc/sysctl.conf:
kernel.shmmax = 134217728
kernel.shmall = 32768
kernel.shmmni = 4096
eAccelerator 早已停止維護(最後更新 2012 年),PHP 5.5+ 內建 Zend OPcache,效能更優、穩定度更高、無共享記憶體配置困擾。強烈建議升級 PHP 並啟用 OPcache:
; php.ini
zend_extension=opcache.so
opcache.enable=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=10000
opcache.revalidate_freq=60
opcache.fast_shutdown=1
PHP-FPM 啟動失敗提示 Could not allocate ... bytes 時,核心原因是 kernel.shmmax 過小。透過 sysctl.conf 永久調大該參數並 sysctl -p 套用即可解決。長遠來看,應規劃遷移至 PHP 7/8 + OPcache,徹底消除此類舊式擴展相容性問題。