PHP Accelerator (XCache), PHP implements lamp in the form of a module

Source: Internet
Author: User
Tags php language php server zts

Introduction to PHP

PHP is a generic server-side scripting language, which is used primarily for web development to implement dynamic Web pages, and is one of the first server-side scripting languages to implement scripts embedded in HTML source documents. PHP also provides a command-line interface, so it can also be used as a standalone shell on most systems.


PHP Zend Engine

Zend Engine is an open source, PHP scripting language interpreter, developed by the C language and highly optimized, and can be used as a backend module for PHP. Zend Engine provides PHP with memory and resource management capabilities as well as other standard services, and its high performance, reliability, and scalability play an important role in promoting PHP as a popular language.

The advent of the Zend engine divides the process of PHP code into two stages: first, parsing the PHP code and converting it into a binary format called Zend opcode (Java-like bytecode) and storing it in memory; the second stage is using Zend Engine to perform these converted opcode.


php's opcode

OpCode is a PHP script-compiled intermediate language, like Java's bytecode, or. Net of MSL. PHP executes PHP script code generally through the following 4 steps (to be exact, it should be PHP language engine Zend):

1, scanning (lexing) ( scan )--Convert PHP code to language fragments (Tokens)

2. Parsing ( analysis )-converts tokens into simple and meaningful expressions

3, compilation ( compile )--compile the expression into Opocdes

4, Execution ( execution )--execute opcodes sequentially, one at a time, thus realizing the function of PHP script


Accelerator for PHP

The PHP process (corresponding to a request) compiled results can not be used by the second PHP process (opcode cannot be shared), which makes every request for dynamic pages need to be scanned, analyzed, compiled, executed, even the exact same request also need to go through the 4 steps. Then there are a variety of PHP accelerators.

PHP Accelerator is a special extension mechanism based on PHP, such as opcode cache extension, you can also cache opcode in PHP shared memory, so that the same piece of code to skip the compilation phase of subsequent execution to improve performance. It can also be seen that these accelerators do not really improve the speed of the opcode, but only by analyzing the opcode and rearranging them to achieve fast execution of the target. Common PHP accelerators are: apc ( Alternative PHP Cache), Eaccelerator,xcache,xcache Fast and stable, rigorously tested and heavily used in production environments. Project address: http ://xcache.lighttpd.net/


Installation of XCache

The installed version is xcache-3.1.0.tar.bz2

1. Installation

[[email protected] ~]# lltotal 20532.....-rw-r--r--.   1 root root    146444 Jul  5 10:41  Xcache-3.1.0.tar.bz2 ..... [[email protected] ~]# tar xf xcache-3.1.0.tar.bz2 [[email protected]  ~]# cd xcache-3.1.0[[email protected] xcache-3.1.0]# /usr/local/php-5.4/bin/phpize    #准备一个模块以实现编译php支持                                          #第三方模块 (integration with current PHP) Configuring for:php api version:          20100412Zend Module Api No:       20100525zend extension api no:   220100525 

/usr/local/php-5.4/bin/phpize This step must be performed, the Configure script file will be available in the directory of the installation package after execution is completed.


[Email protected] xcache-3.1.0]#/configure--enable-xcache--with-php-config=/usr/local/php-5.4/bin/php-config ....... [[email protected] xcache-3.1.0]# make && make install

At the end of the installation, a line similar to the following appears:

Installing shared extensions:/usr/local/php-5.4/lib/php/extensions/no-debug-non-zts-20100525


2. Edit PHP.ini, integrate PHP and XCache

The sample configuration provided by XCache is first imported into the php.ini

[[email protected] xcache-3.1.0]# Mkdir/etc/php.d[[email protected] xcache-3.1.0]# CP XCACHE.INI/ETC/PHP.D

Description: The Xcache.ini file is in the XCache source directory.


Next edit/etc/php.d/xcache.ini, locate the line that begins with zend_extension, and modify it to the following line:

Extension =/usr/local/php-5.4/lib/php/extensions/no-debug-non-zts-20100525/xcache.so

Note: If you have more than one zend_extension line in the php.ini file, make sure that this new row is ranked first.

xcache.ini a few parameters:

; to enable : xcache.size=64m etc  (any size > 0)  and  your system mmap allowsxcache.size  =                60M                                #用于缓存的内存大小; set to cpu count  (cat /proc/cpuinfo |grep -c  processor) xcache.count =                  1                               # Number of cores set to CPU;  just a hash hints, you can always store count (items)  > slotsxcache.slots =                8k;  ttl of the cache item, 0=foreverxcache.ttl   =                  0; interval of gc  scanning expired items, 0=no scan, other values is in  Secondsxcache.gc_interval =           0

Center Load service after completion

[Email protected] xcache-3.1.0]# service php-fpm restartgracefully shutting down php-fpm. Donestarting PHP-FPM Done


To access the configuration page:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6F/53/wKioL1WZN_rA4DWkAAArv60yvhQ640.jpg "style=" float: none; "Title="%9WPNUKE3[M0[S4~UVW (x%c.png "alt=" Wkiol1wzn_ra4dwkaaarv60yvhq640.jpg "/>

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6F/55/wKiom1WZNi-gmJl2AAJiGnxx2A4544.jpg "style=" float: none; "Title=" O{j (@d~j1_bqm2i4%s8qbi7.png "alt=" Wkiom1wzni-gmjl2aajignxx2a4544.jpg "/>

Loaded XCache


Using the AB command to load the PHP server, the command format is as follows

ab-n num-c num URL

-N #共多少次请求

-C #并发请求数


Turn off the cache function first:

[[Email protected] php.d]# mv Xcache.ini xcache.ini.bak[[email protected] php.d]# service PHP-FPM restartgracefully Shutt ing down php-fpm. Donestarting PHP-FPM Done

To start the test:

[[email protected] ~]# ab -n 200 -c 5  http://admin.xiaoxiao.com/index.phpthis is apachebench, version 2.3 <$ Revision: 1554214 $>copyright 1996 adam twiss, zeus technology ltd,  http://www.zeustech.net/licensed to the apache software foundation, http:/ /www.apache.org/benchmarking admin.xiaoxiao.com  (be patient) completed 100  Requestscompleted 200 requestsfinished 200 requests 

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6F/55/wKiom1WZPoGi_KG-AAKLYc7ewvk177.jpg "title=" 1.png " alt= "Wkiom1wzpogi_kg-aaklyc7ewvk177.jpg"/>


Start XCache:

[[Email protected] php.d]# mv Xcache.ini.bak xcache.ini[[email protected] php.d]# service PHP-FPM restartgracefully Shutt ing down php-fpm. Donestarting php-fpm done[[email protected] ~]# ab-n 500-c-http://admin.xiaoxiao.com/index.php ...

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6F/53/wKioL1WZQJqSRXKIAAKHAnH1gLA231.jpg "title=" 2.png " alt= "Wkiol1wzqjqsrxkiaakhanh1gla231.jpg"/>

Speed is twice times more, the effect is quite obvious ~ ~

.................^_^




PHP Accelerator (XCache), PHP implements lamp in the form of a module

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.