Speed ​​up PHP with ZendOpcache

黄舟
Release: 2023-03-03 14:18:02
Original
1199 people have browsed it

Optimizer+ is a closed-source but free-to-use PHP optimization acceleration component developed by Zend. It is the first and fastest opcode caching tool. Now, Zend Technologies has open sourced Optimizer+ as Zend Opcache under the PHP License.

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

1. What is opcode cache?

When the interpreter completes the analysis of the script code, it generates intermediate codes that can be run directly, also called operation codes (Operate Code, opcode). The purpose of Opcode cache is to avoid repeated compilation and reduce CPU and memory overhead. If the performance bottleneck of dynamic content lies not in CPU and memory, but in I/O operations, such as disk I/O overhead caused by database queries, then the performance improvement of opcode cache is very limited. But since opcode cache can reduce CPU and memory overhead, this is always a good thing - in an environmentally friendly manner, consumption should be reduced as much as possible, right? :D

Modern opcode caches (Optimizer+, APC2.0+, others) use shared memory for storage and can execute files directly from them without having to "deserialize" the code before execution. This results in significant performance speedups, often lower overall server memory consumption, and few downsides.

2. Comparison of the advantages and disadvantages of Optimizer+ and APC

Optimizer+ was renamed Opcache in mid-March 2013.

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

Advantages of OPTIMIZER+ over APC

Performance. Based on testing, Zend Optimizer+ consistently outperforms APC. Depending on the code, the number of requests processed per second is 5~20% higher. Among the test results recorded on Google doc, the performance of WordPress 2.1.1 (I don’t know why I didn’t test it with a new version of WP) increased by about 8%. Theoretically, for WP 3.5.1, performance should be improved by about 5~10%. For servers running WordPress, using Optimizer+ can significantly reduce CPU usage and increase page load speeds (graphics here).

Support new PHP version. Zend and the PHP community will help Optimizer+ support the latest versions of PHP.

Reliability. Optimizer+ has optional corruption detection capabilities that prevent server crashes due to data corruption.

Better compatibility. The PHP community intends for Optimizer+ to be compatible with all community-supported versions of PHP.

Advantages of APC over OPTIMIZER+

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

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

3. Use Zend Opcode

Now you can use Zend Opcache instead of 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 version. However, support for PHP 5.2 will be removed 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, and it is still called ZendOptimizerPlus.

For detailed installation steps, please refer to its README file.

Note:

It is best to test in a local virtual machine before deploying to your own server;

It is best to delete components such as eacceleratro, xcache or apc before installation.

By the way, php-devel is required when compiling and installing from source code. It is used at the beginning of the quick installation section in the README,

$PHP_DIR/bin/phpize

If you don’t know the path to phpize, you can,

where is phpize

There are also corresponding recommended optimization settings in the README file.

Install and configure from EPEL source

I don’t like to compile and install programs from source code. One is because my level is limited, and the other is because I’m afraid of trouble. The following describes how to install Zend Opcache from the EPEL installation source, taking the operation on CentOS as an example, based on the configuration of my VPS.

EPEL community has provided the installation package of Zend Opcache, which can be installed directly with yum. Of course, the premise is that the EPEL installation source has been configured and used. If not, you can refer to here.

As a reminder, PHP on the REMI installation source is already version 5.4. Since some people have tested that WordPress performs better on PHP 5.4 than on PHP 5.3 (10% faster and lower ram consuming), it’s not a bad thing to upgrade PHP by the way.

Operation steps:

Configure using epel installation source. If it already exists, skip it.

Delete eaccelerator, xcache, apc:

yum remove php-eaccelerator php-xcache php-apcu

Skip if not used.

Upgrade the system:

yum update

The purpose is to upgrade the current php and other software to the latest version supported by remi based on the status of the remi installation source. At this time, you 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-* RPMs 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                                                                                                                                               ’ s ‐ ‐ ‐ ‐ ‐ ‐ ‐ ‐ 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 means that we are now 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 problems arise are found online, and I rarely ask 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 basically uses the recommended settings in the README, and only a few places need to be modified.

vi /etc/php.d/opcache.ini

Just modify and save the recommended configuration as follows (please refer to the complete Zend Opcache configuration information):

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

No need to modify the php.ini configuration, restart the Apache service to make it take effect:

service httpd restart

Check to see if it is started correctly:

php -v

The output result is similar to:

PHP 5.4.14 (cli) (built: Apr 11 2013 11:04:35) Copyright (c) 1997-2013 The PHP Group Zend 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 content of using ZendOpcache to accelerate PHP. For more related articles, please pay attention to the PHP Chinese website (www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template