Opcache

From QualityBox Wiki
Jump to navigation Jump to search

Is your Zend Opcache out of memory? Do you see "Warning Interned string buffer overflow" in Apache's error log? We've tweaked the Opcache settings for better performance.

Meza installs php56u-opcache in /opt/meza/src/roles/apache-php/tasks/php.yml and sets opcache.interned_strings_buffer=4 in /opt/meza/src/roles/apache-php/templates/php.ini.j2

In raising the setting we also optimized the PHP opcache according to the Scaling PHP book recommendations.

Also, in order to effectively monitor our actual usage of the Opcache, I installed Rasmus Ledorf's (creator of PHP): opcache-status for instances where the deploy mode is 'development'. Note: There are a few (simple+good) pull requests that have sat idle for a long time, so it might be better to use a fork, or create a fork instead of using Rasmus' repo.

Opcache status does reveal document root and physical script path information so it's best to keep it 'private' (hence why we only install it in development). I've left out details on installation (see example). Apache configuration to restrict access to opcache.php.

Opcache.max_accelerated_files should be larger than the number of files in your project. We have ~7455, [1] and the next prime number is 7963. So let's set it to 7963 (currently 20,000)

One big change recommended by the book is to never re-validate file timestamps in production...it's wasting valuable time with stat calls. Instead, the book says re-validate in the dev environment, and whenever you push to production, restart your webserver.

"Yes, this is a pain in the ass, but you should use it. Why? While you're updating or deploying code, new code files can get mixed with old ones— the results are unknown. It's unsafe as hell."

TL;DR-[edit | edit source]

in php.ini

opcache.revalidate_freq=0
opcache.validate_timestamps=0 (set to 1 in dev environments)
opcache.max_accelerated_files=7963
opcache.memory_consumption=256 (unchanged from current)
opcache.interned_strings_buffer=16
opcache.fast_shutdown=1
  1. cd /opt/htdocs
    find . -type f -print | grep php | wc -l
    7455