Table of Contents
说明
PHP开启opcache方法
测试
文章参考
Home php教程 php手册 [PHP] - 性能加速 - 开启opcache

[PHP] - 性能加速 - 开启opcache

Jun 12, 2016 pm 08:06 PM
opcache php code Open source performance programming programming language software development

说明

PHP 5.5+版本以上的,可以使用PHP自带的opcache开启性能加速(默认是关闭的)。对于PHP 5.5以下版本的,需要使用APC加速,这里不说明,可以自行上网搜索PHP APC加速的方法。

 


 

PHP开启opcache方法

1、打开php.ini文件

2、找到:[opcache],设置为:

<span style="color: #800000; font-weight: bold;">[</span><span style="color: #800000;">opcache</span><span style="color: #800000; font-weight: bold;">]</span>
<span style="color: #008000;">;</span><span style="color: #008000;"> dll地址</span>
zend_extension=<span style="color: #000000;">php_opcache.dll
</span><span style="color: #008000;">;</span><span style="color: #008000;"> 开关打开</span>
opcache.enable=1
<span style="color: #008000;">;</span><span style="color: #008000;"> 开启CLI</span>
opcache.enable_cli=1
<span style="color: #008000;">;</span><span style="color: #008000;"> 可用内存, 酌情而定, 单位为:Mb</span>
opcache.memory_consumption=528
<span style="color: #008000;">;</span><span style="color: #008000;"> Zend Optimizer + 暂存池中字符串的占内存总量.(单位:MB)</span>
opcache.interned_strings_buffer=8
<span style="color: #008000;">;</span><span style="color: #008000;"> 对多缓存文件限制, 命中率不到 100% 的话, 可以试着提高这个值</span>
opcache.max_accelerated_files=10000
<span style="color: #008000;">;</span><span style="color: #008000;"> Opcache 会在一定时间内去检查文件的修改时间, 这里设置检查的时间周期, 默认为 2, 定位为秒</span>
opcache.revalidate_freq=1
<span style="color: #008000;">;</span><span style="color: #008000;"> 打开快速关闭, 打开这个在PHP Request Shutdown的时候回收内存的速度会提高</span>
opcache.fast_shutdown=1
Copy after login

 

3、重启apache即可。


 

测试

配置完成后,可以使用如下代码查询opcache:

<span style="color: #000000;">php
    </span><span style="color: #008080;">phpinfo</span><span style="color: #000000;">();
</span>?>
Copy after login

访问页面得到如下界面:

 


 

 

以下是opcache的配置说明:

<span style="color: #800000; font-weight: bold;">[</span><span style="color: #800000;">opcache</span><span style="color: #800000; font-weight: bold;">]</span><span style="color: #000000;">
zend_extension </span>= "G:/PHP/php-5.5.6-Win32-VC11-x64/ext/php_opcache.dll"
 
<span style="color: #008000;">;</span><span style="color: #008000;"> Zend Optimizer + 的开关, 关闭时代码不再优化.</span>
opcache.enable=1
 
<span style="color: #008000;">;</span><span style="color: #008000;"> Determines if Zend OPCache is enabled for the CLI version of PHP</span>
opcache.enable_cli=1
 
 
<span style="color: #008000;">;</span><span style="color: #008000;"> Zend Optimizer + 共享内存的大小, 总共能够存储多少预编译的 PHP 代码(单位:MB)</span><span style="color: #008000;">
;</span><span style="color: #008000;"> 推荐 128</span>
opcache.memory_consumption=64
 
<span style="color: #008000;">;</span><span style="color: #008000;"> Zend Optimizer + 暂存池中字符串的占内存总量.(单位:MB)</span><span style="color: #008000;">
;</span><span style="color: #008000;"> 推荐 8</span>
opcache.interned_strings_buffer=4
 
 
<span style="color: #008000;">;</span><span style="color: #008000;"> 最大缓存的文件数目 200  到 100000 之间</span><span style="color: #008000;">
;</span><span style="color: #008000;"> 推荐 4000</span>
opcache.max_accelerated_files=2000
 
<span style="color: #008000;">;</span><span style="color: #008000;"> 内存“浪费”达到此值对应的百分比,就会发起一个重启调度.</span>
opcache.max_wasted_percentage=5
 
<span style="color: #008000;">;</span><span style="color: #008000;"> 开启这条指令, Zend Optimizer + 会自动将当前工作目录的名字追加到脚本键上,</span><span style="color: #008000;">
;</span><span style="color: #008000;"> 以此消除同名文件间的键值命名冲突.关闭这条指令会提升性能,</span><span style="color: #008000;">
;</span><span style="color: #008000;"> 但是会对已存在的应用造成破坏.</span>
opcache.use_cwd=0
 
 
<span style="color: #008000;">;</span><span style="color: #008000;"> 开启文件时间戳验证 </span>
opcache.validate_timestamps=1
 
 
<span style="color: #008000;">;</span><span style="color: #008000;"> 2s检查一次文件更新 注意:0是一直检查不是关闭</span><span style="color: #008000;">
;</span><span style="color: #008000;"> 推荐 60</span>
opcache.revalidate_freq=2
 
