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