在 CentOS 7 中安装 OPCache 以提高 PHP 性能
PHP 是最流行的应用程序开发编程语言之一,您会在每个网络托管服务器上找到它。最流行的内容管理系统 (CMS) 是用PHP 编写的,例如 WordPress、Drupal 和 Joomla。
PHP 广为人知的众多原因之一是它的默认发行版中有许多扩展,例如 OPcahce。
什么是 Opcache 及其工作原理?
Opcache(在 PHP 5.5.0 中引入)最初称为 Zend Optimizer+,是一个强大的 PHP 扩展,旨在增强 PHP 性能,从而提高整体应用程序的性能表现。它可通过 PECL 作为 PHP 版本 5.2、5.3 和 5.4 的扩展提供。 >。它的工作原理是将预编译的脚本字节码存储在共享内存中,从而消除了 PHP 在每个请求上加载和解析脚本的需要。
在本文中,我们将解释如何在 CentOS 7 中针对特定 PHP 版本安装和配置 OPcache。
在 CentOS 7 中安装 Opcache PHP 扩展
1. 首先在您的系统上安装 EPEL 存储库,然后安装 REMI 存储库,如下所示。
yum update && yum install epel-release
yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
2.接下来,您需要安装yum-utils,这是一个扩展yum默认功能的实用程序集合;它们可以帮助您管理 yum 存储库以及软件包,而无需任何手动配置等。
yum install yum-utils
3.安装yum-utils后,使用yum-config-manager启用Remi存储库作为用于安装不同 PHP 版本和模块的默认存储库。
yum-config-manager --enable remi-php55 #For PHP 5.5
yum-config-manager --enable remi-php56 #For PHP 5.6
yum-config-manager --enable remi-php70 #For PHP 7.0
yum-config-manager --enable remi-php71 #For PHP 7.1
yum-config-manager --enable remi-php72 #For PHP 7.2
4. 现在安装 Opcache 扩展并验证您的 PHP 版本,以确认它已使用以下命令安装了 Opcache 扩展。
yum install php-opcache
php -v
在 CentOS 7 中配置 Opcache PHP 扩展
5. 接下来,通过编辑 /etc/php.d/10-opcache.ini (或 /etc/使用您最喜欢的编辑器编辑 php.d/10-opcache.ini) 文件。
vim /etc/php.d/10-opcache.ini
以下设置应该可以帮助您开始使用OPcache,并且通常建议您进行良好的性能。您可以通过取消注释来启用配置。
opcache.enable_cli=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
opcache.fast_shutdown=1
6.最后,重新启动您的网络服务器以使Opcache开始工作。
systemctl restart nginx
OR
systemctl restart httpd
就这样! Opcache 是一个 PHP 扩展,旨在提高 PHP 性能。在本文中,我们解释了如何在 CentOS 7 中安装和配置 OPcache。如果您有任何疑问,请通过下面的评论表与我们联系。