PHP优化之Operate Code

操作码介绍及缓存原理

当客户端请求一个PHP程序时,服务器的PHP引擎会解析该PHP程序,并将其编译为特定的操作码文件(Operate Code,opcode),这是要执行的PHP代码的一种二进制表示形式。
默认情况下,这个编译好的操作码文件由PHP引擎执行后丢弃。操作码缓存(Operate Code)的原理就是将这个编译后的操作码保存下来,并放到共享内存里,以便在下次调用该PHP页面时复用避免了相同代码的重复编译。
节省了PHP引擎重复编译的时间,降低了服务器负载,同时也减少了CPU和内存开销

为了提高PHP引擎的高并发访问及执行速度,产生了一系列的PHP缓存及加速软件。
这些软件设计的目的就是缓存前文提到的PHP引擎解析过的操作码文件,此时,在指定时间若有相同的PHP程序请求访问,就不需要重新解析编译
而是直接调用缓存中的PHP操作码文件,这样就提高了动态Web服务的处理速度,从而提升了用户访问企业网站的整体体

 

LNMP环境PHP缓存加速器原理详解

在LNMP环境中,PHP引擎不再使用libphp5.so模块,而是启动了独立的FCGI(php-fpm)进程,由它来监听来自Nginx的PHP程序请求,并交给PHP引擎解析处理,整个执行流程大概如下
  1、Nginx接收客户端的PHP程序访问请求
  2、Nginx根据扩展名等过滤规则将PHP程序请求传递给解析PHP的FCGI进程(php-fpm)
  3、php-fpm进程调用PHP解析器读取站点磁盘上的PHP文件,并加载到内存中
  4、PHP解释器将PHP程序编译成opcode文件,然后把opcode缓存起来
  5、php-fpm引擎执行opcode,返回数据给Nginx,进而返回客户端
  6、Nginx接收新的PHP程序请求,php-fpm引擎就会直接读取缓存中的opcode执行,将结果返回,而无需进行第4步操作,从而提升了PHP编译解析效率

 

PHP缓存加速器软件种类及选择建议xcache, eaccelerator. ZendOpcahce(另外还有APC略)

1、首选xcache
经过测试效率更好,更快
xcache软件开发社区活跃
支持更高版本的php,例如php5.5 php5.6
2、次选eaccelerator
安装配置参数更简单,加速效果不错
文档资料较多,但官方团建的更新较慢,社区不活跃
仅适合php5.4以下的程序
3、ZendOpcahce
php官方的新一代加速软件,以后发展潜力可能会很好,php5.5以前的版本可以通过ZendOpcahce软件以插件扩展的方式安装,之后的版本已经整合,编译参数--enable-opcache
ZendOpcahce可能是未来的缓存加速首选,现在的稳定性有待检验

 

PHP缓存加速器安装环境准备

LNMP基础Web环境准备:在安装PHP的扩展及缓存加速之前,需要先装好LNMP的完整环境,即phpinfo及数据库连接测试
CentOS release 6.7 (Final)
nginx-1.8.1
MySql5.5.49*64
PHP 5.3.29
有关LNMP环境扩展软件的部署说明——LNMP缓存加速特别提示
不管是Apache的libphp5.so模块方式,还是Nginx的FCGI的PHP服务方式,最后都是通过PHP提供动态程序解析的,因此在PHP引擎上的优化也是一致的,即都是基于PHP(php.ini)的,因此,如无特别说明,本节以后的环境安装和优化均适合。因此Apache服务的学习也可以参考


有关LNMP环境扩展软件的部署说明——LNMP缓存加速特别提示
不管是Apache的libphp5.so模块方式,还是Nginx的FCGI的PHP服务方式,最后都是通过PHP提供动态程序解析的
因此在PHP引擎上的优化也是一致的,即都是基于PHP(php.ini)的,因此,如无特别说明,本节以后的环境安装和优化均适合。因此Apache服务的学习也可以参考


