RHEL / CentOS 安装 OPcache 提升 PHP 效能


OPcache 在 PHP 5.5 (默认没有开启)开始内建在 PHP, 前身是 Zend Optimizer, PHP 5.2, 5.3 及 5.4 则要透过安装 PECL extension 安装。它的作将已经编译的 PHP Script 储存在内存, 当下次存取 PHP Script 时不用重新编译, 这样便可以提高 PHP 的效能。

以下是在 CentOS 7 安装指定 PHP 版本及设定 OPcache 的方法。

1. 在开始前先安装 EPEL Repository 及 REMI Repository:

# yum update
# yum install epel-release
# yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm

2. 安装 yum-utils, 这是开启 yum 的功能, 下面选择 PHP 版本时需要用上:

# yum install yum-utils

3. 安装好 yum-utils 后, 可以用 yum-config-manager 设定 Remi Repository 成默认的 Repository, 这样可以安装较新版本的 PHP (CentOS 7 内建的 PHP 版本是 5.4), 根据要安装的 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. 现在可以用 yum 安装 OPcache:

# yum install php-opcache

如果只需要使用 OPcache 的默认设定, 则可以直接重新启动网页服务器, 如果需要设定 OPcache, 可以开启 OPcache 的设定档 /etc/php.d/10-opcache.ini

# vi /etc/php.d/10-opcache.ini

最后可以重新启动网页服务器:

# systemctl restart httpd

# systemctl restart nginx

Leave a Reply