PHP Service Cache Acceleration Optimization Combat

Source: Internet
Author: User
Tags apc fpm imagemagick php language php and mysql php database php memcached php software

A PHP Cache acceleration Introduction and Environment Introduction 1 PHP Cache acceleration introduction 1 opcode

The opcode is a php-compiled intermediate file, which is a byte code.
Nginx concurrency max, php concurrency second hundreds of, MySQL minimum hundreds of, want to increase the concurrency of the site, the first thing to optimize is PHP and MySQL.
PHP parsing on the server in the parsing of the PHP language will generate a specific opcode, by default, PHP will delete the code after the execution of the program, and the principle of opcode cache is to save the compiled opcode, and put into the shared memory, so that the next time you invoke the PHP page to re-use him, Avoids repetitive compilation of the same code, saves PHP engine time to compile repeatedly, reduces server load, and reduces CPU and memory overhead

2 php opcode caching schematic diagram

The biggest bottleneck for internet companies is PHP and MySQL bottlenecks
Process Description:
1 Nginx accepts client's PHP program Access request
2 Nginx Pass PHP program request to parsing PHP fcgi process (PHP-FPM) according to filtering rules such as extension
3 PHP fpm Process calls PHP parser to read PHP file on site disk and load into memory
4 PHP parser compiles the PHP program into a opcode file and then caches the opcode.
5 PHP fpm engine after executing opcode, return data to Nginx, and return to the client
6 Nginx When a new PHP request is received, the PHP fpm engine reads the opcode in the cache directly and executes it, returning the result.

3 Types of PHP Accelerators

1 XCache
2 Eaccelerator
4 Zendopcache
Choose the way: through their own platform to play a stress test, choose their most suitable

1 preferred XCache, the reason is the efficiency is good, fast, community active support higher version
2 times selected Eaccelerator, the reason, the installation configuration is simple, the acceleration effect is higher, the document material is many, but the official inactive, only is suitable for PHP version 5.4 below the program
3 re-election is Zendopcache, the reason is the official value of PHP, the development potential is good, PHP5.5 can be installed through the Zendopcache software in the form of plug-in extension, from the PHP5.5 version has been integrated into the PHP software, compile only need to specify the relevant parameters
4 APC Understanding Content

Two experimental environment construction

Links: Https://
Password: ec45

1 Installing and configuring PHP

2 Installing and configuring Nginx

3 Configuring Nginx Support PHP

4 Testing

Installation of the three-cache accelerator 1 Eaccelerator install and load 1 eaccelerator cache acceleration Software Description

Free, open source php acceleration, optimization and cache extension software, can cache PHP program compiled by the intermediate code opcode, session data, etc., reduce the PHP program in the compilation of the server performance overhead, it can also speed up the execution of PHP programs, reduce server load pressure , PHP program code execution is more efficient.

2 Fundamentals

Eaccelerator will store the compiled PHP program in shared memory. The execution is then invoked every time from memory.

2 XCache Install and load 1 XCache cache acceleration Software Description

Open source, stable php opcode cache optimizer.

2 principle

XCache the PHP program compiled data opcode cached in the shared memory, the same surface of the program repeated execution. When users request data, they can use the data in the cache directly, increasing the speed of access to PHP, usually up to 2-5 times.

3 Znedopcache Installation 1 Zendopcache plugin description

is the PHP official to adapt to the market needs of a software development, before PHP5.5, need to manually compile, but after 5.5, only need to compile PHP when the time to add--enable-opcache on the line.

4 installation of the PHP memcached extension 1 memcached cache flowchart

2 memcached work steps are as follows:

1 The program first checks whether the data requested by the client is present in the cache of the memcached service and, if present, returns the requested data directly and no longer requests the backend database
2 If the requested data does not exist in the memcached cache, the program will go to the Memcached backend database Server service
3 returning data from the database to the client
4 at the same time, the new data is cached in the memcached server cache, the next time the same request directly from the Memcached service cache to return data, thereby reducing the database access pressure

5 Installing the plug-in Pdo_mysql expansion module

PDO defines a lightweight, consistent interface for the PHP Access database, which provides a data access abstraction layer so that queries and data can be executed through consistent functions, regardless of the database used.

If it is not installed,

6 Installing the graphical expansion Module 1 installing the dependent modules

2 Installing the Unpacking package

3 Installing ImageMagick

is a powerful and stable free toolset and development package that can be used to read, write, and process image files in more than 89 basic formats.

4 Installing Imagick

The Imagick plugin requires the support of the ImageMagick software, so the above software must be installed first.
The Imagick plugin is an extension module that provides PHP calls to ImageMagick functionality, which allows PHP to have the same functionality as ImageMagick.


7 Basic Summary

The function in this module is consistent, and only one can be installed at a time

