Cách kích hoạt mô-đun OPcache tích hợp của ServBay
Là một công cụ phát triển Web tích hợp mạnh mẽ, ServBay cung cấp mô-đun OPcache và quá trình kích hoạt nó rất đơn giản. OPcache là một mô-đun bộ đệm bytecode của PHP, cải thiện hiệu suất của các ứng dụng PHP bằng cách lưu trữ bytecode đã biên dịch sẵn.
Giới thiệu mô-đun OPcache
OPcache là một phần mở rộng tích hợp sẵn của PHP, được sử dụng để cải thiện hiệu suất thực thi của PHP. Nó lưu trữ bytecode của các script PHP, tránh việc biên dịch lại code PHP mỗi lần yêu cầu, giảm việc sử dụng CPU và bộ nhớ, tăng tốc độ phản hồi.
Đặc điểm chính
- Tăng hiệu suất: Bằng cách lưu trữ bytecode PHP, giảm tải chi phí biên dịch, tăng tốc độ thực thi của ứng dụng PHP.
- Giảm tiêu thụ tài nguyên: Giảm việc sử dụng CPU và bộ nhớ, tận dụng tài nguyên máy chủ hiệu quả hơn.
- Quản lý tự động: OPcache quản lý bộ đệm tự động, không cần sự can thiệp của nhà phát triển.
- Cấu hình linh hoạt: Cung cấp nhiều tùy chọn cấu hình, cho phép nhà phát triển điều chỉnh hành vi bộ đệm theo nhu cầu.
- Phần mở rộng tích hợp: OPcache là một phần mở rộng tích hợp của PHP, dễ cài đặt và cấu hình.
Phiên bản mô-đun OPcache tích hợp của ServBay
ServBay hỗ trợ nhiều phiên bản PHP và cài đặt sẵn các mô-đun OPcache tương ứng cho mỗi phiên bản.
Cách kích hoạt mô-đun OPcache
Mặc định, mô-đun OPcache bị vô hiệu hóa. Để kích hoạt mô-đun OPcache, chỉ cần chỉnh sửa tập tin cấu hình của phiên bản PHP tương ứng. Dưới đây là các bước chi tiết:
Bước 1: Tìm tập tin cấu hình
Trước hết, xác định thư mục conf.d
của phiên bản PHP tương ứng. Ví dụ, để kích hoạt mô-đun OPcache cho PHP 8.3, chúng ta cần chỉnh sửa tập tin sau:
/Applications/ServBay/etc/php/8.3/conf.d/opcache.ini
Bước 2: Chỉnh sửa tập tin cấu hình
Mở tập tin opcache.ini
, bỏ dấu chú thích và thêm các cấu hình cần thiết:
[Zend Opcache]
; Bỏ dấu chú thích dòng sau để kích hoạt OPcache
zend_extension = opcache.so
opcache.memory_consumption = 128
opcache.interned_strings_buffer = 8
opcache.max_accelerated_files = 4000
opcache.revalidate_freq = 60
opcache.fast_shutdown = 1
opcache.enable_cli = 1
2
3
4
5
6
7
8
9
Bước 3: Khởi động lại dịch vụ PHP
Trong bảng điều khiển quản lý dịch vụ của ServBay, khởi động lại dịch vụ PHP tương ứng. Ví dụ, khởi động lại dịch vụ PHP 8.3. Sau khi khởi động lại, mô-đun OPcache sẽ được tải thành công.
Kiểm tra xem mô-đun OPcache đã được tải thành công chưa
Bạn có thể tạo một tập tin PHP đơn giản để kiểm tra xem mô-đun OPcache đã được tải thành công chưa. Tạo một tập tin phpinfo.php
trong thư mục gốc của máy chủ Web với nội dung sau:
<?php
phpinfo();
?>
2
3
Truy cập https://servbay.host/phpinfo.php
, trên trang thông tin PHP xuất ra, tìm thông tin liên quan đến mô-đun OPcache. Nếu nhìn thấy các thông tin về OPcache, có nghĩa là mô-đun đã được tải thành công.
Cấu hình các tham số của OPcache
OPcache cung cấp nhiều tùy chọn cấu hình, cho phép nhà phát triển điều chỉnh hành vi bộ đệm theo nhu cầu. Dưới đây là một số tùy chọn cấu hình thông dụng:
[OPcache]
zend_extension = opcache.so
opcache.enable = 1
opcache.memory_consumption = 128 ; Đặt kích thước bộ nhớ dành cho OPcache, đơn vị là MB
opcache.interned_strings_buffer = 8 ; Đặt kích thước bộ nhớ dành cho chuỗi nội, đơn vị là MB
opcache.max_accelerated_files = 10000 ; Đặt số lượng tệp tối đa được lưu trữ trong bộ đệm
opcache.revalidate_freq = 2 ; Đặt tần suất xác minh lại bộ đệm, đơn vị là giây
opcache.fast_shutdown = 1 ; Kích hoạt đóng nhanh
opcache.enable_cli = 1 ; Kích hoạt OPcache trong chế độ CLI
2
3
4
5
6
7
8
9
Kết luận
ServBay cung cấp một cách thuận tiện để quản lý và kích hoạt mô-đun OPcache. Thông qua các bước cấu hình và khởi động lại đơn giản, nhà phát triển có thể nhanh chóng kích hoạt mô-đun OPcache trên các phiên bản PHP khác nhau, qua đó tăng đáng kể hiệu suất của ứng dụng PHP. Chức năng bộ đệm bytecode của OPcache giảm chi phí biên dịch code, tăng tốc độ phản hồi và khả năng sử dụng tài nguyên, là một công cụ hiệu quả để tối ưu hóa hiệu suất của ứng dụng PHP. Thông qua ServBay và OPcache, nhà phát triển có thể xây dựng ứng dụng Web hiệu quả và phản hồi nhanh chóng.