解决部分加速软件的perl编译问题
在下列各软件的安装编译中,如果不解决perl的一些环境问题可能会带来意想不到的错误或警告,为了避免这些问题,需提前解决
1)配置环境变量LC_ALL
#echo "export LC_ALL=C" >>/etc/profile
#tail -1 /etc/profile ==>export LC_ALL=C
#source /etc/profile
#echo $LC_ALL ==>C
如果不设置该变量,在安装某些加速软件时,可能会遇到如下问题
perl :waring: Setting locale failed
perl :waring: Please check that your locale settings
LANGUAGE = (unset)
LC_ALL =(unset)

2)安装perl相关软件依赖
yum install perl-CPAN或yum install perl-devel
#如果不安装,在后面安装ImageMagick时可能会报错,后文安装ImageMagick时有报错说明

 

eAccelerator缓存加速模块

eAccelerator的最新版本为0.9.6.1,支持PHP5.3及以前5系列的版本
https://github.com/eaccelerator/eaccelerator/downloads
早期的0.9.5版本支持PHP4和PHP5.2以前的版本

eAccelerator缓存加速插件说明
eAccelerator是一个免费、开源的PHP加速、优化及缓存的扩展插件软件,它可以缓存opcode、session数据等,降低PHP程序在编译解析时对服务器的性能开销。
eAccelerator还可以加快PHP程序的执行速度,降低服务器负载压力,使PHP程序代码执行效率能提高1
-10倍 eAccelerator会把编译好的PHP程序存放在共享内存,然后每次直接从内存调用执行,可以设定把一些不适合放在内存里缓存的编译结果存储到磁盘上。
默认情况,磁盘和内存缓存都会被eAccelerator使用。 eAccelerator算是一个老牌的缓存加速软件,曾经在结合PHP引擎解析时被广泛使用,成熟稳定,目前代码更新不活跃,因此,使用的企业逐渐较少,但是eAccelerator仍是一款值得信赖的缓存加速软件


eAccelerator安装
# tar -xf eaccelerator-0.9.6.1.tar.bz2
# cd eaccelerator-0.9.6.1
# /usr/local/php/bin/phpize
Configuring for:
PHP Api Version: 20090626
Zend Module Api No: 20090626
Zend Extension Api No: 220090626
# ./configure --enable-eaccelerator=shared --with-php-config=/usr/local/php/bin/php-config
#make
#make install
Installing shared extensions: /usr/local/php5.3.29/lib/php/extensions/no-debug-non-zts-20090626/
# ls /usr/local/php5.3.29/lib/php/extensions/no-debug-non-zts-20090626/
eaccelerator.so


# /usr/local/php/bin/php -v
PHP 5.3.29 (cli) (built: Jan 30 2017 11:48:14)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2014 Zend Technologies
with eAccelerator v0.9.6.1, Copyright (c) 2004-2010 eAccelerator, by eAccelerator



 

PHP xcache缓存加速模块

xcache缓存加速插件说明
xcache是一个开源,又快又稳定的PHP opcode缓存器/优化器,其项目leader曾经是Lighttpd的开发成员之一。
xcache通过把PHP程序编译后的数据(opcode)缓存到共享内存,避免让相同的程序重复编译。
用户请求PHP程序时,可以直接使用缓存中已编译好的数据,从而提高PHP的访问速度,通常可以提升2-5倍,并大幅降低服务器负载开销。 很多公司在使用xache,它已经能在大流量/高负载的生产环境中稳定运行,与同类opcode缓存器相比在各个方面都更胜一筹。
例如:社区活跃、快读开发、能够快速跟进PHP的版本更新等。 当前稳定版本为3.
1.x(支持PHP5.1-5.5)和3.2.x(支持PHP5.1-5.6) 软件详情 http://xcache.lighttpd.net或http://xcache.lighttpd.net/woki/Introduction xcache3.2.x安装 #wget http://xcache.lighttpd.net/pub/Releases/3.2.0/xcache-3.2.0.tar.bz2 #tar xf xcache-3.2.0.tar.bz2 #cd xcache-3.2.0 #/usr/local/php/bin/phpize Configuring for: PHP Api Version: 20090626 Zend Module Api No: 20090626 Zend Extension Api No: 220090626 #./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config #make #make install Installing shared extensions: /usr/local/php5.3.29/lib/php/extensions/no-debug-non-zts-20090626/ #ls /usr/local/php5.3.29/lib/php/extensions/no-debug-non-zts-20090626/ eaccelerator.so xcache.so

 