The functionality of the modules in this is consistent. To achieve the same functionality

This module is used to link the database

Configure how the above modules are in effect, and if there is/ETC/PHP.D add

Its default read file

If no/etc/php.d is added by adding the php.ini

If not configured here. The following module specifies the absolute path of the module, and if configured, specifies the relative path of the module, where its path is the path generated when the Make install module

If you add a reboot is required to complete, I've configured it here, so I can view it directly

Quad Cache Accelerator Optimization Combat 1 Configuration Eaccelerator effective 1 basic configuration and loading

2 parameter-dependent configuration

Method One, which is configured without/ETC/PHP.D:

Method Two: In the configuration with. etc/php.d

Parameter explanation:
Eaccelerator.shm_size= "64"
The amount of shared memory that Eaccelerator can use (in megabytes). "0" refers to the default value of the operating system. The default value is "0". can be adjusted according to the actual situation of the server, 16,32,64,128 are OK.
Eaccelerator.cache_dir= "/tmp/eaccelerator"
This directory is used for disk caching. Eaccelerator stores pre-compiled code, process data, content, and user-defined content here. The same data can also be stored in shared memory (which can improve access speed). The default setting is "/tmp/eaccelerator".

eaccelerator.enable= "1"
Turn eaccelerator on or off. "1" is on, "0" is off. The default value is "1".

Eaccelerator.optimizer= "1"
Power on or off the internal optimizer to increase code execution speed. "1" is on, "0" is off. The default value is "1".

Eaccelerator.check_mtime= "1"
Open or close the PHP file modification check. "1" means open, "0" means close. If you recompile the PHP file after you modify it, you should set it to "1". The default value is "1".

eaccelerator.debug= "0"
Turn debug logging on or off. "1" is on, "0" is off. The default value is "0". Records are written to the log when the cache is hit.

Eaccelerator.filter= ""
Determine which PHP files must be cached. You can specify cached and non-cached file types (such as ". php . phtml", etc.)
If the argument starts with "!", the file that matches these parameters is ignored by the cache. The default value is "", that is, all PHP files will be cached.

eaccelerator.shm_max= "0"
When you use the "eaccelerator_put ()" function, it is forbidden to store too large files in shared memory. This parameter specifies the maximum value that is allowed to be stored in bytes (10240, 10K, 1M). "0" is not limited. The default value is "0".
Eaccelerator.shm_ttl= "0"
When Eaccelerator gets the shared memory size of a new script fails, it removes all script caches that were not accessed in the last "Shm_ttl" seconds from shared memory. The default value is "0", which is: Do not delete any cache files from the share inner spring.

eaccelerator.shm_prune_period= "0"
When Eaccelerator gets the shared memory size of a new script fails, he tries to remove the cache script that is older than "shm_prune_period" seconds from shared memory. The default value is "0", which is: Do not delete any cache files from the share inner spring.

eaccelerator.shm_only= "0"
Allows or disables the caching of compiled scripts on disk. This option is not valid for session data and content caching. The default value is "0", which is: Use disk and shared memory for caching.

eaccelerator.compress= "1"
Allows or disables the compression of the content cache. The default value is "1", which is: Allow compression.

Eaccelerator.compress_level= "9"
Specifies the compression level of the content cache. The default value is "9", which is the highest level.

3 Creating a write directory and restarting the service

4 TMPFS Optimization:

TMPFS is a memory-based file system that typically uses TMPFS as temporary storage for data, which is much faster than disk storage, and is suitable for a variety of caching scenarios.
Add Tmpfs

2 configuring XCache Plug-in acceleration 1 basic configuration and loading

2 Detailed parameters

Extension =

Login Admin Interface user name and password, leave the admin interface blank
Xcache.admin.enable_auth = On

Manage user Names
Xcache.admin.user = "Xcache-admin"

MD5 encrypted password, # Echo-n Pass | Md5sum
Xcache.admin.pass = ""

Determine how XCache allocates shared memory from the system
Xcache.shm_scheme = "Mmap"

The size of the shared cache used. If 0, the cache will not be able to use
Xcache.size = 60M

Specifies how many blocks the cache is cut into and recommended to be set to the number of CPUs (Cat/proc/cpuinfo |grep-c processor)
Xcache.count = 1

Just as a reference value for the number of hash slots, you can safely cache items that exceed this number.
Xcache.slots = 8K

The lifetime of the opcode file. If this value is set to 0, it will be cached indefinitely (int)
Xcache.ttl = 0

The time interval at which garbage collection is triggered. The default setting is 0 (seconds)
Xcache.gc_interval = 0

Ditto, just for variable cache settings
Xcache.var_size = 4M
Xcache.var_count = 1
Xcache.var_slots = 8K
Xcache.var_ttl = 0
Xcache.var_maxttl = 0
Xcache.var_gc_interval = 300

