search
HomeBackend DevelopmentPHP Tutorialphp加速器(XCache),php以模块的形式实现LAMP

PHP简介

PHP是通用服务器端脚本编程语言,其主要用于web开发以实现动态web页面,它也是最早实现将脚本嵌入HTML源码文档中的服务器端脚本语言之一。同时,php还提供了一个命令行接口,因此,其也可以在大多数系统上作为一个独立的shell来使用。


PHP Zend Engine

Zend Engine是开源的、PHP脚本语言的解释器,由C语言开发且经过高度优化,并能够做为PHP的后端模块使用。Zend Engine为PHP提供了内存和资源管理的功能以及其它的一些标准服务,其高性能、可靠性和可扩展性在促进PHP成为一种流行的语言方面发挥了重要作用。

Zend Engine的出现将PHP代码的处理过程分成了两个阶段:首先是分析PHP代码并将其转换为称作Zend opcode的二进制格式(类似Java的字节码),并将其存储于内存中;第二阶段是使用Zend Engine去执行这些转换后的Opcode。


PHP的Opcode

Opcode是一种PHP脚本编译后的中间语言,就像Java的ByteCode,或者.NET的MSL。PHP执行PHP脚本代码一般会经过如下4个步骤(确切的来说,应该是PHP的语言引擎Zend):

1、Scanning(Lexing)(扫描) ―― 将PHP代码转换为语言片段(Tokens)

2、Parsing(分析) ―― 将Tokens转换成简单而有意义的表达式

3、Compilation(编译) ―― 将表达式编译成Opocdes

4、Execution(执行) ―― 顺次执行Opcodes,每次一条,从而实现PHP脚本的功能


php的加速器

PHP进程(对应一次请求)编译的结果无法被第二个PHP进程使用(opcode无法共享),这使得每一次对动态页面的请求都需要进行扫描,分析,编译,执行,即使是一模一样的请求也需要也需要经历这4个步骤。然后就有了各种PHP加速器。

php的加速器是基于PHP的特殊扩展机制,如opcode缓存扩展,也可以将opcode缓存于php的共享内存中,从而可以让同一段代码的后续重复执行时跳过编译阶段以提高性能。由此也可以看出,这些加速器并非真正提高了opcode的运行速度,而仅是通过分析opcode后并将它们重新排列以达到快速执行的目的。常见的php加速器有:APC (Alternative PHP Cache),eAccelerator,XCache,NuSphere PhpExpress,Zend Optimizer和Zend Guard Loader........其中XCache快速而且稳定,经过严格测试且被大量用于生产环境。项目地址:http://xcache.lighttpd.net/


XCache的安装

安装的版本是xcache-3.1.0.tar.bz2

1、安装

[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支持                                      #第三方模块(与当前的php整合)Configuring for:PHP Api Version:         20100412Zend Module Api No:      20100525Zend Extension Api No:   220100525

/usr/local/php-5.4/bin/phpize 这里必须要执行这一步,执行完成之后,安装包的目录下才会有configure脚本文件


[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

安装结束时,会出现类似如下行:

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


2、编辑php.ini,整合php和xcache

首先将xcache提供的样例配置导入php.ini

[root@www xcache-3.1.0]# mkdir /etc/php.d[root@www xcache-3.1.0]# cp xcache.ini /etc/php.d

说明:xcache.ini文件在xcache的源码目录中。


接下来编辑/etc/php.d/xcache.ini,找到zend_extension开头的行,修改为如下行:

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

注意:如果php.ini文件中有多条zend_extension指令行,要确保此新增的行排在第一位。

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, 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

完成之后中心加载服务

[root@www xcache-3.1.0]# service php-fpm restartGracefully shutting down php-fpm . doneStarting php-fpm  done


访问配置页面:

已加载xcache


用ab命令对php服务器进行压测,命令格式如下

ab -n num -c num url

-n   #共多少次请求

-c    #并发请求数


先把缓存功能关掉:

[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

开始测试:

[root@www ~]# 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


启动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 20 http://admin.xiaoxiao.com/index.php.........

速度是两倍多一点,效果还是挺明显的~~

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




Statement
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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

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: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

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 and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

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 and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

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.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

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 and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

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.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

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

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

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.

See all articles

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

Video Face Swap

Video Face Swap

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

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

mPDF

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),

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment