使用 Zend Opcache 加速 PHP
Optimizer+ 是 Zend 开发的闭源但可以免费使用的 PHP 优化加速组件,是第一个也是最快的 opcode 缓存工具。现在,Zend 科技公司将 Optimizer+ 在 PHP License 下开源成为 Zend Opcache。
Zend OPcache 通过 opcode 缓存和优化提供更快的 PHP 执行过程。它将预编译的脚本文件存储在共享内存中供以后使用,从而避免了从磁盘读取代码并进行编译的时间消耗。同时,它还应用了一些代码优化模式,使得代码执行更快。
1. 什么是 opcode 缓存?
当解释器完成对脚本代码的分析后,便将它们生成可以直接运行的中间代码,也称为操作码(Operate Code,opcode)。Opcode cache 的目地是避免重复编译,减少 CPU 和内存开销。如果动态内容的性能瓶颈不在于 CPU 和内存,而在于 I/O 操作,比如数据库查询带来的磁盘 I/O 开销,那么 opcode cache 的性能提升是非常有限的。但是既然 opcode cache 能带来 CPU 和内存开销的降低,这总归是好事 —— 本着环保的态度,也应该尽量减少消耗不是? :D
现代操作码缓存器(Optimizer+,APC2.0+,其他)使用共享内存进行存储,并且可以直接从中执行文件,而不用在执行前“反序列化”代码。这将带来显着的性能加速,通常降低了整体服务器的内存消耗,而且很少有缺点。
2. Optimizer+ 与 APC 的优缺点对比
Optimizer+ 于 2013年3月中旬改名为 Opcache。
根据 PHP wiki 上的讨论,Zend Opcache 即将整合到 php 5.5 中。作为 APC 的竞争对手,新生的 Zend Opcache 很有可能取代 APC 的位置,虽然 OptimizerPlus 没有象 APC 那样的 user cache 功能。
OPTIMIZER+ 相对 APC 的优点
- 性能。根据测试,Zend Optimizer+ 始终优于 APC。随代码差异,每秒钟处理的请求数高 5~20%。Google doc 上记录的测试结果中,WordPress 2.1.1(不知道为什么不用个新版本的 WP 来测试),性能提高约 8%。理论上来说,对于 WP 3.5.1,性能应该也能得到大约 5~10% 的提升吧。对于运行 WordPress 的服务器而言,使用 Optimizer+ 可以显著降低 CPU 使用率和提高页面加载速度(graphics here)。
- 支持新的 PHP 版本。Zend 和 PHP 社区都会帮助 Optimizer+ 能够支持最新版本的 PHP。
- 可靠性。Optimizer+ 拥有可选的损坏检测能力,可以防止因数据损坏而导致的服务器崩溃。
- 更好的兼容性。PHP 社区打算让 Optimizer+ 与社区支持的所有 PHP 版本相兼容。
APC 相对 OPTIMIZER+ 的优势
- APC 有数据缓存 API,而 Optimizer+ 没有。
- APC 能够回收旧的无效的脚本占用的内存。APC 有内存管理器,可以将那些不再使用的脚本关联的内存进行回收。而 Optimizer+ 不同,它将这样的内存标记为“脏的”,但并不会回收它们。一旦“脏的”内存占用配置阈值的百分比达到一定值,Optimizer+ 就将自己重新启动。这种行为在稳定性上既有优势也有劣势。
3. 使用 Zend Opcode
现在已经可以使用 Zend Opcache 替代 APC 作为 PHP 优化加速工具了。目前的 Zend Opcode 兼容 PHP 5.2.*、5.3.*、5.4.* 和 PHP-5.5 开发版。不过,将来会取消对 PHP 5.2 的支持。
注意:Zend Opcache 与 eaccelerator 相冲突。要安装 Zend Opcache,可能需要先卸载 eaccelerator —— 如果你用了这个加速模块的话。
从源码安装并配置
Zend Opcache 的源代码托管在 github 上,目前还是叫做 ZendOptimizerPlus。
安装步骤详见其 README 文件。
注意:
- 最好在本地虚拟机里测试之后再部署到自己的服务器上;
- 安装前最好先删除 eacceleratro、xcache 或 apc 等组件。
顺便说一句,从源码编译安装时需要用到 php-devel。README 中快速安装一节的开头就用到,
$PHP_DIR/bin/phpize
如果不清楚 phpize 的路径,可以,
whereis phpize
README 文件中也有相应的推荐优化设置。
从 EPEL 源安装并配置
我不喜欢从源码编译安装程序,一个是水平有限,一个就是怕麻烦。下面介绍从 EPEL 安装源安装 Zend Opcache,以 CentOS 上的操作为例,基于我的 VPS 的配置。
EPEL 社区已经提供了 Zend Opcache 的安装包,可以直接 yum 安装。当然,前提是已经配置使用了 EPEL 的安装源。如果没有,可以参考这里。
提醒一下,REMI 安装源上的 PHP 已经是 5.4 版本了。鉴于有人测试说 WordPress 在 PHP 5.4 上的性能要优于在 PHP 5.3 上的性能(10% faster and lower ram consuming),顺便升级一下 PHP 也不是什么坏事。
操作步骤:
- 配置使用 epel 安装源。已有则跳过。
- 删除 eaccelerator、xcache、apc:
yum remove php-eaccelerator php-xcache php-apcu
没有使用则跳过。
- 对系统执行升级:
yum update
目的是根据 remi 安装源的状态升级当前的 php 等软件到 remi 支持的最新版本。此时,可以看到系统有类似下面的输出:
Updating : php-common-5.4.14-1.el6.remi.i686 1/26WARNING : These php-* RPM are not official Fedora / Red Hat build andoverrides 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/26WARNING : This MySQL RPM is not an official Fedora / Red Hat build and itoverrides 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
表示我们现在要从 Fedora / Red Hat 的版本迁移到 Remi 版本了,所以不要去 Fedora / Red Hat 寻求帮助了。呵呵,貌似出问题都是在网上找,还真是很少到官方论坛里提问。像我这样的入门级用户,也不会遇到那么深度的问题。
- 安装 Zend Opcache(pecl 版本):
yum install php-pecl-zendopcache
安装时产生的 opcache 的配置文件位于默认的
/etc/php.d
目录中:opcache-default.blacklistopcache.ini
这个配置文件采用的基本就是 README 中的推荐设置,只有几个地方需要修改。
vi /etc/php.d/opcache.ini
对照如下推荐配置修改并保存即可(可参考完整的 Zend Opcache 配置信息):
opcache.memory_consumption=128opcache.interned_strings_buffer=8opcache.max_accelerated_files=4000opcache.revalidate_freq=60opcache.fast_shutdown=1opcache.enable_cli=1
- 不需要修改 php.ini 配置,重起 Apache 服务使之生效:
service httpd restart
查询一下看看是否正确启动了:
php -v
输出结果类似于:
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<br><br><br><span style="font-size: 16px;"><strong><span style="font-size: 14px;">原文转载于:http://cnzhx.net/blog/zendopcache-accelerate-php</span></strong>/ 感谢原著</span>

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Zend Studio 13.0.1
Powerful PHP integrated development environment