简体   繁体   English

如何确定 PHP OPcache 是否启用?

[英]How to determine if PHP OPcache is enabled or not?

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 ).我正在尝试在我的服务器上启用 opCache( ubuntu 12.04 LTS ,运行apache 2.4.7PHP 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.在开始做任何事情之前,我阅读了这篇高度相关的帖子,它告诉我 opCache 在默认情况下是禁用的,我必须手动启用它。

I went into php.ini and found that there is no text with opcache.so , also everything related to opcache is commented out.我进入 php.ini 发现 opcache.so 没有文本,与opcache.so相关的所有内容也被注释掉了。 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();但是当我咨询phpinfo(); it shows me the following (and based on my understanding it tells me that opCache is already used).它向我展示了以下内容(根据我的理解,它告诉我 opCache 已被使用)。

在此处输入图像描述

So is my opcache enabled and used and how can I see/verify it?那么我的 opcache 是否已启用和使用,我如何查看/验证它?

Have faith in your phpinfo() , you've got the necessary shared module running or it wouldn't be showing up.相信你的phpinfo() ,你已经运行了必要的共享模块,否则它不会出现。

Also, your opcache is indeed enabled, but only for web, not cli.此外,您的 opcache 确实已启用,但仅适用于 Web,不适用于 cli。 The default for the library is enabled for web so , to disable uncomment the line starting with a semicolon like this:该库的默认值是为 web so 启用的,以禁用取消注释以分号开头的行,如下所示:

 opcache.enable=0 

As noted, for command line php use, the default is disabled, to enable it, uncomment and set to 1如前所述,对于命令行 php 使用,默认是禁用的,要启用它,取消注释并设置为 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以下是所有运行时参数的列表以及供进一步参考的默认值: 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.更新:截至 2020 年,该库可能不再维护,请参阅下面的评论以了解其他可能的选项。

If you want a cool web monitor page for it (like apc.php does for apc) try this: https://github.com/rlerdorf/opcache-status如果你想要一个很酷的网络监控页面(就像 apc.php 对 apc 所做的那样)试试这个: https : //github.com/rlerdorf/opcache-status

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.出于某种原因,关于 PHP v5.5(最初支持它的版本)的 PHP OPcache 有很多相互矛盾的文档,一些网页说它默认启用,而其他网页说它已编译,但默认情况下未启用.

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.仅凭我的记忆,我想我已经看到 OPcache 确实已编译但尚未启用的 PHP 5.5 和 5.6 安装。 This would explain why default php.ini line shows disabled.这将解释为什么默认的php.ini行显示为禁用。 The confusion is likely due to the various ways in which PHP can be compiled.混淆可能是由于编译 PHP 的方式多种多样。

Anyway, for various reasons it seems PHP 7+ pretty much always has OPcache enabled by default, and again, avoiding complex discussions re: compiling.无论如何,出于各种原因,似乎 PHP 7+几乎总是默认启用 OPcache,再次避免复杂的讨论:编译。

All that said, the OP question is really for checking if OPcache is enabled or not (only).综上所述,OP 问题实际上是为了检查是否启用了 OPcache(仅)。

To that end, there is a special function opcache_get_status for this, which can accept arrays too.为此,有一个 特殊的函数opcache_get_status可以接受数组 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.展望未来,这个函数可能会变得更受欢迎,因为它比php.ini更适合解释 OPcache 配置设置,并且可以在各种脚本等中使用。

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参考: https : //www.digitalocean.com/community/questions/enable-php-5-5-opcache-on-ubuntu-14-04-with-nginx-and-php-fpm? comment =25376

As already mentioned, phpinfo() reveals the status of module installation.如前所述, phpinfo()显示模块安装的状态。

However, using OPcache functions you can find more useful statuses and configurations for further debugging.但是,使用OPcache 函数,您可以找到更多有用的状态和配置,以便进一步调试。

opcache_get_configuration(): array|false

The above function returns configuration information about the cache instance.上面的 function 返回缓存实例的配置信息。 You can see for example the set values of opcache.enable and opcache.enable_cli .例如,您可以看到opcache.enableopcache.enable_cli的设置值。

opcache_get_status(): array|false

The above function returns state information about the in-memory cache instance.上述 function 返回有关内存缓存实例的 state 信息。 If the OPCache is disabled, this functions returns false.如果禁用了 OPCache,则此函数返回 false。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM