Use ZendOpcache to accelerate PHP

Source: Internet
Author: User
Tags apc php software
Optimizer + is a closed-source php optimization acceleration component developed by Zend but available for free. it is the first and fastest opcode cache tool. Optimizer + is a closed-source php optimization acceleration component developed by Zend but available for free. it is the first and fastest opcode cache tool. Now Zend technology company has turned Optimizer + into Zend Opcache open-source under PHP License.

Zend OPcache provides faster PHP execution process through opcode caching and optimization. It stores pre-compiled script files in the shared memory for future use, thus avoiding the time consumption of reading code from the disk and compiling. At the same time, it also applies some code optimization modes to make code execution faster.

1. what is opcode cache?

After analyzing the script Code, the interpreter generates intermediate Code that can be run directly, also known as the Operate Code (opcode ). Opcode cache is designed to avoid repeated compilation and reduce CPU and memory overhead. If the performance bottleneck of dynamic content does not lie in CPU and memory, but in I/O operations, such as disk I/O overhead caused by database queries, the performance improvement of opcode cache is very limited. However, since opcode cache can reduce CPU and memory overhead, it is always a good thing-in line with the environmental attitude, should we minimize the consumption? : D

The modern opcode cache (Optimizer +, APC2.0 +, and others) uses shared memory for storage and can directly execute files from it, instead of "deserialization" code before execution. This will lead to significant performance acceleration, which usually reduces the overall server memory consumption and has few disadvantages.

2. Advantages and disadvantages of Optimizer + and APC

Optimizer + was renamed Opcache in middle March 2013.

According to the discussion on PHP wiki, Zend Opcache will be integrated into php 5.5. As a competitor of APC, Zend Opcache is likely to replace APC, although OptimizerPlus does not have the user cache function like APC.

Advantages of OPTIMIZER + relative APC

Performance. According to tests, Zend Optimizer + is always better than APC. The number of requests processed per second increases by 5 to 5 depending on the code ~ 20%. WordPRess 2.1.1 (I don't know why I don't need a new version of WP for testing) in the test results recorded on Google doc, and the performance is improved by about 8%. Theoretically, for WP 3.5.1, the performance should also be approximately 5 ~ 10% improvement. For servers running WordPress, using Optimizer + can significantly reduce CPU usage and increase the page loading speed (graphics here ).

Supports the new PHP version. Both Zend and the PHP community will help Optimizer + support the latest version of PHP.

Reliability. Optimizer + has the optional damage detection capability to prevent server crashes caused by data corruption.

Better compatibility. The PHP community intends to make Optimizer + compatible with all PHP versions supported by the community.

Advantages of APC over OPTIMIZER +

APC has a data cache API, but Optimizer + does not.

APC can reclaim the memory occupied by old invalid scripts. APC has a memory manager that can recycle memory associated with scripts that are no longer in use. Unlike Optimizer +, it marks such memory as "dirty", but does not recycle them. Once the percentage of the configured threshold value for "dirty" memory usage reaches a certain value, Optimizer + restarts itself. This behavior has both advantages and disadvantages in terms of stability.

3. use Zend Opcode

Now you can use Zend Opcache to replace APC as a PHP optimization acceleration tool. The current Zend Opcode is compatible with PHP 5.2. *, 5.3. *, 5.4. * and PHP-5.5 development edition. However, the support for PHP 5.2 will be canceled in the future.

Note: Zend Opcache conflicts with eaccelerator. To install Zend Opcache, you may need to uninstall eaccelerator first-if you use this acceleration module.

Install and configure from source code

The source code of Zend Opcache is hosted on github. it is still called ZendOptimizerPlus.

For installation steps, see The README file.

Note:

It is best to deploy it on your own server after testing in the local virtual machine;

You are advised to delete components such as eacceleratro, xcache, and apc before installation.

By the way, php-devel is required for source code compilation and installation. In README, quick installation is used at the beginning of the section,

$ PHP_DIR/bin/phpize

If you do not know the phpize path, you can,

Whereis phpize

The recommended optimization settings are also available in the README file.

Install and configure from EPEL source

I do not like to compile and install programs from the source code. one is limited in level, and the other is afraid of trouble. The following describes how to install Zend Opcache from the EPEL installation source. the operations on CentOS are used as an example based on my VPS configuration.

The EPEL community already provides the Zend Opcache installation package, which can be directly installed using yum. Of course, the premise is that the EPEL installation source has been configured and used. If not, refer to here.

Please note that PHP on the REMI installation source is already version 5.4. Some people have tested that WordPress's performance on PHP 5.4 is better than that on PHP 5.3 (10% faster and lower ram consuming). By the way, upgrading PHP is not a bad thing.

Procedure:

Configure to use epel to install the source. Skip if it already exists.

Delete eaccelerator, xcache, and apc:

Yum remove php-eaccelerator php-xcache php-apcu

Skip this step.

Upgrade the system:

Yum update

The purpose is to upgrade the current php software to the latest version supported by remi based on the status of the remi installation source. At this point, we can see that the system has output similar to the following:

Updating: php-common-5.4.14-1.el6.remi.i686 1/26

WARNING: These php-* RPM are not official Fedora/Red Hat build and
Overrides the official ones. Don't file bugs on Fedora Project nor Red Hat.

Use dedicated forums http://forums.famillecollet.com/

Warning:/etc/php. ini created as/etc/php. ini. rpmnew
Updating: MySQL-libs-5.5.31-1.el6.remi.i686 2/26

WARNING: This MySQL RPM is not an official Fedora/Red Hat build and it
Overrides the official one. Don't file bugs on Fedora Project nor Red Hat.
Use dedicated forums http://forums.famillecollet.com/

Warning:/etc/my. cnf created as/etc/my. cnf. rpmnew

It indicates that we are migrating from the Fedora/Red Hat version to the Remi version, so do not go to Fedora/Red Hat for help. Haha, it seems that all the problems are found on the Internet, and there are very few questions in the official forum. Entry-level users like me will not encounter such in-depth problems.

Install Zend Opcache (pecl version ):

Yum install php-pecl-zendopcache

The opcache configuration file generated during installation is located in the default/etc/php. d Directory:

Opcache-default.blacklist
Opcache. ini

This configuration file uses the recommended settings in README, and only needs to be modified in a few places.

Vi/etc/php. d/opcache. ini

Modify and save the configuration as follows (see the complete Zend Opcache configuration ):

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

You do not need to modify the php. ini configuration and restart the Apache service to make it take effect:

Service httpd restart

Check to see if the startup is correct:

Php-v

The output result is similar:

PHP 5.4.14 (cli) (built: Apr 11 2013 11:04:35) Copyright (c) 1997-2013 The PHP GroupZend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies with Zend OPcache v7.0.1, copyright (c) 1999-2013, by Zend Technologies

The above is the use of ZendOpcache to accelerate PHP Content. For more articles, please follow the PHP Chinese network (www.php1.cn )!

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.