php的加速器

php的加速器

php的加速器:基于PHP的特殊扩展机制如opcode缓存扩展也可以将opcode缓存于php的共享内存中,从而可以让同一段代码的后续重复执行时跳过编译阶段以提高性能。这些加速器并非真正提高了opcode的运行速度,而仅是通过分析opcode后并将它们重新排列以达到快速执行的目的

常见的第三方php加速器

1、APC (Alternative PHP Cache)
遵循PHP License的开源框架,PHP opcode缓存加速器,目前的版本不适用于PHP 5.4
项目地址:http://pecl.php.net/package/APC
2、eAccelerator
源于Turck MMCache,早期的版本包含了一个PHP encoder和PHP loader,目前encoder已经不在支持。项目地址 :http://eaccelerator.net/
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功能的扩展。项目地址http://www.zend.com/en/products/guard/runtime-decoders
5、NuSphere PhpExpress
NuSphere的一款开源PHP加速器,它支持装载通过NuSphere PHP Encoder编码的PHP程序文件,并能够实现对常规PHP文件的执行加速。项目地址,http://www.nusphere.com/products/phpexpress.htm

实现 xcache 加速 php 5.X

编译php-xcache加速访问,支持php 5.6版以下
官网:http://xcache.lighttpd.net/wiki/ReleaseArchive

案例:CentOS 7上安装清华源的php56,并编译安装 xcache加速

