eaccelerator.shm_size设置过大导致php-fpm启动失败

前几天遇到了这样的问题:在一台Godaddy Linux环境的VPS中,我编译安装了nginx, php,mysql。由于网站比较多,我选择安装了eaccelerator来缓存php以提供响应时间,eaccelerator编译完成并通过模块方式载入到php中,一切运行正常。但是通过控制eaccelerator_control.php的控制脚本看到了共享内存只有32M,于是设置eaccelerator.shm_size=”128”,重启之后发现php-fpm启动失败,而且没有任何提示。
找了很多资料,最后在github.com上找到了如下答案:

This setting will allow you to control the amount of shared memory eAccelerator should allocate to cache PHP scripts. The number sets the amount of memory in megabytes. Setting this value to 0 will use the default size.

On Linux the maximum amount of memory a process can allocate is limited by the number set in /proc/sys/kernel/shmmax. Allocating more than this value will result in eAccelerator failing to initialize. The size in this file is given in bytes. You can raise this amount with:

echo value > /proc/sys/kernel/shmmax

Where value is the size in bytes you want to use. This value is reset to the default value every time you reboot, but you can raise it permanently by adding the amount you need in /etc/sysctl.conf. This is done by adding:

kernel.shmmax = value

大致意思是eaccelerator.shm_siz控制php缓存使用共享内存的的大小,但是在linux下,这个设置是无效的(受内核限制),如果需要更改的话,必须把系统的共享内存变大,有两种方法均可以,第一种是临时的方法,系统重启的话是失效:echo value > /proc/sys/kernel/shmmax。第二种是永久的方法,在/etc/sysctl.conf文件中增加一行kernel.shmmax = value。

把这个设置完成之后,在重启php-fpm发现一切运行正常。

附:
共享内存(Shared Memory)指在多处理器的计算机系统中,可以被不同中央处理器访问的大容量内存。由于多个CPU需要快速访问存储器,这样就要对存储器进行缓存。由于其他处理器可能也要存取,任一缓存数据更新后,共享内存就需要立即更新,否则不同处理器可能用到不同的数据。

发表回复