Rocky/Alma/CentOS 9|8 Üzerine PHP OPcache Kurulumu


OPcache, derlenmiş betik bayt kodlarını paylaşılan bellekte saklayarak PHP’nin performansını artırmayı amaçlar. PHP betiklerini bellekte önbelleğe alarak, PHP’nin her aldığı istekte betikleri yükleyip çözümlemesi gereksinimini ortadan kaldırır. PHP ile oluşturulmuş web siteleri veya web uygulamaları barındırırken, OPcache kullanmak yanıt verme hızını ve genel yükleme sürelerini önemli ölçüde artıracaktır.

PHP OPcache böyle çalışır:

  1. Betik derleme: OPcache, istenen bir betiği bytecode'a derler.
  2. Bytecode önbellekleme: Derlendikten sonra, bytecode daha hızlı erişim için paylaşılan bellekte saklanır.
  3. Sonraki isteklerin yürütülmesi: Aynı betik için bir istek alındığında, önbelleğe alınmış baytkod kullanılır; böylece derleme adımı atlanır ve yürütme daha hızlı gerçekleşir.

Özetle, OPcache kullanarak PHP betiklerinin daha hızlı çalışmasını, CPU ve bellek üzerinde daha az yük oluşmasını ve daha fazla eşzamanlı isteği işleyebileceği için daha iyi sistem ölçeklenebilirliğini deneyimleyeceksiniz.

O zaman Rocky Linux/AlmaLinux ve CentOS Stream 9|8 Linux sisteminde PHP OPcache nasıl kurulur ve yapılandırılır? Yapılışını öğrenmek için takip edin.

REMI'den PHP sürümüne geç

OPcache'i yalnızca PHP yüklü bir sistemde etkinleştirebiliriz. Bahsedilen sistemler AppStream depolarında birden fazla PHP sürümü sunar. Ancak Opcache, REMI depolarında sağlanmaktadır.

PHP'nin kurulu olup olmadığını kontrol etmek için şu komutu çalıştırın:

$ php --version
PHP 8.2.13 (cli) (built: Nov 21 2023 09:55:59) (NTS gcc x86_64)
Copyright (c) The PHP Group
Zend Engine v4.2.13, Copyright (c) Zend Technologies

Sistemimden sürümün 8.2 olduğunu doğrulayabiliriz. Bu nedenle, PHP modülünü sıfırlayabiliriz.

sudo dnf module reset php -y

Sonraki adım olarak Remi’nin RPM deposunu kuruyoruz:

  • Kurumsal Linux 9 – (Rocky/Alma/CentOS Stream 9)
sudo dnf -y install https://rpms.remirepo.net/enterprise/remi-release-9.rpm
  • Kurumsal Linux 8 – (Rocky/Alma/CentOS Stream 8)
sudo dnf -y install https://rpms.remirepo.net/enterprise/remi-release-8.rpm

Yükleyici, bağımlılık olarak EPEL deposunu otomatik olarak ayarlayacaktır. REMI'nin çalıştığını doğrulamak için şunu çalıştırın:

$ sudo dnf -y repolist
repo id                                                                                 repo name
appstream                                                                               Rocky Linux 8 - AppStream
baseos                                                                                  Rocky Linux 8 - BaseOS
epel                                                                                    Extra Packages for Enterprise Linux 8 - x86_64
extras                                                                                  Rocky Linux 8 - Extras
remi-modular                                                                            Remi's Modular repository for Enterprise Linux 8 - x86_64
remi-safe                                                                               Safe Remi's RPM repository for Enterprise Linux 8 - x86_64

Tüm PHP modülleri, aşağıdaki komutu çalıştırarak kontrol edilebilir:

 dnf module list php

İşte örnek çıktı:

Rocky Linux 8 - AppStream
Name                                        Stream                                              Profiles                                                          Summary
php                                         7.2 [d][x]                                          common [d], devel, minimal                                        PHP scripting language
php                                         7.3 [x]                                             common [d], devel, minimal                                        PHP scripting language
php                                         7.4 [x]                                             common [d], devel, minimal                                        PHP scripting language
php                                         8.0 [x]                                             common [d], devel, minimal                                        PHP scripting language
php                                         8.2 [x]                                             common [d], devel, minimal                                        PHP scripting language

