PHP缓存加速
■ PHP默认会将Operate Code文件丢弃,缓存加速是将其保存下来,放置共享内存中,以便在下次调用该PHP页面时重用,避免相同代码的重复编译
___________________________________________________________
■ LAMP环境PHP解析原理
___________________________________________________________
Apache接收客户的PHP程序请求,并根据规则过滤之
Apache将PHP程序请求传递给PHP处理模块libphp5.so
PHP引擎定位磁盘上的PHP文件,并将其加载到内存中解析
PHP处理模块libphp5.so将PHP源码编译成opcode
PHP处理模块libphp5.so执行opcode,然后把opcode缓存起来
Apache接收客户端新的PHP程序请求,PHP引擎直接读取缓存执行opcode文件,并将结果返回
■ LNMP环境PHP解析原理
___________________________________________________________
Nginx接收客户端的PHP程序访问请求
Nginx根据扩展名等过滤规则将PHP程序请求传递给解析PHP的FCGI(PHP FPM)进程
PHP FPM进程调用PHP解析器读取站点磁盘上的PHP文件,并加载到内存中
PHP解析器将PHP程序编译成为opcode文件,然后把opcode缓存起来
PHP FPM引擎执行opcode树后,返回数据给Nginx,进而返回客户端
Nginx接收客户新的PHP程序请求,PHP FPM引擎就会直接读取缓存中的opcode并执行,将结果返回
■ 缓存加速插件安装配置(xcache)
___________________________________________________________
[root@Web_server src]# wget http://xcache.lighttpd.net/pub/Releases/3.2.0/xcache-3.2.0.tar.bz2
[root@Web_server src]# tar xf xcache-3.2.0.tar.bz2
[root@Web_server src]# cd xcache-3.2.0
[root@Web_server xcache-3.2.0]# /usr/local/php/bin/phpize
[root@Web_server xcache-3.2.0]# ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config
[root@Web_server xcache-3.2.0]# make && make install
[root@Web_server xcache-3.2.0]# ll /usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/
total 2208
-rwxr-xr-x 1 root root 1021684 Apr 13 10:33 opcache.a
-rwxr-xr-x 1 root root 538161 Apr 13 10:33 opcache.so
-rwxr-xr-x 1 root root 692628 Apr 18 09:44 xcache.so #<==生成xcache.so模块
[root@Web_server xcache-3.2.0]# cat /usr/local/src/xcache-3.2.0/xcache.ini >> /usr/local/php/etc/php.ini
[root@Web_server xcache-3.2.0]# tail -85 /usr/local/php/etc/php.ini | egrep -v "^;|^$"
[xcache-common]
extension = xcache.so
[xcache.admin]
xcache.admin.enable_auth = On
xcache.admin.user = "mOo"
xcache.admin.pass = "md5 encrypted password"
[xcache]
xcache.shm_scheme = "mmap"
xcache.size = 256M
xcache.count = 2
xcache.slots = 8K
xcache.ttl = 86400
xcache.gc_interval = 3600
xcache.var_size = 64M
#更改标红参数,生产环境根据硬件和业务数据来调整
[root@Web_server xcache-3.2.0]# /usr/local/php/bin/php -v #检查配置
PHP 5.5.31 (cli) (built: Apr 13 2016 10:29:58)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies
with XCache v3.2.0, Copyright (c) 2005-2014, by mOo
with XCache Cacher v3.2.0, Copyright (c) 2005-2014, by mOo
[root@Web_server ~]# echo -n "szk" |md5sum
[root@Web_server ~]# vim /usr/local/php/etc/php.ini #配置xcache用户密码
xcache.admin.user = "szk"
xcache.admin.pass = "005eff7dd9626f0c30f2eb8003ceafc9"
[root@Web_server ~]# cp -a /usr/local/src/xcache-3.2.0/htdocs/ /data/www/xadmin
[root@Web_server ~]# chown -R nobody.nobody /data/www/xadmin
[root@Web_server ~]# /etc/init.d/php-fpm start
■ 缓存加速插件安装配置(OPcache)
___________________________________________________________
#若5.5版本,会自动安装opcache插件,编译时--enable=opcache启用
[root@Web_server ~]# ll /usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/
-rwxr-xr-x 1 root root 538161 Apr 13 10:33 opcache.so
[root@Web_server ~]# vim /usr/local/php/etc/php.ini #若编译没有启用,则在配置文件中添加下面内容即可
zend_extension=opcache.so
[opcache]
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
opcache.fast_shutdown=1
opcache.enable=1
opcache.enable_cli=1
[root@Web_server ~]# /usr/local/php/bin/php -v
PHP 5.5.31 (cli) (built: Apr 13 2016 10:29:58)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies
with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2015, by Zend Technologies