How do I correctly enable PHP 5.5's OPCache in Ubuntu 14.04?

I'm setting up a local server to test configurations; it's a clean 14.04 LAMP server, as selected during OS install.

If I understand correctly, OPCache comes with PHP 5.5 (php -v confirms this) but in a disabled state. I've followed the Internet's suggestions to enable it, however I'm receiving mixed messages about whether or not this has been successful.

My php.ini has

zend_extension=/usr/lib/php5/20121212/opcache.so
opcache.enable=1
opcache.enable_cli=1

yet php -i, after restarting the machine, is telling me

Opcode Caching => Disabled
Optimization => Disabled
Startup Failed => Opcode Caching is disabled for CLI

opcache.enable => On => On
opcache.enable_cli => Off => Off

i.e. it's apparently enabled, and simultaneously disabled.

Is enabling OPCache possible with the 14.04 repository PHP package, or do I need to compile PHP from source?


Solution 1:

For some reason, setting fast_shutdown=1 was causing problems and preventing OPCache from starting. Once this was set to 0, OPCache is successfully up and running (possibly related to this bug?).

All that is needed therefore to enable OPCache is to set

; Determines if Zend OPCache is enabled
opcache.enable=1

in Apache's php.ini followed by a server restart

service apache2 restart

Side note: php -i tends to use a different php.ini from Apache (e.g. /etc/php5/cli/php.ini), hence the confusing discrepancy in the original question.

Solution 2:

I faced this exact same problem through the day and am glad I solved it in the below manner. Although, the above reply is marked as an answer, it is clear from the comments to that answer that it doesn't completely answer the question.

The correct file to enable is in

/etc/php5/cli/conf.d 

Then enter the following in there

zend_extension=opcache.so
opcache.memory_consumption=128
opcache.max_accelerated_files=2500
opcache.interned_strings_buffer=8
opcache.revalidate_freq=60
opcache.fast_shutdown=1

Dont forget to restart apache

Use a tool like Opcache status to check if everything is fine and it will help you with areas to fine tune.

Hope that helps someone.

Solution 3:

I ran into this when using php-fpm. I discovered in /etc/php5/fpm/conf.d that there was a symlink 05-opcache.ini to ../../mods-available/opcache.ini and one 20-opcache.ini pointing to the same place. That was causing a conflict.

I removed one (the 20 since that had a later modification date), restarted php-fpm, and everything came back.