Remi's Modular repository for Enterprise Linux 8 - x86_64
Name                                        Stream                                              Profiles                                                          Summary
php                                         remi-7.2 [x]                                        common [d], devel, minimal                                        PHP scripting language
php                                         remi-7.3 [x]                                        common [d], devel, minimal                                        PHP scripting language
php                                         remi-7.4 [x]                                        common [d], devel, minimal                                        PHP scripting language
php                                         remi-8.0 [x]                                        common [d], devel, minimal                                        PHP scripting language
php                                         remi-8.1 [x]                                        common [d], devel, minimal                                        PHP scripting language
php                                         remi-8.2 [x]                                        common [d], devel, minimal                                        PHP scripting language
php                                         remi-8.3 [x]                                        common [d], devel, minimal                                        PHP scripting language

Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled

Şimdi, REMI'den belirli PHP modülünü etkinleştirin. Daha önceki çıktıya göre yüklü PHP sürümü 8.2 idi, bu da php:remi-8.2 modülünü etkinleştirmemiz gerektiği anlamına geliyor.

sudo dnf module install php:remi-8.2 -y

Çoğu durumda, yüklü PHP sürümünüzü yükseltecek ve paketleri kaldırmayacaktır.

Dependencies resolved.
==============================================================================================================================================================================================================================
 Package                                               Architecture                                   Version                                                      Repository                                            Size
==============================================================================================================================================================================================================================
Upgrading:
 php-cli                                               x86_64                                         8.2.21-1.el8.remi                                            remi-modular                                         5.5 M
 php-common                                            x86_64                                         8.2.21-1.el8.remi                                            remi-modular                                         1.3 M
 php-fpm                                               x86_64                                         8.2.21-1.el8.remi                                            remi-modular                                         1.9 M
 php-mbstring                                          x86_64                                         8.2.21-1.el8.remi                                            remi-modular                                         583 k
 php-xml                                               x86_64                                         8.2.21-1.el8.remi                                            remi-modular                                         264 k
Installing dependencies:
 oniguruma5php                                         x86_64                                         6.9.9-1.el8.remi                                             remi-safe                                            213 k
Installing module profiles:
 php/common
Enabling module streams:
 php                                                                                                  remi-8.2

Transaction Summary
==============================================================================================================================================================================================================================
Install  1 Package
Upgrade  5 Packages

Total download size: 9.7 M
Is this ok [y/N]: y

PHP OPcache Kurulumu

REMI’nin RPM deposu yapılandırıldığında, OPcache’in mevcut sürümlerini arayabiliriz.

$ sudo dnf search opcache
Last metadata expiration check: 0:07:55 ago on Sun 28 Jul 2024 10:12:13 PM UTC.
============================================================================================== Name & Summary Matched: opcache ===============================================================================================
php-opcache.x86_64 : The Zend OPcache
php56-php-opcache.x86_64 : The Zend OPcache
php70-php-opcache.x86_64 : The Zend OPcache
php71-php-opcache.x86_64 : The Zend OPcache
php72-php-opcache.x86_64 : The Zend OPcache
php73-php-opcache.x86_64 : The Zend OPcache
php74-php-opcache.x86_64 : The Zend OPcache
php80-php-opcache.x86_64 : The Zend OPcache
php81-php-opcache.x86_64 : The Zend OPcache
php82-php-opcache.x86_64 : The Zend OPcache
php83-php-opcache.x86_64 : The Zend OPcache
php84-php-opcache.x86_64 : The Zend OPcache

Bu örnekte, PHP sürümü 8.2'ye özel olan php82-php-opcache kuruyoruz. Kurulu PHP sürümünüze uygun bir sürüme geçiş yapın.

sudo dnf install php82-php-opcache

Yüklü paket hakkında daha fazla bilgi rpm -qi komutları kullanılarak sorgulanabilir:

