Bộ đệm video php

Sử dụng APC hoặc APCu từ lâu đã là một phương pháp opcode cache được sử dụng rộng rãi để tối ưu hóa hoạt động của VPS. Tuy nhiên APC không được cập nhật, nâng cấp thường xuyên [phiên bản ổn định mới nhất 3. 1. 9 ra ngày 2011-05-14] và thỉnh thoảng vẫn xuất hiện một số lỗi phụ

Với sự xuất hiện của Zend OPcache mặc định đi kèm từ phiên bản PHP 5. 5 trở về sau, chắc chắn rằng APC hoặc các bộ đệm opcode khác sẽ không có cửa để phát triển nữa

Trong bài viết này mình sẽ hướng dẫn các bạn cách cài đặt Zend OPcache với phiên bản PHP 5. 5 và 5. 6. Các phiên bản PHP 5. 4 hoặc 5. 3 sẽ có cách cài đặt khác, mình không đề cập nhật trong bài viết này vì phiên bản PHP đã quá cũ rồi

Để xem phiên bản PHP hiện tại, bạn hãy sử dụng lệnh php -v hoặc php-fpm -v

Các bạn có thể sử dụng tham số 

## PHP 7.0 ##
yum --enablerepo=remi,remi-php70 install php-opcache
service php-fpm restart

## PHP 5.6 ##
yum --enablerepo=remi,remi-php56 install php-opcache
service php-fpm restart

## PHP 5.5 ##
yum --enablerepo=remi,remi-php55 install php-opcache
service php-fpm restart
0 khi biên dịch PHP, tuy nhiên cách này hơi khó thực hiện với đa số người dùng, do đó hãy sử dụng cách cài đặt thêm tiện ích mở rộng cho PHP. Lưu ý cần cài đặt PHP trước khi bắt đầu

HocVPS Script mặc định tích hợp Zend Opcache rồi, các bạn đang dùng không cần cài thêm nữa nhé

Plugin Duplicator, BackWPUp hay UpDraftPlus của WordPress bị lỗi 502 Bad Gateway không tương thích với Zend OPCache, để khắc phục các bạn tham khảo hướng dẫn sau

1. Add repo epel, remi

## CentOS 7 ##
yum install epel-release
rpm -Uvh //rpms.famillecollet.com/enterprise/remi-release-7.rpm

## CentOS 6 ##
yum install epel-release
rpm -Uvh //rpms.famillecollet.com/enterprise/remi-release-6.rpm

Bước này có thể bỏ qua nếu bạn đã làm theo hướng dẫn cài đặt LEMP trên CentOS

2. Cài đặt Zend Opcache của PHP

## PHP 7.0 ##
yum --enablerepo=remi,remi-php70 install php-opcache
service php-fpm restart

## PHP 5.6 ##
yum --enablerepo=remi,remi-php56 install php-opcache
service php-fpm restart

## PHP 5.5 ##
yum --enablerepo=remi,remi-php55 install php-opcache
service php-fpm restart

Với PHP 5. 4, 5. 3 lượt xem

Sau khi cài đặt xong, bạn hãy xem thông tin 

## PHP 7.0 ##
yum --enablerepo=remi,remi-php70 install php-opcache
service php-fpm restart

## PHP 5.6 ##
yum --enablerepo=remi,remi-php56 install php-opcache
service php-fpm restart

## PHP 5.5 ##
yum --enablerepo=remi,remi-php55 install php-opcache
service php-fpm restart
1, nếu Zend OPcache xuất hiện như bên dưới là đã thành công

3. Cấu hình Zend Opcache của PHP

Mặc định, OPcache sử dụng bộ nhớ 128 MB và bộ nhớ cache tối đa là 4. 000 tập tin. Nếu bạn muốn thay đổi tham số này thì chỉ cần chỉnh sửa trong tệp cấu hình 

## PHP 7.0 ##
yum --enablerepo=remi,remi-php70 install php-opcache
service php-fpm restart

## PHP 5.6 ##
yum --enablerepo=remi,remi-php56 install php-opcache
service php-fpm restart

## PHP 5.5 ##
yum --enablerepo=remi,remi-php55 install php-opcache
service php-fpm restart
2 hoặc
## PHP 7.0 ##
yum --enablerepo=remi,remi-php70 install php-opcache
service php-fpm restart

## PHP 5.6 ##
yum --enablerepo=remi,remi-php56 install php-opcache
service php-fpm restart

## PHP 5.5 ##
yum --enablerepo=remi,remi-php55 install php-opcache
service php-fpm restart
3

nano /etc/php.d/10-opcache.ini

Các thông tin cần chỉnh sửa

; The OPcache shared memory storage size.
opcache.memory_consumption=128

; The maximum number of keys [scripts] in the OPcache hash table.
; Only numbers between 200 and 100000 are allowed.
opcache.max_accelerated_files=4000

4. Trình xem web

Để theo dõi trạng thái sử dụng Zend Opcache, các bạn có thể sử dụng Web Viewer opcache-gui bằng amnuts [hoặc cài đặt plugin OPcache Dashboard bằng WordPress]

The following function retrieves a line in a file, regardless of its size, so you won't get an error if the file's size is beyond php's allowed memory limit [the string has to be below however], which is something i was needing for accessing a big log file generated by a webhost. Indexes start at 1 [so $line = 1 means the first line unlike arrays]. If the file is small, it would be better to use "file[]" however.

function strpos_count[$haystack, $needle, $i = 0] {
    while [strpos[$haystack,$needle] !== false] {$haystack = substr[$haystack, [strpos[$haystack,$needle] + 1]]; $i++;}
    return $i;
}
function getLine[$file,$line=1]{
    $occurence = 0;
    $contents = '';
    $startPos = -1;
    if [!file_exists[$file]] return '';
    $fp = @fopen[$file, "rb"];
    if [!$fp] return '';
    while [!@feof[$fp]] {
        $str = @fread[$fp, 1024];
        $number_of_occurences = strpos_count[$str,"\n"];
        if [$number_of_occurences == 0] {if [$start_pos != -1] {$contents .= $str;}}
        else {
            $lastPos = 0;
            for [$i = 0; $i < $number_of_occurences; $i++]{
                $pos = strpos[$str,"\n", $lastPos];
                $occurence++;
                if [$occurence == $line] {
                    $startPos = $pos;
                    if [$i == $number_of_occurences - 1] {$contents = substr[$str, $startPos + 1];}
                } elseif [$occurence == $line + 1] {
                    if [$i == 0] {$contents .= substr[$str, 0, $pos];} else {$contents = substr[$str, $startPos, $pos - $startPos];}
                    $occurence = 0;
                    break;
                }
                $lastPos = $pos + 1;
            }
        }
    }
    @fclose[$fp];
    return $contents;
}
?>

Chủ Đề