ติดตั้ง PHP OPcache บน Rocky/Alma/CentOS 9|8
OPcache ถูกออกแบบมาเพื่อปรับปรุงประสิทธิภาพของ PHP โดยการเก็บโค้ดบไตร์ที่ถูกคอมไพล์แล้วไว้ในหน่วยความจำร่วม โดยการเก็บสคริปต์ PHP ไว้ในหน่วยความจำ จะช่วยลดความจำเป็นที่ PHP ต้องโหลดและวิเคราะห์สคริปต์ในทุกคำขอที่ได้รับ เมื่อโฮสต์เว็บไซต์หรือแอปพลิเคชันเว็บที่สร้างด้วย PHP การใช้ OPcache จะช่วยเพิ่มความรวดเร็วในการตอบสนองและเวลาการโหลดโดยรวมอย่างมีนัยสำคัญ
นี่คือวิธีการทำงานของ PHP OPcache:
- การคอมไพล์สคริปต์: OPcache จะคอมไพล์สคริปต์ที่ถูกเรียขอเป็นไบต์โค้ด
- การเก็บบันทึกไบต์โค้ด: เมื่อถูกคอมไพล์แล้ว ไบต์โค้ดจะถูกเก็บไว้ในหน่วยความจำร่วมเพื่อการเข้าถึงที่รวดเร็วขึ้น
- การดำเนินการคำขอถัดไป: ครั้งต่อไปที่มีการรับคำขอสำหรับสคริปต์เดียวกัน จะใช้ไบต์โค้ดที่เก็บไว้ในแคช ทำให้สามารถข้ามขั้นตอนการคอมไพล์ ส่งผลให้การดำเนินการเร็วขึ้น
สรุปได้ว่า การใช้ OPcache จะช่วยให้การรันสคริปต์ PHP เร็วขึ้น ลดการใช้งาน CPU และหน่วยความจำ และเพิ่มความสามารถในการปรับขนาดของระบบเนื่องจากสามารถรองรับคำขอพร้อมกันได้มากขึ้น
แล้วคุณจะติดตั้งและตั้งค่า PHP OPcache บนระบบ Linux อย่าง Rocky Linux/AlmaLinux และ CentOS Stream 9|8 ได้อย่างไร ทำตามขั้นตอนต่อไปเพื่อเรียนรู้วิธีทำ
เปลี่ยนเป็นเวอร์ชัน PHP จาก REMI
เราสามารถเปิดใช้งาน OPcache ได้เฉพาะบนระบบที่ติดตั้ง PHP เท่านั้น ระบบที่กล่าวถึงจะจัดจำหน่ายหลายเวอร์ชันของ PHP ในที่เก็บ AppStream แต่ Opcache จะมีให้ในที่เก็บ REMI
ในการตรวจสอบว่า PHP ถูกติดตั้งแล้ว ให้รันคำสั่ง:
$ 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จากระบบของผม เราสามารถยืนยันได้ว่ารุ่นคือ 8.2 ดังนั้นเราสามารถรีเซ็ตโมดูล PHP ได้
sudo dnf module reset php -yต่อไปเราจะติดตั้งที่เก็บ RPM ของ Remi:
- Enterprise Linux 9 – (Rocky/Alma/CentoS Stream 9)
sudo dnf -y install https://rpms.remirepo.net/enterprise/remi-release-9.rpm- Enterprise Linux 8 – (Rocky/Alma/CentoS Stream 8)
sudo dnf -y install https://rpms.remirepo.net/enterprise/remi-release-8.rpmตัวติดตั้งจะตั้งค่าที่เก็บ EPEL โดยอัตโนมัติเป็นการขึ้นต่อกัน เพื่อยืนยันว่า REMI ทำงานได้ ให้รัน:
$ 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สามารถตรวจสอบโมดูล PHP ทั้งหมดได้โดยการรันคำสั่งดังต่อไปนี้:
dnf module list phpนี่คือตัวอย่างผลลัพธ์:
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ตอนนี้ ให้เปิดใช้งานโมดูล PHP เฉพาะจาก REMI จากผลลัพธ์ก่อนหน้านี้ PHP ที่ติดตั้งคือ 8.2 ซึ่งหมายความว่าเราควรเปิดใช้งานโมดูล php:remi-8.2
sudo dnf module install php:remi-8.2 -yในกรณีส่วนใหญ่ จะทำการอัพเกรดเวอร์ชัน PHP ที่ติดตั้งอยู่ของคุณ และจะไม่ลบแพ็กเกจออก
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
เมื่อได้กำหนดค่าที่เก็บ RPM ของ REMI แล้ว เราสามารถค้นหารุ่นของ OPcache ที่มีอยู่ได้
$ 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
ในตัวอย่างนี้เรากำลังติดตั้ง php82-php-opcache ซึ่งเฉพาะกับ PHP รุ่น 8.2 ให้เปลี่ยนเป็นรุ่นที่เหมาะสมกับ PHP ที่คุณติดตั้งอยู่
sudo dnf install php82-php-opcacheข้อมูลเพิ่มเติมเกี่ยวกับแพ็กเกจที่ติดตั้งสามารถสอบถามได้โดยใช้คำสั่ง rpm -qi:
$ 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
ใช้คำสั่ง find เพื่อค้นหาเส้นทางโมดูล OPcache
$ sudo find / -name opcache.so
/usr/lib64/php/modules/opcache.so
/usr/lib64/php-zts/modules/opcache.soโมดูลควรถูกโหลดโดยอัตโนมัติเมื่อทำการติดตั้ง
$ php -m | grep -i opcache
Zend OPcache
Zend OPcacheโปรแกรมติดตั้งจะสร้างเทมเพลตการตั้งค่าภายใน /etc/php.d ด้วย คุณสามารถปรับแต่งได้
$ ls /etc/php.d/|grep opcache
10-opcache.ini
opcache-default.blacklistเปิดไฟล์เพื่อแก้ไข
sudo vi /etc/php.d/10-opcache.iniในเซิร์ฟเวอร์ของเรา เราใช้การตั้งค่าต่อไปนี้เพื่อให้ได้ประสิทธิภาพที่ดี:
; 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โปรดดูเอกสาร OPcache อย่างเป็นทางการสำหรับรายการตัวเลือกทั้งหมดที่คุณสามารถกำหนดค่าและปรับแต่งได้
ทดสอบว่า OPcache ทำงานหรือไม่
รีสตาร์ทเว็บเซิร์ฟเวอร์และบริการ php-fpm ของคุณ
- เซิร์ฟเวอร์เว็บ Nginx
sudo systemctl restart php-fpm nginx- เว็บเซิร์ฟเวอร์ Apache httpd
sudo systemctl restart php-fpm httpdการแสดงรายการการตั้งค่าเวลาใช้งานของ OPcache สามารถทำได้โดยการรันคำสั่งดังต่อไปนี้:
php -i | grep opcache
สร้างสคริปต์ PHP ตัวอย่างสำหรับทดสอบว่า OPcache ทำงานในระบบของเราหรือไม่
- เซิร์ฟเวอร์เว็บ Nginx
sudo vi /usr/share/nginx/html/opcache.php- เว็บเซิร์ฟเวอร์ Apache httpd
sudo vi /var/www/html/opcache.phpเพิ่มเนื้อหาลงในไฟล์
<?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.";
}
?>บันทึกและเปิดไฟล์จาก http://serverIP_or_fqdn/opcache.php
นี่ยืนยันว่า PHP OPcache ทำงานได้ดีในระบบของเรา เราหวังว่าบทความนี้จะเป็นประโยชน์และขอให้เว็บไซต์ของคุณโหลดได้เร็วขึ้น!

