Use Opcache to speed up your website (reprint)

Source: Internet
Author: User
Tags fpm php website zend

[Article rolling City reproduced please specify the original source: https://mo2g.com/view/137/]

Maybe a lot of friends in contact with PHP, more or less understand: PHP is a scripting language, an interpreted language, so it does not need to be compiled to run, and so on. This is not entirely true, but for beginners, it might be more appropriate to get a quick start and debug your code. But when you want to optimize your PHP-based Web site to support higher concurrent access, you need to know what opcodes is.

Maybe a lot of friends in contact with PHP, more or less understand: PHP is a scripting language, an interpreted language, so it does not need to be compiled to run, and so on.

This is not entirely true, but for beginners, it may be more appropriate to understand this, not only to get started quickly, but also to debug the code quite handy. But when you want to optimize your PHP-based Web site to support higher concurrent access, you need to know what opcodes is.

By default, PHP will generate the corresponding opcode for each request, such as code parsing, compiling, and finally executing through the Zend engine. :

Examples of reference laruence:

12345 <?phpecho"Hello World";$a = 1 + 1;echo$a;?>

PHP executes this code in the following 4 steps (to be exact, it should be PHP's language engine Zend)
1.Scanning (lexing), convert PHP code to language fragment (Tokens)
2.Parsing, convert tokens to simple and meaningful expressions
3.Compilation, compile the expression into Opocdes
4.Execution, execute opcodes sequentially, one at a time, thus realizing the function of PHP script.

The role of the opcodes cache is to save the compiled operation code for a period of time, so that the Zend engine does not have to parse and compile in subsequent requests. :

When you're struggling to find a way to speed up your PHP Web response, I'm sure Cache opcodes is one of the methods you're looking for, and it's the easiest.

This article describes how to use Opcache (formerly known as Zend Optimizer Plus, or o+) to accelerate PHP programs. O+ is a PHP performance improvement tool developed by Zend, and unlike other opcodes caching tools, o+ does a lot of opcodes optimizations in addition to cache opcodes.

Starting with PHP5.5, O+ has been released along with PHP's source code. More than 5.2 of the PHP version can also be compiled to load the o+. Http://pecl.php.net/zendopcache

Under the same configuration, using Apache benchmark for stress testing, the o+ optimization results are as follows:

Ab-c50-t2 10.0.0.59/ab_test.php

Open o+:requests per second:168.77 [#/sec] (mean)

Close o+:requests per second:157.44 [#/sec] (mean)

Increased by 11.33

The ab_test.php code is as follows:

123456789 <?php$x= 10000;$arrarray();while($x) {    $arr[$x] = $x--;}foreach$arras $row) {    echo"$row<br>";}

Next, I will post the installation process, because I use Yum to build the environment, may not apply to your situation, for reference only.

1) Download, compile, install

1234567 wget http://pecl.php.net/get/zendopcache-7.0.3.tgztarzxvf zendopcache-7.0.3.tgzcd zendopcache-7.0.3/phpize./configuremakemakeinstall

2) Edit php.ini, add configuration at the bottom of the file

12345678910111213141516 vi/etc/php.inizend_extension=/usr/lib64/php/modules/opcache.so;命令行模式是否开启缓存功能,我建议不开启opcache.enable_cli=0;共享内存大小, 这个根据你们的需求可调opcache.memory_consumption=128;interned string的内存大小, 也可调opcache.interned_strings_buffer=8;最大缓存的文件数目opcache.max_accelerated_files=4000;60s检查一次文件更新opcache.revalidate_freq=60;打开快速关闭, 打开这个在PHP Request Shutdown的时候,回收内存的速度会提高opcache.fast_shutdown=1;不保存文件/函数的注释opcache.save_comments=0

3) Let php-fpm reload the configuration file

1 /ect/init.d/php-fpmreload

4) Run the following code

12 <?phpphpinfo();

If you can find a similar text hint, PHP has successfully loaded the Opcache. Try it now, is the PHP website responding faster?

With Zend Opcache v7.0.3, Copyright (c) 1999-2014, by Zend Technologies

Use Opcache to speed up your website (reprint)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.