How to fix the «Unable to allocate shared memory segment» PHP issue

Symptoms

Command:

aptitude install php7.0-fpm

Response:

Job for php7.0-fpm.service failed. See ‘systemctl status php7.0-fpm.service’ and ‘journalctl -xn’ for details.

Command:

journalctl -xn

Response:

Fatal Error Unable to allocate shared memory segment of 67108864 bytes: mmap: Cannot allocate memory (12)

Cause

The standard OPcache PHP module is unable to allocate shared memory segment because the opcache.memory_consumption option value is bigger than maximum allowed segment size on the server.

Command:

ipcs -lm

Response:

------ Shared Memory Limits --------
max number of segments = 4096
max seg size (kbytes) = 32768
max total shared memory (kbytes) = 8388608
min seg size (bytes) = 1

Command:

php -i | grep opcache.memory_consumption

Response:

opcache.memory_consumption => 64 => 64

Solution

Edit /etc/sysctl.conf:

kernel.shmmax=200000000
kernel.shmall=50000

Then reboot and test.