<span style="color: #008000;">;</span><span style="color: #008000;"> 允许或禁止在 include_path 中进行文件搜索的优化</span><span style="color: #008000;">
;</span><span style="color: #008000;">opcache.revalidate_path=0</span>
 
 
<span style="color: #008000;">;</span><span style="color: #008000;"> 是否保存文件/函数的注释   如果apigen、Doctrine、 ZF2、 PHPUnit需要文件注释</span><span style="color: #008000;">
;</span><span style="color: #008000;"> 推荐 0</span>
opcache.save_comments=1
 
<span style="color: #008000;">;</span><span style="color: #008000;"> 是否加载文件/函数的注释</span><span style="color: #008000;">
;</span><span style="color: #008000;">opcache.load_comments=1</span>
 
 
<span style="color: #008000;">;</span><span style="color: #008000;"> 打开快速关闭, 打开这个在PHP Request Shutdown的时候会收内存的速度会提高</span><span style="color: #008000;">
;</span><span style="color: #008000;"> 推荐 1</span>
opcache.fast_shutdown=1
 
<span style="color: #008000;">;</span><span style="color: #008000;">允许覆盖文件存在(file_exists等)的优化特性。</span><span style="color: #008000;">
;</span><span style="color: #008000;">opcache.enable_file_override=0</span>
 
 
<span style="color: #008000;">;</span><span style="color: #008000;"> 定义启动多少个优化过程</span><span style="color: #008000;">
;</span><span style="color: #008000;">opcache.optimization_level=0xffffffff</span>
 
 
<span style="color: #008000;">;</span><span style="color: #008000;"> 启用此Hack可以暂时性的解决”can’t redeclare class”错误.</span><span style="color: #008000;">
;</span><span style="color: #008000;">opcache.inherited_hack=1</span>
 
<span style="color: #008000;">;</span><span style="color: #008000;"> 启用此Hack可以暂时性的解决”can’t redeclare class”错误.</span><span style="color: #008000;">
;</span><span style="color: #008000;">opcache.dups_fix=0</span>
 
<span style="color: #008000;">;</span><span style="color: #008000;"> 设置不缓存的黑名单</span><span style="color: #008000;">
;</span><span style="color: #008000;"> 不缓存指定目录下cache_开头的PHP文件. /png/www/example.com/public_html/cache/cache_ </span><span style="color: #008000;">
;</span><span style="color: #008000;">opcache.blacklist_filename=</span>
 
 
<span style="color: #008000;">;</span><span style="color: #008000;"> 通过文件大小屏除大文件的缓存.默认情况下所有的文件都会被缓存.</span><span style="color: #008000;">
;</span><span style="color: #008000;">opcache.max_file_size=0</span>
 
<span style="color: #008000;">;</span><span style="color: #008000;"> 每 N 次请求检查一次缓存校验.默认值0表示检查被禁用了.</span><span style="color: #008000;">
;</span><span style="color: #008000;"> 由于计算校验值有损性能,这个指令应当紧紧在开发调试的时候开启.</span><span style="color: #008000;">
;</span><span style="color: #008000;">opcache.consistency_checks=0</span>
 
<span style="color: #008000;">;</span><span style="color: #008000;"> 从缓存不被访问后,等待多久后(单位为秒)调度重启</span><span style="color: #008000;">
;</span><span style="color: #008000;">opcache.force_restart_timeout=180</span>
 
<span style="color: #008000;">;</span><span style="color: #008000;"> 错误日志文件名.留空表示使用标准错误输出(stderr).</span><span style="color: #008000;">
;</span><span style="color: #008000;">opcache.error_log=</span>
 
 
<span style="color: #008000;">;</span><span style="color: #008000;"> 将错误信息写入到服务器(Apache等)日志</span><span style="color: #008000;">
;</span><span style="color: #008000;">opcache.log_verbosity_level=1</span>
 
<span style="color: #008000;">;</span><span style="color: #008000;"> 内存共享的首选后台.留空则是让系统选择.</span><span style="color: #008000;">
;</span><span style="color: #008000;">opcache.preferred_memory_model=</span>
 
<span style="color: #008000;">;</span><span style="color: #008000;"> 防止共享内存在脚本执行期间被意外写入, 仅用于内部调试.</span><span style="color: #008000;">
;</span><span style="color: #008000;">opcache.protect_memory=0</span>
Copy after login

 

文章参考

使用 OpCache 提升 PHP 5.5+ 程序性能

https://phphub.org/topics/301

php opcache 配置

https://blog.hackroad.com/operations-engineer/windows_server/9311.html

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

The Key to Coding: Unlocking the Power of Python for Beginners The Key to Coding: Unlocking the Power of Python for Beginners Oct 11, 2024 pm 12:17 PM

Python is an ideal programming introduction language for beginners through its ease of learning and powerful features. Its basics include: Variables: used to store data (numbers, strings, lists, etc.). Data type: Defines the type of data in the variable (integer, floating point, etc.). Operators: used for mathematical operations and comparisons. Control flow: Control the flow of code execution (conditional statements, loops).

Problem-Solving with Python: Unlock Powerful Solutions as a Beginner Coder Problem-Solving with Python: Unlock Powerful Solutions as a Beginner Coder Oct 11, 2024 pm 08:58 PM

Pythonempowersbeginnersinproblem-solving.Itsuser-friendlysyntax,extensivelibrary,andfeaturessuchasvariables,conditionalstatements,andloopsenableefficientcodedevelopment.Frommanagingdatatocontrollingprogramflowandperformingrepetitivetasks,Pythonprovid

See all articles