Categories
Magento

Magento2 Recommended OpCache Configuration

Magento is very demanding on component settings. Everything has to be carefully configured, especially the opCache in PHP. It is opCache that is mainly responsible for PHP’s performance.

So, the most important thing is that opCache must be enabled. After that, it must be carefully tuned.

We’ll assume Magento runs in Kubernetes containers and the services are split into php-fpm and cron. That is, php-fpm processes requests from the frontend, while cron works exclusively from the command line.

Thus, the opCache settings for php-fpm and cron should be different.

For php-fpm service add to php.ini:

opcache.enable_cli=0
opcache.file_cache=""
; Enable huge pages, it is important
opcache.huge_code_pages=On
opcache.memory_consumption=384
opcache.interned_strings_buffer=40
opcache.max_accelerated_files=20000
opcache.validate_timestamps=On
opcache.revalidate_freq=10
opcache.consistency_checks=0
opcache.enable_file_override=On
opcache.save_comments=1
opcache.blacklist_filename="/usr/local/etc/php/opcache-blacklist.txt"
opcache.log_verbosity_level=3
opcache.error_log="/dev/stdout"

For cron the same above, but some overriding settings:

opcache.enable=1
opcache.enable_cli=1
opcache.file_cache="/tmp/php-file-cache"
opcache.file_cache_only=1
opcache.file_cache_consistency_checks=1
opcache.enable_file_override=Off

Pay attention to opcache.huge_code_pages = On. Most Linux now supports Huge Pages, so be sure to enable this option.

With these optimized settings Magento will work smoothly.

To monitor the status of opCache, you can install one of the GUI into a container. I :

https://github.com/amnuts/opcache-gui

Installation for Docker is simple:

RUN mkdir -p /usr/local/var/www/opcache && \
    curl -o /usr/local/var/www/opcache/index.php https://raw.githubusercontent.com/amnuts/opcache-gui/master/index.php

Then just add a location for nginx and you can view and manage the opCache state.

Leave a Reply

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