PHP开启缓存加速

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)
___________________________________________________________

  1. [root@Web_server src]# wget http://xcache.lighttpd.net/pub/Releases/3.2.0/xcache-3.2.0.tar.bz2
  2. [root@Web_server src]# tar xf xcache-3.2.0.tar.bz2
  3. [root@Web_server src]# cd xcache-3.2.0
  4. [root@Web_server xcache-3.2.0]# /usr/local/php/bin/phpize
  5. [root@Web_server xcache-3.2.0]# ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config  
  6. [root@Web_server xcache-3.2.0]# make && make install
  7. [root@Web_server xcache-3.2.0]# ll /usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/
  8. total 2208
  9. -rwxr-xr-x 1 root root 1021684 Apr 13 10:33 opcache.a
  10. -rwxr-xr-x 1 root root  538161 Apr 13 10:33 opcache.so
  11. -rwxr-xr-x 1 root root  692628 Apr 18 09:44 xcache.so      #<==生成xcache.so模块
  12.  
  13. [root@Web_server xcache-3.2.0]# cat /usr/local/src/xcache-3.2.0/xcache.ini >> /usr/local/php/etc/php.ini
  14. [root@Web_server xcache-3.2.0]# tail -85 /usr/local/php/etc/php.ini | egrep -v "^;|^$"
  15. [xcache-common]
  16. extension = xcache.so
  17. [xcache.admin]
  18. xcache.admin.enable_auth = On
  19. xcache.admin.user = "mOo"
  20. xcache.admin.pass = "md5 encrypted password"
  21. [xcache]
  22. xcache.shm_scheme =        "mmap"
  23. xcache.size  =               256M
  24. xcache.count =                 2
  25. xcache.slots =                8K
  26. xcache.ttl   =                 86400
  27. xcache.gc_interval =          3600
  28. xcache.var_size  =            64M

#更改标红参数,生产环境根据硬件和业务数据来调整

  1. [root@Web_server xcache-3.2.0]# /usr/local/php/bin/php -v    #检查配置
  2. PHP 5.5.31 (cli) (built: Apr 13 2016 10:29:58)
  3. Copyright (c) 1997-2015 The PHP Group
  4. Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies
  5. with XCache v3.2.0, Copyright (c) 2005-2014, by mOo
  6. with XCache Cacher v3.2.0, Copyright (c) 2005-2014, by mOo
  7.  
  8. [root@Web_server ~]# echo -n "szk" |md5sum
  9. [root@Web_server ~]# vim /usr/local/php/etc/php.ini   #配置xcache用户密码
  10. xcache.admin.user = "szk"
  11. xcache.admin.pass = "005eff7dd9626f0c30f2eb8003ceafc9"
  12. [root@Web_server ~]# cp -a /usr/local/src/xcache-3.2.0/htdocs/ /data/www/xadmin
  13. [root@Web_server ~]# chown -R nobody.nobody /data/www/xadmin
  14. [root@Web_server ~]# /etc/init.d/php-fpm start

■ 缓存加速插件安装配置(OPcache)
___________________________________________________________
#若5.5版本,会自动安装opcache插件,编译时--enable=opcache启用

  1. [root@Web_server ~]#  ll /usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/  
  2. -rwxr-xr-x 1 root root  538161 Apr 13 10:33 opcache.so
  3. [root@Web_server ~]# vim /usr/local/php/etc/php.ini  #若编译没有启用,则在配置文件中添加下面内容即可
  4. zend_extension=opcache.so
  5. [opcache]
  6. opcache.memory_consumption=128
  7. opcache.interned_strings_buffer=8
  8. opcache.max_accelerated_files=4000
  9. opcache.revalidate_freq=60
  10. opcache.fast_shutdown=1
  11. opcache.enable=1
  12. opcache.enable_cli=1
  13. [root@Web_server ~]# /usr/local/php/bin/php -v
  14. PHP 5.5.31 (cli) (built: Apr 13 2016 10:29:58) 
  15. Copyright (c) 1997-2015 The PHP Group
  16. Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies
  17. with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2015, by Zend Technologies

发布日期:

所属分类: Php 标签: