George McKinney Adventures in Software Development

November 30, 2020

Disabling Zend OPcache

Filed under: Laminas,PHP,Zend Framework — georgemck @ 8:14 am

I ran across the need to disable Zend OPcache when working with Laminas API Tools in PHP, which was formerly called Zend Framework’s APIgility. In addition to requiring PHP Unit, you have to disable Zend OPcache to create a working API. Zend OPcache is bundled with many PHP installations to improve performance. In development on our local computers, we need to turn this off if we want this tool to work!

On your running server, figure out if it is on. You can see this in phpinfo() command. Create file name phpinfo.php and make sure it has correct permissions to run
<?php
phpinfo();
?>
Start your Development Server with built-in PHP server on Port 8080. Then retrieving the php settings information, phpinfo.php. Do not put this information on production server!
php server started view phpinfo
Here, the phpinfo() command shows the loaded configuration file location. /etc/php/7.4/cli/php.ini
phpinfo loaded showing php.ini file
Zend OPcache is enabled.
phpinfo shows zend opcache is ON in localhost

Check the php.ini. It looks off, but obviously it is actually enabled.

phpinfo zend opcache is ON in code editor
Disabling the Zend OPcache in the php.ini configuration file.  If the setting is on, then open the php.ini file and add
[opcache]
; Determines if Zend OPCache is enabled
;opcache.enable=1
opcache.enable=0
turning zend opcache OFF using code editor
Then restart your server application which created the website. This could be apache2, php -S, or apache monitor, whatever.
php server started view phpinfo
Zend OPcache has now been disabled
phpinfo showing zend opcache is OFF in localhost
Enjoy.
###
NOTE ABOUT OPCACHE from Laminas API Tools page

Disable all opcode caches when running the admin!

The admin cannot and will not run correctly when an opcode cache, such as APC or OpCache, is enabled. API Tools does not use a database to store configuration; instead, it uses PHP configuration files. Opcode caches will cache these files on first load, leading to inconsistencies as you write to them, and will typically lead to a state where the admin API and code become unusable.

The admin is a development tool, and intended for use a development environment. As such, you should likely disable opcode caching, regardless.

When you are ready to deploy your API to production, however, you can disable development mode, thus disabling the admin interface, and safely run an opcode cache again. Doing so is recommended for production due to the tremendous performance benefits opcode caches provide.

Powered by WordPress