49

I am trying to enable opCache on my server (ubuntu 12.04 LTS, running apache 2.4.7 with PHP Version 5.5.10-1+deb.sury.org~precise+1).

Before starting to do anything, I read this highly relevant post which told me that opCache is disabled by default and I have to manually enable it.

I went into php.ini and found that there is no text with opcache.so, also everything related to opcache is commented out. Like this:

[opcache]
; Determines if Zend OPCache is enabled
;opcache.enable=0

; Determines if Zend OPCache is enabled for the CLI version of PHP
;opcache.enable_cli=0

This is ok, because the person told that it is disabled. But when I consult phpinfo(); it shows me the following (and based on my understanding it tells me that opCache is already used).

enter image description here

So is my opcache enabled and used and how can I see/verify it?

Jesse Nickles
  • 1,435
  • 1
  • 17
  • 25
Salvador Dali
  • 214,103
  • 147
  • 703
  • 753
  • did you restart your server before running `phpinfo()` – Victory Mar 31 '14 at 22:00
  • @Victory I just restarted it and the result is the same. (except of the numbers in cache hits 3, cache misses 2). Also Used/Free memory caches scripts/keys. Which actually moves me closer to believe that it is already running. – Salvador Dali Mar 31 '14 at 22:05
  • I have the same issue but my values are not moving.. http://i.imgur.com/JwEaGwu.png any advice? – Revious Sep 29 '16 at 14:05

4 Answers4

51

Have faith in your phpinfo(), you've got the necessary shared module running or it wouldn't be showing up.

Also, your opcache is indeed enabled, but only for web, not cli. The default for the library is enabled for web so , to disable uncomment the line starting with a semicolon like this:

 opcache.enable=0 

As noted, for command line php use, the default is disabled, to enable it, uncomment and set to 1

 opcache.enable_cli=1

Here is a list of all runtime arguments and there default value for further reference: http://www.php.net/manual/en/opcache.configuration.php

UPDATE: As of 2020, this library may be no longer maintained, see comments below for other possible options.

If you want a cool web monitor page for it (like apc.php does for apc) try this: https://github.com/rlerdorf/opcache-status

Ray
  • 40,256
  • 21
  • 101
  • 138
  • Cool, thanks. Apparently I do have to trust phpinfo more than highly upvoted answers. Thanks for confirming this. – Salvador Dali Mar 31 '14 at 22:15
  • 1
    @SalvadorDali The other post you're referring to regarding whether the extension's share library is present and loaded. I think it would be clear to state, the shared module might not be set to load by default php 5.5 build, but this does not mean it's 'disabled', it's not even loaded. Clearly your setup has the shared library for module enabled. Once the module is enabled, you use the config settings I not to turn on and off specific functionality and enable/disable particular features. – Ray Mar 31 '14 at 22:25
  • 1
    for some reason, I think this answer will have a big hit in the next couple years :) – mamdouh alramadan Mar 31 '14 at 22:29
  • I'm trying this on PHP 5.4 but it shows 1 key used. That's probably the one used by opcached.php (opcache-status). Even though a lot of other pages are being requested. I assume opcache works like apc and caches all compiled php files. Maybe the report is wrong as the composer.json does say `php > 5.5` but opcache can be installed on `php > 5.3` maybe I'll try another report – HMR Jun 10 '14 at 14:28
  • The module *is* probably loaded, but not in php.ini, but through `/etc/php5/apache2/conf.d/05-opcache.ini` and by default it is enabled if the module is loaded. – hbogert Oct 17 '15 at 11:05
  • 4
    just wanted to say if you need to check it from your cli you can run php -i | grep opcache – Dr. Chocolate Oct 26 '18 at 00:16
  • 1
    https://github.com/rlerdorf/opcache-status has not been updated in long time. Try https://github.com/amnuts/opcache-gui. Its updated regularly. – Umair Khan Jul 03 '20 at 07:35
  • @UmairKhan Thanks for the update, added an update in the answer. – Ray Jul 04 '20 at 16:13
  • @Ray. Your welcome. I tried to edit the post myself, but the edit suggestion got rejected in favor of commenting the suggestion instead of editing the answer. :-P – Umair Khan Jul 06 '20 at 07:12
  • For the next not enough proficient readers like myself who are wondering how to execute this method from the terminal it is: `php -r 'phpinfo();'` or even better `php --info` – Martin Tovmassian Apr 20 '23 at 14:20
7

For some reason there is a lot of conflicting documentation about PHP OPcache for PHP v5.5 (the version that first supported it), with some web pages saying it is enabled by default and others saying it is compiled, but not enabled, by default.

Going off my memory only, I think I've seen PHP 5.5 and 5.6 installations where OPcache was indeed compiled but not yet enabled. This would explain why default php.ini line shows disabled. The confusion is likely due to the various ways in which PHP can be compiled.

Anyway, for various reasons it seems PHP 7+ pretty much always has OPcache enabled by default, and again, avoiding complex discussions re: compiling.

All that said, the OP question is really for checking if OPcache is enabled or not (only).

To that end, there is a special function opcache_get_status for this, which can accept arrays too. Going forward, this function will probably become more popular as it is more tuned to explaining the OPcache configuration settings than php.ini and can be used in various scripts, etc.

Here's a quick example:

<?php (is_array(opcache_get_status()) ? 'enabled' : 'disabled') ?>

Ref: https://www.digitalocean.com/community/questions/enable-php-5-5-opcache-on-ubuntu-14-04-with-nginx-and-php-fpm?comment=25376

Jesse Nickles
  • 1,435
  • 1
  • 17
  • 25
  • 2
    also note that you may check function_exists( 'opcache_get_status' ) to avoid fatal errors – Ramon Fincken Feb 05 '21 at 15:44
  • I yield different results depending whether I call `echo "starting test\nopcache status:".is_array(opcache_get_status()) ? 'enabled' : 'disabled';` or just `echo is_array(opcache_get_status()) ? 'enabled' : 'disabled';` - this is a bit inconveniant... – clockw0rk Feb 23 '21 at 12:17
2

Inside docker container / any linux:

php -i | grep 'opcache.enable'
# or the entire configuration of opcache:
# php -i | grep 'opcache'
Jan 'splite' K.
  • 1,667
  • 2
  • 27
  • 34
  • This command was the most straightforward to get feedback since I did not know how to execute the `phpinfo()` method suggested by @ray. – Martin Tovmassian Apr 20 '23 at 14:13
1

As already mentioned, phpinfo() reveals the status of module installation.

However, using OPcache functions you can find more useful statuses and configurations for further debugging.

opcache_get_configuration(): array|false

The above function returns configuration information about the cache instance. You can see for example the set values of opcache.enable and opcache.enable_cli.

opcache_get_status(): array|false

The above function returns state information about the in-memory cache instance. If the OPCache is disabled, this functions returns false.

Petro Mäntylä
  • 711
  • 1
  • 7
  • 9