Enable or disable the test feature
Xcache.test = Off

If Readonlyprotection is enabled, it will slightly degrade performance, but will increase a certain factor of safety.
This option is not valid for Xcache.mmap_path =/dev/zero.
Xcache.readonly_protection = On

File path for read-only protection
This will limit the two PHP process groups to share the same/tmp/xcache directory, which is invalid when specified as/dev/zero
Xcache.mmap_path = "/tmp/xcache"

In the case of a failure, place the core dump directory. Must be a directory that PHP can write to. Leave blank to disable the delegate.
Xcache.coredump_directory = ""

For Windows only
Xcache.coredump_type = 0

Xcache.disable_on_crash = Off
Xcache.experimental = Off

Use/do not use opcode cache. Xcache.size = 0 o'clock is not valid.
Xcache.cacher = On

Check for script updates using stat () discovery.
Xcache.stat = On

Enables or disables optimizations. Default state is disabled
Xcache.optimizer = Off

Enable Code Overlay Information Collector
The Xcache.coveragedump_directory setting is enabled and the Xcache_coverager_start/stop/get/clean () series functions are available. (The impact is reduced when enabled).
Xcache.coverager = Off

Each page request is automatically called Xcache_coverager_start ()
Xcache.coverager_autostart = Off

Location of the directory where data collection information is placed. Use directory/tmp/pcovis by default
Xcache.coveragedump_directory = ""

3 Configuring the Web interface to view XCache cache information

1 Configuring the login user name and password

2 Copy related pages to nginx default publishing directory

4 Log in and view

3 Configuring Zendopencache Optimization 1 Basic configuration and loading

2 Opcache Important parameter description

1 Zend Optimizer + switch, code is no longer optimized when off.

2 Zend Optimizer + shared memory size, total ability to store how much precompiled PHP code (in MB) recommended 128

3 Zend Optimizer + The total memory of the string in the staging pool. (unit: MB) Recommended 8

4 maximum number of cached files 200 to 100000, recommended 4000

5 memory "Wasted" reaches the percentage corresponding to this value, a restart schedule is initiated.

6 Open this command, Zend Optimizer + will automatically append the name of the current working directory to the script key,
This eliminates key-value naming conflicts between files of the same name. Turning this command on will improve performance,
However, it can cause damage to existing applications.

7 Open File Timestamp verification

8 2s Check for file updates Note: 0 is always checked not off, recommended 60

9 Allow or disallow the optimization of file search in Include_path, opcache.revalidate_path=0

10 whether to save comments on file/function if Apigen, Doctrine, ZF2, PHPUnit need file comments, recommend 0

11 whether to load a file/function comment, Opcache.load_comments=1
Turn on quick close, open this will increase the memory speed when PHP Request shutdown will improve the recommended 1

12 allow overwriting of the file existence (file_exists, etc.) optimization features. Opcache.enable_file_override=0

13 Define how many optimization processes to start

14 Enabling this hack can temporarily resolve the "can ' t Redeclare class" error.

15 Enabling this hack can temporarily resolve the "can ' t Redeclare class" error.

16 set non-cached blacklist
PHP files beginning with cache_ in the specified directory are not cached. /png/www/\

17 The cache of large files is removed by the file size screen. All files are cached by default.

18 Check cache checksum per N requests. The default value of 0 indicates that the check is disabled.
Due to the calculation of the checksum lossy performance, this directive should be opened tightly in the development of debugging.

19 after the cache is not accessed, wait for how long (in seconds) to schedule the restart

20 error log file name. Leaving blank means using standard error output (stderr).

21 write error messages to the server (Apache etc) log

22 Preferred background for memory sharing. Leave it blank to make the system select.

23 Prevent accidental writes during script execution within a share, for internal debugging only.

Five summary:

Production Environment PHP extension installation recommendations

PHP EXT Module Description Notes
Eaccelerator For PHP5.3 and previous versions, PHP cache acceleration PHP Optional Extensions
ImageMagick.tar.gz Common image processing program, which belongs to functional application Non-PHP extensions
Imagick-2.3.0.tgz Need to install graphics processing program, is a functional application Optional PHP Plugin
Memcache-2.7.7.tgz memcached Client Database Cache optimization Optional PHP Plugin
Pdo_mysql-1.0.2.tgz PHP Database access plugin, which belongs to the functional application Optional PHP extensions
xcache-3.0.0.tar.bz2 Supports php5.1-5.6,php cache acceleration Optional PHP Plugin
Zendopcache-7.0.5.tgz Supports php5.3-5.4,php cache acceleration Optional PHP Plugin

PHP Service Cache Acceleration Optimization Combat

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: 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.