前言:
看了很多别人对xcache理解,稍微总结了一下,咱们就知道有这么一个PHP缓存加速的工具xcache,他的作用就是可以提高高并发时的访问php网页性能,提高动态web的处理能力,优化用户体验,减少服务器负担,减少cpu内存负担等之类的各种好词怼上就哦了0.0
至于原理吗?(复制官方的话,嘻嘻)
当客户请求一个PHP程序时,服务器的PHP引擎会解析该PHP程序,并将其编译为特定的操作码(Operate Code,简称opcode文件),该文件是执行PHP代码后的一种二进制的表示形式。默认情况下,这个编译好的的操作码文件由PHP引擎执行后丢弃。而操作码缓存(opcode cache)的原理就是将编译后的操作码报错下来,并放到共享内存里,以便在下次调用PHP页面时重用它,它避免了相同代码的重复编译,我觉得有点像三层交换机的原理:一次路由,多次转发。纯属个人意见,不对的话大佬们可以继续补充。
以LAMP环境为例,建议结合着看 可以参考博文:https://mp.csdn.net/console/editor/html/105031305
在安装xcache之前,先要安装LAMP环境,在前面我们也配置好了,安装版本如下
项目 | 版本 |
Linux | CentOS 7.2 |
apache | apache-2.4.25 |
mysql | mysql-5.6.36 |
php | php-5.6.27 |
说明:
安装xcache缓存加速器:
因为我们用的PHP版本为5.6,通过Xcache官网可以了解到需要用到3.20的版本才能支持PHP5.6,可以到官网下载。http://xcache.lighttpd.net/
安装xcache
wget http://xcache.lighttpd.net/pub/Releases/3.2.0/xcache-3.2.0.tar.gz ##下载
解压预编译and安装
[root@phpserver ~]tar zxf xcache-3.2.0.tar.gz #解压
[root@phpserver ~] cd xcache-3.2.0/ #进入安装目录
[root@phpserver xcache-3.2.0]/usr/local/php5.6/bin/phpize #用 phpize 生成 configure 配置文件
[root@phpserver xcache-3.2.0]./configure --enable-xcache --enable-xcache-coverager --enable-xcache-optimizer --with-php-config=/usr/local/php5.6/bin/php-config
[root@phpserver xcache-3.2.0]make && make install #编译、安装
如果报错configure不能编译之类的
[root@php xcache-3.2.0] ./configure --enable-xcache --enable-xcache-coverager --enable-xcache-optimizer --with-php-config=/usr/local/php5.6/bin/php-config
bash: ./configure: No such file or directory
留意上一步执行/usr/local/php5.6/bin/phpize 回复的结果:Cannot find autoconf
[root@php xcache-3.2.0]# /usr/local/php5.6/bin/phpize
Configuring for:
PHP Api Version: 20131106
Zend Module Api No: 20131226
Zend Extension Api No: 220131226
Cannot find autoconf. Please check your autoconf installation and the
$PHP_AUTOCONF environment variable. Then, rerun this script.
那我们就yum -y install autoconf 解决问题
创建 xcache 缓存文件
touch /tmp/xcache
chmod 777 /tmp/xcache
拷贝 xcache 后台管理程序到网站根目录
[root@phpserver xcache-3.2.0] cp -r htdocs/ /var/www/benet/xcache
配置 php 支持 xcache
vi / etc/php.ini #编辑配置文件,在最后一行添加以下内容
保存退出后 service php-fpm restart
[xcache-common]
extension = /usr/local/php5.6/lib/php/extensions/no-debug-zts-20131226/xcache.so #加载xcache模块,前面一大推是安装完自动生成的
[xcache.admin]
xcache.admin.enable_auth = Off
[xcache]
xcache.shm_scheme ="mmap" # 设置xcache如何从系统分配共享内存
xcache.size=60M #设置xcache缓存的大小值
xcache.count =1
xcache.slots =8K
xcache.ttl=0 #设定cache对象的生存期TTL,0为永不过期
xcache.gc_interval =0
xcache.var_size=64M
xcache.var_count =1
xcache.var_slots =8K
xcache.var_ttl=0
xcache.var_maxttl=0
xcache.var_gc_interval =300
xcache.test =Off
xcache.readonly_protection = Off
xcache.mmap_path ="/tmp/xcache"
xcache.coredump_directory =""
xcache.cacher =On
xcache.stat=On
xcache.optimizer =Off
[xcache.coverager]
xcache.coverager =On
xcache.coveragedump_directory =""
将 xcache 目录拷贝到 apache 主机的网页文档目录下
[root@phpserver ~] scp -r /var/www/benet/xcache/ root@192.168.10.3:/var/www/benet/
测试
![](https://i-blog.csdnimg.cn/blog_migrate/5844453b952235fbe1777d1a37276be8.png)
至此,Linux 下安装 php 加速软件 Xcache 完成
执行 ab 压力测试:
第一次测试:
[root@mysql ~]# ab -c 100 -n 10000 http://192.168.10.3/index.php
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 192.168.10.3 (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests
Server Software: Apache/2.4.25
Server Hostname: 192.168.10.3
Server Port: 80
Document Path: /index.php
Document Length: 84894 bytes
Concurrency Level: 100
Time taken for tests: 28.734 seconds
Complete requests: 10000
Failed requests: 1032
(Connect: 0, Receive: 0, Length: 1032, Exceptions: 0)
Write errors: 0
Total transferred: 850648843 bytes
HTML transferred: 848938843 bytes
Requests per second: 348.02 [#/sec] (mean)
Time per request: 287.342 [ms] (mean)
Time per request: 2.873 [ms] (mean, across all concurrent requests)
Transfer rate: 28910.23 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 1 1.1 0 24
Processing: 21 286 147.0 276 3108
Waiting: 15 114 139.0 57 2916
Total: 22 287 147.0 277 3109
Percentage of the requests served within a certain time (ms)
50% 277
66% 300
75% 318
80% 330
90% 368
95% 402
98% 450
99% 508
100% 3109 (longest request)
第二次测试:
[root@mysql ~]# ab -c 100 -n 10000 http://192.168.10.3/index.php
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 192.168.10.3 (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests
Server Software: Apache/2.4.25
Server Hostname: 192.168.10.3
Server Port: 80
Document Path: /index.php
Document Length: 84894 bytes
Concurrency Level: 100
Time taken for tests: 26.307 seconds
Complete requests: 10000
Failed requests: 1009
(Connect: 0, Receive: 0, Length: 1009, Exceptions: 0)
Write errors: 0
Total transferred: 850648867 bytes
HTML transferred: 848938867 bytes
Requests per second: 380.12 [#/sec] (mean)
Time per request: 263.072 [ms] (mean)
Time per request: 2.631 [ms] (mean, across all concurrent requests)
Transfer rate: 31577.41 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 1 1.1 0 13
Processing: 15 262 79.0 263 2291
Waiting: 10 68 53.2 47 2265
Total: 15 263 79.0 264 2291
Percentage of the requests served within a certain time (ms)
50% 264
66% 285
75% 299
80% 309
90% 334
95% 359
98% 391
99% 418
100% 2291 (longest request)
总结:xcache整体上性能还是有提升的