PHP官方加速插件ZendOpcache

ZendOpcache插件说明
PHP5.5版本后已经集成ZendOpcache,编译安装PHP时加入编译参数--enable-opcache
PHP5.2-PHP5.4以插件方式安装(http://pecl.php.net/package/ZendOpcache)
Release 7.0.5:
PHP Version: PHP version 5.5.0 or older
PHP Version: PHP 5.2.0 or newer
PEAR Package: PEAR 1.4.0b1 or newer

ZendOpcache安装
#wget http://pecl.php.net/get/zendopcache-7.0.5.tgz
#tar xf zendopcache-7.0.5.tgz
#cd zendopcache-7.0.5
#/usr/local/php/bin/phpize 
#./configure --enable-opcache --with-php-config=/usr/local/php/bin/php-config 
#make
#make install
#ls /usr/local/php5.3.29/lib/php/extensions/no-debug-non-zts-20090626/
 eaccelerator.so  opcache.so  xcache.so

==========5.5及以后编译时为开启的,如未开启
cd /home/tools/php-5.6.22/ext/opcache
/usr/local/php/bin/phpize 
./configure --with-php-config=/usr/local/php/bin/php-config
make
make install
==========

 

数据库缓存Memcached

Memcached缓存软件说明
Memcached是一个开源的、支持高性能、高并发及分布式的内存缓存服务软件。
分为服务器端和客户端:Memcached-xxx和Memcache-xxx
Release 3.0.8:(Release 2.2.7:)
PHP Version: PHP 4.3.11 or newer
PEAR Package: PEAR 1.4.0b1 or newer

Memcached的作用
传统场景下,多数web应用都将数据保存到RDBMS中,WWW服务器从中读取数据并在浏览器中显示,但是随着数据量的增大,访问的集中,就会出现RDBMS的负担加重,数据库响应缓慢,网站打开延迟等恶劣影响。
而Memcached是高性能的分布式内存缓存服务,使用Memcached的主要目的是,通过在自身内存中缓存数据库的查询结果,较少数据库访问次数,以提高动态Web应用的速度,提高网站架构的并发能力和可扩展性。
Memcached服务通过在事先规划好的系统内存空间中临时缓存数据库中的各类数据,以达到减少前端业务对数据库的直接高并发访问,从而提升大规模网站集群中动态服务的并发访问能力。



安装PHP Memcache扩展插件
wget http://pecl.php.net/get/memcache-2.2.7.tgz
tar -zxf memcache-2.2.7.tgz
cd memcache-2.2.7
/usr/local/php/bin/phpize
./configure --enable-memcache --with-php-config=/usr/local/php/bin/php-config
make
make install
ls /usr/local/php5.3.29/lib/php/extensions/no-debug-non-zts-20090626/
eaccelerator.so memcache.so opcache.so xcache.so

提示
如果安装的是mencache-2.2.4,可能会报错:make:***[memcache.lo] Error 1
解决方法:cp memcache.loT memcache.lo

 

PDO_MYSQL扩展模块

PDO_MYSQL扩展插件说明:
PDO扩展为PHP访问数据库定义了一个轻量级一致性接口,它提供了一个数据访问抽象层,这样,无论使用的是什么数据库,都可以通过一致的函数执行查询并获取数据
PDO_MYSQL的安装方法:
  一种是插件安装,
  另一种是编译PHP时直接指定PHP对应PDO_MYSQL的编译参数
--with-pdo-mysql=mysqlnd PDO_MYSQL扩展插件安装 wget -q http://pecl.php.net/get/PDO_MYSQL-1.0.2.tgz tar -xf PDO_MYSQL-1.0.2.tgz cd PDO_MYSQL-1.0.2 /usr/local/php/bin/phpize ./configure --with-php-config=/usr/local/php/bin/php-config --with-pdo-mysql=/usr/local/mysql #如果未安装mysql,可以拷贝个mysql二进制包 make make install ls /usr/local/php5.3.29/lib/php/extensions/no-debug-non-zts-20090626/ eaccelerator.so memcache.so opcache.so pdo_mysql.so xcache.so php5.5之后如果编译时未安装 cd /home/tools/php-5.6.22/ext/pdo_mysql /usr/local/php/bin/phpize ./configure --with-php-config=/usr/local/php/bin/php-config --with-pdo-mysql=/usr/local/mysql make make install ls /usr/local/php5.6.22/lib/php/extensions/no-debug-non-zts-20131226/ eaccelerator.so memcache.so opcache.so pdo_mysql.so xcache.so 说明: [root@test80 PDO_MYSQL-1.0.2]# /usr/local/php/bin/phpize 遇到如下过错,似乎可以忽略 config.m4:104: warning: AC_CACHE_VAL(pdo_inc_path, ...): suspicious cache-id, must contain _cv_ to be cached ../../lib/autoconf/general.m4:1974: AC_CACHE_VAL is expanded from... ../../lib/autoconf/general.m4:1994: AC_CACHE_CHECK is expanded from... aclocal.m4:2754: PHP_CHECK_PDO_INCLUDES is expanded from... config.m4:104: the top level config.m4:104: warning: AC_CACHE_VAL(pdo_inc_path, ...): suspicious cache-id, must contain _cv_ to be cached ../../lib/autoconf/general.m4:1974: AC_CACHE_VAL is expanded from... ../../lib/autoconf/general.m4:1994: AC_CACHE_CHECK is expanded from... aclocal.m4:2754: PHP_CHECK_PDO_INCLUDES is expanded from... config.m4:104: the top level

 

PHP扩展插件模块   ImageMagick及imagick

安装图像处理程序ImageMagick及imagick
ImageMagick是一套功能强大、稳定而且开源的工具集和开发包,可以用来读、写和处理超过89种基本格式的图片文件,
包括流行的TIFF、JPEG、GIF、 PNG、PDF以及PhotoCD等格式。利用ImageMagick,你可以根据web应用程序的需要动态生成图片, 
还可以对一个(或一组)图片进行改变大小、旋转、锐化、减色或增加特效等操作,并将操作的结果以相同格式或其它格式保存,对图片的操作,
即可以通过命令行进行,也可以用C/C++、Perl、Java、PHP、Python或Ruby编程来完成。同时ImageMagick提供了一个高质量的2D工具包,
部分支持SVG。ImageMagic的主要精力集中在性能,减少bug以及提供稳定的API和ABI上。
ImageMagick 是一个用来创建、编辑、合成图片的软件。它可以读取、转换、写入多种格式的图片。图片切割、颜色替换、各种效果的应用,
图片的旋转、组合,文本,直线, 多边形,椭圆,曲线,附加到图片伸展旋转。
ImageMagick是免费软件:全部源码开放,可以自由使用,复制,修改,发布。支持大多数的操作系统。

ImageMagick的功能
1. 将图片从一个格式转换到另一个格式,包括直接转换成图标。
2. 改变尺寸、旋转、锐化(sharpen)、减色、图片特效
3. 缩略图片的合成图( a montage of image thumbnails)
4. 适于web的背景透明的图片
5. 将一组图片作成gif动画,直接convert
6. 将几张图片作成一张组合图片,montage
7. 在一个图片上写字或画图形,带文字阴影和边框渲染。
8. 给图片加边框或框架
9. 取得一些图片的特性信息
10,几乎包括了gimp可以作到的常规插件功能。甚至包括各种曲线参数的渲染功能。只是那命令的写法,够复杂。

安装ImageMagick(ftp://ftp.imagemagick.org/pub/ImageMagick/)
tar -xf ImageMagick-6.7.9-9.tar.xz 
cd ImageMagick-6.7.9-9
./configure
make
make install

安装imagick php扩展插件
  imagick插件工作需要ImageMagic软件支持,所以,必须先安装好ImageMagick
  imagick插件是一个可以供php调用ImageMagick功能的扩展模块,
  使用这个扩展可以使PHP具备ImageMagick功能,使用imagick扩展才能使用ImageMagick的api来对图片进行操作 imagick扩展插件安装(http:
//pecl.php.net/package/imagick) tar -xf imagick-2.3.0.tgz cd imagick-2.3.0 /usr/local/php/bin/phpize ./configure --with-php-config=/usr/local/php/bin/php-config make make install ls /usr/local/php5.3.29/lib/php/extensions/no-debug-non-zts-20090626 eaccelerator.so imagick.so memcache.so opcache.so pdo_mysql.so xcache.so 安装imagick3.0扩展插件时报错 make: *** [imagick_class.lo] Error 1 #yum install ImageMagick-devel 安装后出现warning,最终make install能产生so文件 warning: 'MagickGetImageMatte' is deprecated (declared at /usr/include/ImageMagick/wand/deprecate.h:82) /root/tmp/pear/imagick/imagick_class.c: In function 'zim_imagick_paintfloodfillimage':

 

配置memcache/pdo_mysql/imagick模块生效   打开phpinfo页面进行验证=====================================================

1、修改php.ini
#vi /usr/local/php/lib/php.ini
extension_dir = "./"
extension_dir = "/usr/local/php5.3.29/lib/php/extensions/no-debug-non-zts-20090626/"
extension = memcache.so
extension = pdo_mysql.so
extension = imagick.so

报错 #pkill php
-fpm # /usr/local/php/sbin/php-fpm [21-Oct-2016 14:27:28] NOTICE: PHP message: PHP Warning: PHP Startup: Unable to load dynamic library '/usr/local/php5.3.29/lib/php/extensions/no-debug-non-zts-20090626/pdo_mysql.so' - libmysqlclient.so.18: cannot open shared object file: No such file or directory in Unknown on line 0
#
grep pdo_mysql /usr/local/php/lib/php.ini extension = pdo_mysql.so ;extension=php_pdo_mysql.dll ; http://php.net/pdo_mysql.cache_size pdo_mysql.cache_size = 2000 ; http://php.net/pdo_mysql.default-socket 解决方案 find / -name libmysqlclient.so vi /etc/ld.so.conf加入libmysqlclient.so的路径 ldconfig 通用解决方案参考 #找不到.so文件 #find / -name "libmysqlclient.so.18" /usr/lib #vi /etc/ld.so.conf /usr/lib #ldconfig


 

eaccelerator eaccelerator-0.9.6.1.tar.bz2 The latest release, 0.9.6.1, features support for PHP 5.3 and also works with PHP 5.1 and 5.2 https://github.com/eaccelerator/eaccelerator/downloads
  eaccelerator-0.9.5.1.tar.bz2 The previous 0.9.5 branch supports PHP 4 and all PHP 5 releases including 5.2.  
xcache XCache 3.2.0. Latest Stable Release: 3.2.x (full 5.1 ~ 5.6 support) http://xcache.lighttpd.nethttp://xcache.lighttpd.net/woki/Introduction
ZendOpcache   PHP5.5版本后已经集成ZendOpcache,编译安装PHP时加入编译参数--enable-opcache http://pecl.php.net/package/ZendOpcache
Memcache Release 3.0.8:Release 2.2.7:

PHP Version: PHP 4.3.11 or newer

PEAR Package:  1.4.0b1 or newer
http://pecl.php.net/get/memcache-2.2.7.tgz
PDO_MYSQL PDO_MYSQL-1.0.2.tgz PDO_MYSQL的安装方法:一种是插件安装,另一种是编译PHP时直接指定PHP对应PDO_MYSQL的编译参数--with-pdo-mysql=mysqlnd

http://pecl.php.net/get/PDO_MYSQL-1.0.2.tgz

--with-pdo-mysql=mysqlnd,编译参数在生产中可能遇到socker连接问题

解决方法:在php.ini里添加pdo_mysql.default_scok=/usr/local/php/tmp/mysql.sock

ImageMagick

ImageMagick-6.7.9-9.tar.xz ftp://ftp.imagemagick.org/pub/ImageMagick http://pecl.php.net/package/imagick

生产环境插件安装建议

功能性插件 PDO_MYSQL\memcache\imagick建议安装

性能性opcode插件任选一种,选择时可根据实际压测结果,上述插件效果都还可以

主流压测工具apache abwebbench http_loadloader runner

 

配置 eAccelerator插件生效并优化参数

1、配置eAccelerator缓存目录
mkdir -p /tmp/eaccelerator #此目录可以用tmpfs文件系统或者SSD来存储
chown -R nginx.nginx /tmp/eaccelerator
2、修改php.ini文件,加载eaccelerator模块及参数配置
cat >> /usr/local/php/lib/php.ini<<EOF
[eaccelerator]
extension="eaccelerator.so"
eaccelerator.shm_size = "64"   
eaccelerator.cache_dir = "/tmp/eaccelerator"
eaccelerator.enable = "1"
eaccelerator.optimizer = "1"
eaccelerator.debug = 0  
eaccelerator.check_mtime = "1"
eaccelerator.filter = ""
eaccelerator.shm_max = "0"
eaccelerator.shm_ttl = "3600" 
eaccelerator.shm_prune_period = "3600" 
eaccelerator.shm_only = "0"
eaccelerator.compress = "1"
eaccelerator.compress_level = "9"
EOF

3、访问PHP页面测试检查eaccelerator加速情况,重启php服务后,访问php页面,如phpini,就会有缓存
# ls /tmp/eaccelerator/
0 1 2 3 4 5 6 7 8 9 a b c d e f
# find /tmp/eaccelerator/ -type f | xargs file
/tmp/eaccelerator/3/8/eaccelerator-0502.413593: data
eaccelerator-0502.413593就是cache的内容,而且是phpinfo的页面缓存内容,类型为data

# /usr/local/php/bin/php -v
PHP 5.3.29 (cli) (built: Oct 15 2016 01:56:22)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2014 Zend Technologies
with eAccelerator v0.9.6.1, Copyright (c) 2004-2010 eAccelerator, by eAccelerator


tmpfs
tmpfs是一种基于内存的文件系统,它和虚拟磁盘ramdisk比较类似像,但不完全相同,和ramdisk一样,tmpfs可以使用RAM,但它也可以使用swap分区来存储。而且传统的ramdisk是个块设备,要用mkfs来格式化它,才能真正地使用它;而tmpfs是一个文件系统,并不是块设备,只是安装它,就可以使用了。tmpfs是最好的基于RAM的文件系统。(访问量大,总量不大的情况使用)
例如:上传图片,切图,服务器上 /tmp切图 临时文件。

使用tmpfs优化eaccelerator缓存目录
tmpfs是一种基于内存的文件系统,使用tmpfs作为数据临时存储通常比本地磁盘快很多,此方法适合各类缓存场景,例如,上传图片时候很多软件在/tmp下临时缓存切图、存放session数据,则可以让/tmp使用tmpfs文件系统来加快访问效率,本文将/tmp/accelerrator挂载到tmpfs文件系统上,让访问缓存数据更快,具体操作如下
[root@test80 ~]# mount -t tmpfs -o size=16m tmpfs /tmp/eaccelerator/
[root@test80 ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda2 14987656 3192996 11026660 23% /
tmpfs 502384 0 502384 0% /dev/shm
/dev/sda1 194241 35987 148014 20% /boot
tmpfs 16384 0 16384 0% /tmp/eaccelerator
提示:如需要原来缓存内容,可以先cp出来,然后cp进来
tmpfs挂载可以写到rc.local

参数详解
[eaccelerator]           开始eaccelerator加速模块配置
extension="eaccelerator.so" 加载的eaccelerator模块位置,相对路径extension_dir设置
eaccelerator.shm_size = "64" 存储缓存数据的共享内存大小,如果为0,则最大值看内核配置/etc/sys/kernel/shmmax
eaccelerator.cache_dir = "/tmp/eaccelerator" 磁盘缓存存储路径,缓存内容为precompiled code.session data,content and user entries here,缺省为/tmp/eaccelerator
eaccelerator.enable = "1" 缓存生效开关
eaccelerator.optimizer = "1" 加速PHP代码执行速度,1为默认值表示激活,用于缓存前的代码加速
eaccelerator.debug = 0 缓存加速调试,0为关闭,1为打开,打开后可以看到缓存命中信息
eaccelerator.check_mtime = "1" 检查缓存修改时间决定代码是否需要重新编译,1为激活,是默认值
eaccelerator.filter = "" 设定对象是否缓存规则,空表示不设定
eaccelerator.shm_max = "0" 可以被放置的最大缓存最大值,0不限制
eaccelerator.shm_ttl = "3600" 缓存文件的生存期
eaccelerator.shm_prune_period = "3600" 当共享内存空间不够时,从共享内存移除老数据的时间周期
eaccelerator.shm_only = "0" 是否允许缓存数据到磁盘,0为允许,但是对于session data和Content caching无影响
eaccelerator.compress = "1" 是否开启压缩,1为开启
eaccelerator.compress_level = "9" 压缩级别,9为最高

如下根据内容指定是否缓存到共享内存或磁盘参数
eaccelerator.keys = "shm_and_disk" 控制keys缓存位置
shm_and_disk==>cache data in shared memory and on disk (default value)
shm ==>cache data in shared memory or on disk if shared memory is full or data size greater then "eaccelerator.shm_max"
shm_only ==>cache data in shared memory
disk_only ==>cache data on disk
none ==>don't cache data
eaccelerator.sessions = "shm_and_disk" 控制session缓存位置
eaccelerator.content = "shm_and_disk 控制content缓存位置

eaccelerator.log_file = "/var/log/httpd/eaccelerator_log" Set the log file for eaccelerator. When this option isn't set then the data will be logged to stderr, when using PHP with Apache these lines will be added to the Apache error log.

eaccelerator.name_space = ""
参数详解https://github.com/eaccelerator/eaccelerator/wiki/Settings#Loading_eAccelerator

 

配置xcache缓存插件加速

配置前应将eAccerator所有配置清除
xcache.ini参数说明-->tar包解压目录有xcache.ini配置文件
[root@test80 xcache-3.2.0]# cat xcache.ini   (http://xcache.lighttpd.net/wiki/XcacheIni参数)
;; this is an example, it won't work unless properly configured into php.ini
[xcache-common]         #加载xcache.so,路径相对于extension_dir的配置
extension = xcache.so      #3.0版本开始不再支持zend_extension加载xcache的方式
[xcache.admin]           #激活管理员认证
xcache.admin.enable_auth = On

; use http://xcache.lighttpd.net/demo/cacher/mkpassword.php to generate your encrypted password
xcache.admin.user = "mOo"           #指定xcache管理员用户名和密码,空表示禁止管理页面
xcache.admin.pass = "md5 encrypted password"       #密码根据上述地址产生

[xcache]       #开始配置参数,初始值即默认值
; ini only settings, all the values here is default unless explained

; select low level shm implemenation
xcache.shm_scheme =        "mmap"       #设置如何从系统分配共享内存
; to disable: xcache.size=0
; to enable : xcache.size=64M etc (any size > 0) and your system mmap allows
xcache.size  =               60M  #0表示禁止缓存,需要注意系统所允许的nmap最大值
; set to cpu count (cat /proc/cpuinfo |grep -c processor)
xcache.count =                 1  #指定将cache切成多少块,建议值为cpu的数量
; just a hash hints, you can always store count(items) > slots
xcache.slots =                8K   #只是作为hash槽个数的参考值,缓冲超过这个数的内容没有问题
; ttl of the cache item, 0=forever
xcache.ttl   =                 0  #设置cache对象的生存周期TTL,0为永不过期
; interval of gc scanning expired items, 0=no scan, other values is in seconds
xcache.gc_interval =           0    回收器扫描过期的对象回收内存空间的间隔,0为不扫描,单位为秒

; same as aboves but for variable cache,而不是opcode缓存
xcache.var_size  =            4M
xcache.var_count =             1
xcache.var_slots =            8K
; default value for $ttl parameter of xcache_*() functions
xcache.var_ttl   =             0
; hard limit ttl that cannot be exceed by xcache_*() functions. 0=unlimited
xcache.var_maxttl   =          0
xcache.var_gc_interval =     300

; mode:0, const string specified by xcache.var_namespace
; mode:1, $_SERVER[xcache.var_namespace]
; mode:2, uid or gid (specified by xcache.var_namespace)
xcache.var_namespace_mode =    0
xcache.var_namespace =        ""

; N/A for /dev/zero
xcache.readonly_protection = Off 如果启用了此参数,会略微降低性能,但是会提高安全系数
这个选项对于xcache.mmap_path =    "/dev/zero"无效
; for *nix, xcache.mmap_path is a file path, not directory. (auto create/overwrite)
; Use something like "/tmp/xcache" instead of "/dev/*" if you want to turn on ReadonlyProtection
; different process group of php won't share the same /tmp/xcache
; for win32, xcache.mmap_path=anonymous map name, not file path
xcache.mmap_path =    "/dev/zero"  #对于*nix, xcache.mmap_path是个文件路径而非目录,如果要启用该参数,请使用"/tmp/xcache" instead of "/dev/*"。如果开启了xcache.readonly_protection,不同进程组的PHP将不会共享同一个/tmp/xcache路径


; Useful when XCache crash. leave it blank(disabled) or "/tmp/phpcore/" (writable by php)
xcache.coredump_directory =   ""   #当xcache crash后,是否把数据保存到指定路径
; Windows only. leave it as 0 (default) until you're told by XCache dev
xcache.coredump_type =         0  

; disable cache after crash
xcache.disable_on_crash =    Off  #当xcache crash后,自动关闭xcache缓存

; enable experimental documented features for each release if available
xcache.experimental =        Off

; per request settings. can ini_set, .htaccess etc
xcache.cacher =               On
xcache.stat   =               On
xcache.optimizer =           Off

[xcache.coverager]
; enabling this feature will impact performance
; enabled only if xcache.coverager == On && xcache.coveragedump_directory == "non-empty-value"

; per request settings. can ini_set, .htaccess etc
; enable coverage data collecting and xcache_coverager_start/stop/get/clean() functions
xcache.coverager =           Off
xcache.coverager_autostart =  On

; set in php ini file only
; make sure it's readable (open_basedir is checked) by coverage viewer script
xcache.coveragedump_directory = ""


调整关键参数,修改后将上述文件追加到php.ini
xcache.size=64M ##0表示禁止缓存,需要注意系统所允许的nmap最大值 xcache.size=256M
xcache.count = 1 #指定将cache切成多少块,建议值为cpu的数量
xcache.ttl = 0 #设置cache对象的生存周期TTL,0为永不过期 xcache.ttl = 86400
xcache.gc_interval = 0 回收器扫描过期的对象回收内存空间的间隔,0为不扫描,单位为秒 xcache.gc_interval = 3600
xcache.var_size = 4M xcache.var_size = 0




 

 

ZendOpcahce缓存加速配置

# vi  /usr/local/php/lib/php.ini        #加入配置参数
[opcache]
zend_extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/opcache.so
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
opcache.fast_shutdown=1
opcache.enable_cli=1

[root@test80 zendopcache-7.0.5]# cat README (已删除部分,需要可查看)
Compatibility
-------------
This version of Zend OPcache is compatible with PHP 5.2.*, 5.3.*, 5.4.*
and PHP-5.5 development branch.  PHP 5.2 support may be removed in the future.
Quick Install
-------------
- Compile

 $PHP_DIR/bin/phpize
 ./configure \
      --with-php-config=$PHP_DIR/bin/php-config
 make
- Install

 make install # this will copy opcache.so into PHP extension directory
- Edit php.ini

 zend_extension=/...full path.../opcache.so

We recommend the following configuration options for best performance
in a production environment.

关键参数解释 opcache.memory_consumption
=128     #opcache共享内存空间大小,用于存放precompiled PHP code,默认64M opcache.interned_strings_buffer=8   #interned strings 内存的数量,默认4M opcache.max_accelerated_files=4000   #opcache哈希表key的最大数量,默认2000 opcache.revalidate_freq=60         #检查文件时间戳的频率,用于共享内存分配的变化,默认为2 opcache.fast_shutdown=1         #默认为0,如果激活,一个快速的关闭队列将被用来加速代码 opcache.enable_cli=1            默认为0,激活PHP CLI的opcache,用于测试和调试#

 

 redis的php客户端扩展安装

redis的php客户端扩展安装
wget https://github.com/nicolasff/phpredis/archive/master.zip
unzip phpredis-master.zip 
cd phpredis-master
/usr/local/php/bin/phpize 
./configure --with-php-config=/usr/local/php/bin/php-config
make
make install 
vi /usr/local/php/lib/php.ini 
echo "extension = redis.so" >> /usr/local/php/lib/php.ini
ls /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/
memcache.so  redis.so
killall php-fpm
/usr/local/php/sbin/php-fpm

 

 

posted @ 2017-05-27 16:42  黑色月牙  阅读(782)  评论(0编辑  收藏  举报