$ rpm -qi php82-php-opcache
Name        : php82-php-opcache
Version     : 8.2.21
Release     : 1.el8.remi
Architecture: x86_64
Install Date: Sun 28 Jul 2024 10:21:52 PM UTC
Group       : Unspecified
Size        : 1024035
License     : PHP-3.01
Signature   : RSA/SHA256, Tue 02 Jul 2024 02:07:23 PM UTC, Key ID 555097595f11735a
Source RPM  : php82-php-8.2.21-1.el8.remi.src.rpm
Build Date  : Tue 02 Jul 2024 01:33:41 PM UTC
Build Host  : builder.remirepo.net
Relocations : (not relocatable)
Packager    : Remi Collet
Vendor      : Remi's RPM repository <https://rpms.remirepo.net/> #StandWithUkraine
URL         : http://www.php.net/
Bug URL     : https://forum.remirepo.net/
Summary     : The Zend OPcache
...

PHP OPcache'i Yapılandır

find komutunu kullanarak OPcache modül yolunu bulun.

$ sudo find / -name opcache.so
/usr/lib64/php/modules/opcache.so
/usr/lib64/php-zts/modules/opcache.so

Modül, kurulum sırasında otomatik olarak yüklenmelidir.

$ php -m  | grep -i opcache
Zend OPcache
Zend OPcache

Kurulum programı ayrıca /etc/php.d içinde bir yapılandırma şablonu oluşturacaktır, bunu özelleştirebilirsiniz.

$ ls /etc/php.d/|grep opcache
10-opcache.ini
opcache-default.blacklist

Dosyayı düzenlemek için aç.

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

Sunucumuzda, iyi bir performans sağlamak için aşağıdaki ayarları kullanıyoruz:

; Enable Zend OPcache extension module
zend_extension=opcache

; Determines if Zend OPCache is enabled
opcache.enable=1

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

; The OPcache shared memory storage size (in MB)
opcache.memory_consumption=512

; The amount of memory for interned strings in Mbytes.
opcache.interned_strings_buffer=128

; The maximum number of keys (scripts) in the OPcache hash table.
opcache.max_accelerated_files=10000

; The maximum percentage of "wasted" memory until a restart is scheduled.
opcache.max_wasted_percentage=5

; Enable timestamp validation, ensuring that cached files are up to date.
opcache.validate_timestamps=1

; Checks for script changes every 2 seconds.
opcache.revalidate_freq=2

; Path where OPcache can store file cache.
opcache.file_cache=/tmp

; Enables or disables opcode caching in shared memory.
opcache.file_cache_only=0

; Enables or disables checksum validation when script loaded from file cache.
opcache.file_cache_consistency_checks=1

; Other performance settings
opcache.fast_shutdown=1
opcache.enable_file_override=1
opcache.optimization_level=0x7FFFBFFF

Yapılandırabileceğiniz ve özelleştirebileceğiniz seçeneklerin tam listesi için resmi OPcache belgelerine başvurun.

OPcache'in çalışıp çalışmadığını test et

Web sunucunuzu ve php-fpm servislerinizi yeniden başlatın.

  • Nginx web sunucusu
sudo systemctl restart php-fpm nginx
  • Apache httpd web sunucusu
sudo systemctl restart php-fpm httpd

OPcache çalışma zamanı yapılandırmalarının listelenmesi aşağıdaki komutlar çalıştırılarak yapılabilir:

php -i | grep opcache

Sistemimizde OPcache’in çalışıp çalışmadığını test etmek için örnek bir PHP scripti oluşturun.

  • Nginx web sunucusu
sudo vi /usr/share/nginx/html/opcache.php
  • Apache httpd web sunucusu
sudo vi /var/www/html/opcache.php

İçeriği dosyaya ekleyin.

<?php
if (function_exists('opcache_get_status')) {
    $status = opcache_get_status();
    if ($status) {
        echo "OPcache is enabled on the server.";
    } else {
        echo "OPcache is disabled on the server..";
    }
} else {
    echo "OPcache is not installed.";
}
?>

Dosyayı kaydedin ve http://serverIP_or_fqdn/opcache.php adresinden açın

Bu, PHP OPcache'in sistemimizde sorunsuz çalıştığını doğrulamaktadır. Umarız bu makale yardımcı olmuştur ve size daha hızlı web sitesi yükleme süreleri dileriz!