OPcache improves PHP performance by storing precompiled script bytecode in shared memory, thereby removing the need for PHP to load and parse scripts on each request. This guide describe its installation on Debian.

This howto is tested on:

  • Debian 10.0 Buster

Requirements

This howto requires:

Installation

Detect if sudo is available (“command” is used if not):

cmdProxy='command'
command type -f 'sudo' &>'/dev/null' && cmdProxy='sudo'

Detect the version of PHP installed on the system:

phpVersion="$(command realpath '/usr/bin/php' | cut -c 13-)"

Install the software:

${cmdProxy} apt-get install "php${phpVersion}-opcache"

Enable APCu and set its memory cache size:

${cmdProxy} php-tools --add-mod --mod-priority=50 --mod-name='opcache-settings' \
    --mod-settings="; OPCache settings
; priority=50
opcache.enable = 1

; allow opcache use for PHP cli.
opcache.enable_cli = 1

; Memory cache size.
; recommended value is 2.5 * PHP total code base size (across sites).
opcache.memory_consumption = 64M

; Max cached files
; recommended value is to count the total number of PHP files on server:
; # command find / -type f -name "*.php" | command wc -l
; another possibility is to set a very large value (up to 100 000):
opcache.max_accelerated_files = 10000

; check for PHP files timestamps changes (I/O intensive).
opcache.validate_timestamps = 1

; revalidate timestamps validity every X seconds.
opcache.revalidate_freq = 300

; revalidate cache by checking original file contents (I/O very intensive):
opcache.revalidate_path = 0

; maximum wasted memory before opcache restart (default to 5).
; opcache.max_wasted_percentage = 25"

Reload the configuration:

${cmdProxy} php-tools --reload

Thanks

Categories: PHP

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.