[root@centos7 ~]#yum -install https://mirrors.tuna.tsinghua.edu.cn/remi/enterprise/remi-release-7.rpm
[root@centos7 ~]#yum -y install php56-php php56-php-mysqlnd mariadb-server
[root@centos7 ~]#systemctl enable --now httpd mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
[root@centos7 ~]#mysql
MariaDB [(none)]> create database wordpress;
MariaDB [(none)]> grant all on wordpress.* to wordpress@'localhost' identified by 'magedu';
[root@centos7 ~]#tar xvf wordpress-5.3.2-zh_CN.tar.gz  -C /var/www/html
[root@centos7 ~]#cd /var/www/html
[root@centos7 ~]#chown -R apache.apache wordpress/
[root@centos8 ~]#ab -c 10 -n 100 http://10.0.0.7/wordpress/
......
Requests per second:    3.16 [#/sec] (mean)
......

#安装编译xcache
[root@centos7 ~]#yum -y install gcc  php56-php-devel
#下载并解压缩xcache-3.2.0.tar.bz2
[root@centos7 ~]#tar xf xcache-3.2.0.tar.gz
#生成编译环境
[root@centos7 ~]#cd xcache-3.2.0/
[root@centos7 xcache-3.2.0]#/opt/remi/php56/root/usr/bin/phpize
Configuring for:
PHP Api Version:         20131106
Zend Module Api No:      20131226
Zend Extension Api No:   220131226
[root@centos7 xcache-3.2.0]#./configure --enable-xcache --with-php-config=/opt/remi/php56/root/usr/bin/php-config
[root@centos7 xcache-3.2.0]#make && make install
...省略...
Installing shared extensions:     /opt/remi/php56/root/usr/lib64/php/modules/

[root@centos7 xcache-3.2.0]#cat xcache.ini  >> /opt/remi/php56/root/etc/php.ini
#安装base源中执行即可cp xcache.ini  /etc/php.d/
[root@centos7 ~]#systemctl restart httpd.service

#测试性能
[root@centos8 ~]#ab -c10 -n 100 http://LAMP服务器/wordpress
Requests per second:    7.26 [#/sec] (mean)
Bash

opcache加速php 7.X

[root@centos8 ~]#dnf install php-opcache
[root@centos8 ~]#cat  /etc/php.ini
[opcache]
zend_extension=opcache.so                            
opcache.enable=1
Bash

范例:CentOS 8 实现opache 加速

[root@centos8 ~]#dnf -y install httpd php php-mysqlnd mariadb-server php-opcache php-json
[root@centos8 ~]#rpm -ql php-opcache
/etc/php.d/10-opcache.ini
/etc/php.d/opcache-default.blacklist
/usr/lib/.build-id
/usr/lib/.build-id/71
/usr/lib/.build-id/71/55ebb00f7ebcab9d708c1d5c7b7e634cce259c
/usr/lib64/php/modules/opcache.so
[root@centos8 ~]#grep opcache /etc/php.d/10-opcache.ini
zend_extension=opcache
opcache.enable=1
...省略...

#加速前
#禁用加速
[root@centos8 ~]#vim /etc/php.d/10-opcache.ini
opcache.enable=0
[root@centos8 ~]#systemctl restart php-fpm
[root@centos7 ~]#ab -c 10 -n 100 http://10.0.0.8/wordpress/
......
Requests per second:    4.31 [#/sec] (mean)
......

#启用加速
[root@centos8 ~]#vim /etc/php.d/10-opcache.ini
opcache.enable=1
[root@centos8 ~]#systemctl restart php-fpm

##加速后
[root@centos7 ~]#ab -c 10 -n 100 http://10.0.0.8/wordpress/
......
Requests per second:    19.76 [#/sec] (mean)
......























###############################################################################################################################




一、opcache(官方推出的产品还是好用的)
opcache 通过将 PHP 脚本预编译的字节码存储到共享内存中来提升 PHP 的性能, 存储预编译字节码的好处就是 省去了每次加载和解析 PHP 脚本的开销。

PHP 5.5.0 及后续版本中已经绑定了 opcache 扩展。 对于 PHP 5.2,5.3 和 5.4 版本可以使用PECL扩展中的 opcache 库。

windows下的php扩展下载地址:
http://windows.php.net/downloads/pecl/releases/
1
配置如下:
打开php.ini文件,找到[opcache]

; dll地址
extension=php_opcache.dll
; 开关打开
opcache.enable=1
; 开启CLI
opcache.enable_cli=1
; 可用内存, 酌情而定, 单位为:Mb
opcache.memory_consumption=128
; Zend Optimizer + 暂存池中字符串的占内存总量.(单位:MB)
opcache.interned_strings_buffer=8
; 对多缓存文件限制, 命中率不到 100% 的话, 可以试着提高这个值
opcache.max_accelerated_files=10000
; opcache 会在一定时间内去检查文件的修改时间, 这里设置检查的时间周期, 默认为 2, (单位:秒)
opcache.revalidate_freq=1
; 打开快速关闭, 打开这个在PHP Request Shutdown的时候回收内存的速度会提高
opcache.fast_shutdown=1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
二、APC
Alternative PHP Cache (APC) 是一个开放自由的PHP opcode缓存。它的目标是提供一个自由、 开放,和健全的框架用于缓存和优化PHP的中间代码。
- 下载apc扩展dll,选择你对应的PHP版本

http://windows.php.net/downloads/pecl/releases/apc/
1
把下载的php_apc.dll放入php的ext扩展目录下。
打开php.ini文件,配置如下:
[apc]
extension=php_apc.dll
apc.enabled=1
; 共享内存块的数目
apc.shm_segments=1
; 共享内存块的大小(单位:MB)
apc.shm_size=64
; 优化级别,更高的值则使用更主动的优化
apc.optimization=1
; 源文件的数目,不确定设置为0
apc.num_files_hint=0
; 缓存条目在缓冲区中允许逗留的秒数
apc.ttl=7200
; 针对每个用户缓存条目在缓冲区中允许逗留的秒数
apc.user_ttl=7200
; 缓存条目在垃圾回收表中能够存在的秒数
apc.gc_ttl=7200
; 文件写锁
apc.write_lock=on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
三、xcache(国产)
xcache是一个开源的 opcode 缓存器/优化器。
- 下载xcache,选择对应PHP版本的xcache
http://xcache.lighttpd.net/pub/Releases/
1
把php_xcache.dll放到php的ext目录下。
打开php.ini,配置如下:
[xcache-common]
extension = php_xcache.dll
[xcache.admin]
xcache.admin.enable_auth = On
xcache.admin.user = "admin"
xcache.admin.pass = "md5后你的密码"
[xcache]
; 选择底层内存共享实现方案
xcache.shm_scheme = "mmap"
xcache.size = 128M
; 设置为CPU数
xcache.count = 1
; 只是个参考值
xcache.slots = 8K
; 缓存时间
xcache.ttl = 1200
; 垃圾回收的时间间隔
xcache.gc_interval = 1200
; 同上,针对变量缓存设置
xcache.var_size = 4M
xcache.var_count = 1
xcache.var_slots = 8K
xcache.var_ttl = 1200

; 变量最大缓存时间
xcache.var_maxttl = 7200
xcache.var_gc_interval = 1200
xcache.var_namespace_mode = 0
xcache.var_namespace = ""
xcache.readonly_protection = Off
; 对于win系统,这里不是文件路径
xcache.mmap_path = "xcache"
xcache.coredump_directory = ""
xcache.coredump_type = 0
xcache.disable_on_crash = Off
xcache.experimental = Off
xcache.cacher = On
xcache.stat = On
xcache.optimizer = Off
[xcache.coverager]
xcache.coverager = Off
xcache.coverager_autostart = On
xcache.coveragedump_directory = ""


 

 

参考地址         https://blog.csdn.net/Fa_Ker/article/details/79769873

 

posted @ 2021-05-20 11:46  woaibaobei  阅读(164)  评论(0编辑  收藏  举报