Home Backend Development PHP Tutorial php加速器(XCache),php以模块的形式实现LAMP

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

Jun 23, 2016 pm 01:31 PM

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
Copy after login

/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
Copy after login

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

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


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
Copy after login

说明: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
Copy after login

注意:如果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
Copy after login

完成之后中心加载服务

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


访问配置页面:

已加载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
Copy after login

开始测试:

[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
Copy after login


启动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.........
Copy after login

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

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




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

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
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)

Hot Topics

Java Tutorial
1672
14
PHP Tutorial
1277
29
C# Tutorial
1257
24
Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Apr 17, 2025 am 12:06 AM

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values ​​to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

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.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 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: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

How do you prevent SQL Injection in PHP? (Prepared statements, PDO) How do you prevent SQL Injection in PHP? (Prepared statements, PDO) Apr 15, 2025 am 12:15 AM

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.

PHP: Handling Databases and Server-Side Logic PHP: Handling Databases and Server-Side Logic Apr 15, 2025 am 12:15 AM

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

PHP's Purpose: Building Dynamic Websites PHP's Purpose: Building Dynamic Websites Apr 15, 2025 am 12:18 AM

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 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.

See all articles