上篇文章中讲到了如何快速部署LAMP及在LAMP架构基础上搭建phpmyadmin和wordpress。因为要访问到动态页面,所以当并发访问量太大或者访问的页面过大时系统响应时间会有点长。因此我们可以通过加速器来解决这个问题。
php的加速器:基于PHP的特殊扩展机制如opcode缓存扩展也可以将opcode缓存于php的共享内存中,从而可以让同一段代码的后续
重复执行时跳过编译阶段以提高性能。这些加速器并非真正提高了pcode的运行速度,而仅是通过分析opcode后并将它们重新排列以
达到快速执行的目的。
常见的php加速器有:
1、APC (Alternative PHP Cache)
遵循PHP License的开源框架,PHP opcode缓存加速器,目前的版本不适用于PHP 5.4
2、eAccelerator
源于Turck MMCache,早期的版本包含了一个PHP encoder和PHP loader,目前encoder已经不在支持。项目地址
3、XCache
快速而且稳定的PHP opcode缓存,经过严格测试且被大量用于生产环境。项目地址:http://xcache.lighttpd.net/,收录EPEL源
4、Zend Optimizer和Zend Guard Loader
Zend Optimizer并非一个opcode加速器,它是由Zend Technologies为PHP5.2及以前的版本提供的一个免费、闭源的PHP
扩展,其能够运行由Zend Guard生成的加密的PHP代码或模糊代码。而Zend Guard Loader则是专为PHP5.3提供的类似于Zend
Optimizer功能的扩展。项目地址
5、NuSphere PhpExpress
NuSphere的一款开源PHP加速器,它支持装载通过NuSphere PHP Encoder编码的PHP程序文件,并能够实现对常规PHP文件的执行加速。
这里主要讲如何使用xcache来加速访问http服务器上的资源,xcache已被收录于EPEL源中,可以使用yum安装,不过这里主要讲如何编译安装,我们用的版本是xcache-3.2.0。源码包的下载地址为http://xcache.lighttpd.net/。
一、编译前准备,安装开发工具包组,及相关软件包
[root@centos7 ~]# yum groupinstall "devlopment tools"
[root@centos7 ~]# yum -y install php-devel
解压源码包
[root@centos7 ~]# tar xvf xcache-3.2.0.tar.bz2
编译安装xcache有一点特别,当我们进入xcache/目录下发现他没有configure目录。我们得运行phpize命令生成编译环境。
[root@centos7 ~]# cd xcache-3.2.0/
[root@centos7 xcache-3.2.0]# ls
AUTHORS config.m4 devel includes.c Makefile.frag mod_cacher mod_disassembler NEWS run-xcachetest util xcache_globals.h xcache-test.ini
bin config.w32 gen_structinfo.awk INSTALL Makefile.frag.deps mod_coverager mod_encoder processor tests xcache xcache.h xcache-zh-gb2312.ini
ChangeLog COPYING htdocs lib mod_assembler mod_decoder mod_optimizer README THANKS xcache.c xcache.ini
生成编译环境
[root@centos7 xcache-3.2.0]# phpize
Configuring for:
PHP Api Version: 20100412
Zend Module Api No: 20100525
Zend Extension Api No: 220100525
[root@centos7 xcache-3.2.0]# ls
acinclude.m4 build config.sub devel install-sh Makefile.global mod_coverager NEWS tests xcache_globals.h
aclocal.m4 ChangeLog configure gen_structinfo.awk lib missing mod_decoder processor THANKS xcache.h
AUTHORS config.guess configure.in htdocs ltmain.sh mkinstalldirs mod_disassembler README util xcache.ini
autom4te.cache config.h.in config.w32 includes.c Makefile.frag mod_assembler mod_encoder run-tests.php xcache xcache-test.ini
bin config.m4 COPYING INSTALL Makefile.frag.deps mod_cacher mod_optimizer run-xcachetest xcache.c xcache-zh-gb2312.ini
编译安装xcache,编译的时候我们要指定启用xcache功能,同时还要调用php-config这和二进制程序,可以使用which查看该二进制程序的路径。
[root@centos7 xcache-3.2.0]# which php-config
/usr/bin/php-config
[root@centos7 ~]# cd xcache-3.2.0
[root@centos7 xcache-3.2.0]# ./configure --enable-xcache --with-php-config=/usr/bin/php-config
[root@centos7 xcache-3.2.0]# make && make install 编译完成后我们可以发现扩展模块放在了/usr/lib64/php/modules/目录下。
See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
Build complete.
Don't forget to run 'make test'.
Installing shared extensions: /usr/lib64/php/modules/
把配置文件复制到/etc/php.d/目录下,然后重启服务即可。
[root@centos7 xcache-3.2.0]# cp xcache-3.2.0/xcache.ini /etc/php.d/
[root@centos7 xcache-3.2.0]# systemctl restart httpd.service
没有使用加速器的情况,(多测几次这样数据才会准确)
[root@centos7 ~]# ab -c 100 -n 1000 http://192.168.239.128/wordpress
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.239.128 (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests
Server Software: Apache
Server Hostname: 192.168.239.128
Server Port: 80
Document Path: /wordpress
Document Length: 241 bytes
Concurrency Level: 100
Time taken for tests: 0.332 seconds
Complete requests: 1000
Failed requests: 0
Write errors: 0
Non-2xx responses: 1000
Total transferred: 458000 bytes
HTML transferred: 241000 bytes
Requests per second: 3010.65 [#/sec] (mean)
Time per request: 33.215 [ms] (mean)
Time per request: 0.332 [ms] (mean, across all concurrent requests)
Transfer rate: 1346.56 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 1 8.4 0 108
Processing: 5 31 39.0 17 166
Waiting: 2 31 38.9 16 166
Total: 6 32 40.7 17 169
Percentage of the requests served within a certain time (ms)
50% 17
66% 18
75% 21
80% 32
90% 111
95% 156
98% 161
99% 167
100% 169 (longest request)
使用加速器的情况(多测几次这样数据才会准确)
[root@centos7 ~]# ab -c 100 -n 1000 http://192.168.239.128/wordpress
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.239.128 (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests
Server Software: Apache
Server Hostname: 192.168.239.128
Server Port: 80
Document Path: /wordpress
Document Length: 241 bytes
Concurrency Level: 100
Time taken for tests: 0.172 seconds
Complete requests: 1000
Failed requests: 0
Write errors: 0
Non-2xx responses: 1000
Total transferred: 458000 bytes
HTML transferred: 241000 bytes
Requests per second: 5820.72 [#/sec] (mean)
Time per request: 17.180 [ms] (mean)
Time per request: 0.172 [ms] (mean, across all concurrent requests)
Transfer rate: 2603.41 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.6 0 4
Processing: 1 16 3.2 16 21
Waiting: 1 16 3.2 16 21
Total: 5 16 2.7 17 21
Percentage of the requests served within a certain time (ms)
50% 17
66% 17
75% 18
80% 18
90% 19
95% 19
98% 20
99% 21
100% 21 (longest request)