Skip to main content

Newsletter

ads

How to increase your PHP opcache in PHP 7

First check how much memory you have free on average. You can take a snapshot using the free command. 

:/etc/php/7.4/fpm# free -m
              total        used        free      shared  buff/cache   available
Mem:           7961         322         667         161        6972        7182
Swap:             0           0           0

The line you will want to edit in your php.ini is opcache_memory.consumption

:/etc/php/7.4/fpm# grep "opcache.memory_consumption" php.ini
;opcache.memory_consumption=128

Using you favorite editor change the value from the default to whatever you need.

:/etc/php/7.4/fpm# nano php.ini

I prefer to keep the original value commented out and add a new line

; The OPcache shared memory storage size.
;opcache.memory_consumption=128
opcache.memory_consumption=256

Restart PHP

:/etc/php/7.4/fpm# service php7.4-fpm restart

Check memory again and you can see that you have less free memory due to it being allocated to the PHP opcache 

root@ubuntu-s-3vcpu-1gb-nyc1-01:/etc/php/7.4/fpm# free -m
              total        used        free      shared  buff/cache   available
Mem:           7961         351         616         159        6993        7155
Swap:             0           0           0