qq78707 发表于 2018-12-18 14:39:36

php加速器(XCache),php以模块的形式编译

  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、安装
# ll
total 20532
.....
-rw-r--r--.1 root root    146444 Jul5 10:41 xcache-3.1.0.tar.bz2
.....
# tar xf xcache-3.1.0.tar.bz2
# cd xcache-3.1.0
# /usr/local/php-5.4/bin/phpize#准备一个模块以实现编译php支持
                                    #第三方模块(与当前的php整合)
Configuring for:
PHP Api Version:         20100412
Zend Module Api No:      20100525
Zend Extension Api No:   220100525  /usr/local/php-5.4/bin/phpize 这里必须要执行这一步,执行完成之后,安装包的目录下才会有configure脚本文件
  

# ./configure --enable-xcache --with-php-config=/usr/local/php-5.
4/bin/php-config
.......
# 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
# mkdir /etc/php.d
# 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 allows
xcache.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) > slots
xcache.slots =                8K
; ttl of the cache item, 0=forever
xcache.ttl   =               0
; interval of gc scanning expired items, 0=no scan, other values is in seconds
xcache.gc_interval =         0  完成之后中心加载服务
# service php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpmdone  
  

  访问配置页面:
http://s3.运维网.com/wyfs02/M00/6F/53/wKioL1WZN_rA4DWkAAArv60yvhQ640.jpg
http://s3.运维网.com/wyfs02/M00/6F/55/wKiom1WZNi-gmJl2AAJiGnxx2A4544.jpg
  已加载xcache

  

  用ab命令对php服务器进行压测,命令格式如下
  ab -n num -c num url
  -n   #共多少次请求
  -c    #并发请求数
  

  先把缓存功能关掉:
# mv xcache.ini xcache.ini.bak
# service php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpmdone  开始测试:
# ab -n 200 -c 5 http://admin.xiaoxiao.com/index.php
This is ApacheBench, Version 2.3
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 requests
Completed 200 requests
Finished 200 requests  http://s3.运维网.com/wyfs02/M00/6F/55/wKiom1WZPoGi_KG-AAKLYc7ewvk177.jpg
  

  启动xcache:
# mv xcache.ini.bak xcache.ini
# service php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpmdone
# ab -n 500 -c 20 http://admin.xiaoxiao.com/index.php
.........  http://s3.运维网.com/wyfs02/M01/6F/53/wKioL1WZQJqSRXKIAAKHAnH1gLA231.jpg
  速度是两倍多一点,效果还是挺明显的~~
  .................^_^
  

  php以模块的形式工作
  php以模块的形式与httpd整合,两者只能运行在同一台服务器上。
  首先解决依赖关系,安装libxml2-devel,bzip2-devel,libmcrypt-devel。若需要添加GD库,libjpeg-turbo-devel,libpng-devel,freetype-devel,把这3个包也装上,然后进行编译。
# ./configure --prefix=/usr/local/php-5.4 --with-mysql=mysqlnd
--with-pdo-mysql=mysqlnd --with-openssl --with-mysqli=mysqlnd --enable-mbstring --wit
h-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml
   --enable-sockets --with-apxs2=/usr/local/apache-2.4.9/bin/apxs --with-mcrypt--with-con
   fig-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --with-gd --enable-ma
   intainer-zts# make && make install  --enable-maintainer-zts             #如果httpd使用的mpm为event或者worker,这项一定要加上,
                                                      #若果使用的是prefork,这项一定不能加
  --with-apxs2=/usr/local/apache/bin/apxs       #apx是httpd的钩子函数,它能够实现为apache编
                                                                          #译第三方模块,php要编译成apache模块,需要指
                                                                                   #定函数的位置
  添加配置文件
# cp php.ini-production /etc/php.ini  编辑apache配置文件httpd.conf,使httpd支持php
  添加如下两行

   AddType application/x-httpd-php.php      #一般情况下加这一行就行
   AddType application/x-httpd-php-source.phps  识别默认页面

    DirectoryIndex index.php index.html
  添加测试页面,后重新加载httpd服务

# vim /usr/local/apache-2.4.9/htdocs/index.php

.......
# service httpd force-reload
Reloading httpd:http://s3.运维网.com/wyfs02/M00/71/17/wKiom1XFRdOTsPJ9AAMHOssPh0o350.jpg
  php模块已能够正常工作.................^_^
  




页: [1]
查看完整版本: php加速器(XCache),php以模块的形式编译