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

Source: Internet
Author: User
Tags 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 (compilation)--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 the purpose of fast execution. Common PHP accelerators are: APC (alternative PHP Cache), Eaccelerator,xcache,nusphere phpexpress,zend Optimizer and Zend Guard Loader ........ The XCache is 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

[Root@www ~]# lltotal 20532.....-rw-r--r--.  1 root root    146444 Jul  5 10:41 xcache-3.1.0.tar.bz2 ..... [Root@www ~]# tar xf xcache-3.1.0.tar.bz2 [root@www ~]# cd xcache-3.1.0[root@www 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.


[Root@www xcache-3.1.0]#./configure--enable-xcache--with-php-config=/usr/local/php-5.4/bin/php-config .... [Root@www 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

[Root@www xcache-3.1.0]# mkdir/etc/php.d[root@www 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.

Several parameters in the Xcache.ini:

;  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                              #设置成cpu的核心数; Just a hash hints, can alway S 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 are in Secondsxcache.gc_interval =           0

Center Load service after completion

[root@www xcache-3.1.0]# service php-fpm restartgracefully shutting down php-fpm. Donestarting php-fpm Done  


To access the configuration page:

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:

[Root@www php.d]# MV Xcache.ini xcache.ini.bak[root@www php.d]# service php-fpm restartgracefully shutting down php-fpm. Donestarting php-fpm Done  

To start the test:

[Root@www ~]# ab-n 200-c 5 http://admin.xiaoxiao.com/index.phpThis is apachebench, Version 2.3 < $Revision: 1554214 $& Gt 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 requestscompleted 200 Requests


Start XCache:

[Root@www php.d]# MV Xcache.ini.bak xcache.ini[root@www php.d]# service php-fpm restartgracefully shutting down php-fpm. Donestarting php-fpm  done[root@www ~]# ab-n 500-c .....

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

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

  • Related Article

    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.