PHP缓冲加速

目录
一、缓冲加速原理
1、缓冲加速
2、LAMP环境缓冲加速
3、LNMP环境缓冲加速
二、缓冲加速环境
1、Linux版本检查
2、Nginx版本检查
3、PHP版本检查
4、MySQL版本检查
三、插件环境安装配置
1、前置环境
❶环境变量LC_ALL
❷Perl依赖
2、数据库缓冲
❶Memcached
❷PDO_MYSQL
3、图片处理扩展
❶lmageMagick
❷imagick
4、配置Memcache/ PDO _MYSQL/ imagick
⑴ 配置
⑵ 重启
⑶ 编写
⑷ 测试
四、插件安装配置
1、eAccelerator
⑴ 简介
⑵ 安装
⑶ 配置
❶ 缓冲目录及权限
❷ 配置参数
⑷ 测试
2、XCache
⑴ 简介
⑵ 安装
⑶ 配置
⑷ 测试
3、配置ZendOpcache
⑴ 简介
⑵ 安装
⑶ 配置
⑷ 测试
五、缓冲加速优化
1、缓冲目录优化
2、共享内存优化
六、webbench 压力测试
1、简介
2、安装
3、参数
4、测试
⑴ 测百度
⑵ 测博客
5、说明
参考
一、缓冲加速原理
1、缓冲加速
操作码:客户端请求PHP程序,服务器的PHP引擎会解析该PHP程序,并将其编译为特定的操作码文件(Operate Code,简称opcode文件,二进制形式)。默认情况下,操作码文件由PHP引擎执行后丢弃。
缓冲加速原理:将编译后的操作码保存下来,并放到共享内存里,以便在下一次调用该PHP页面时重用它,避免了相同代码的重复编译。
优点:节省了PHP引擎重复编译的时间,降低了服务器负载,同时减少了CPU和内存开销。
2、LAMP环境缓冲加速
❶Apache接收客户的PHP程序请求,并根据规则过滤之。

❷Apache将PHP程序请求传递给PHP处理模块libphp5.so 。

❸PHP引擎定位磁盘上的PHP文件,并将其加载到内存中解析。

❹PHP处理模块libphp5.so将PHP源代码编译成为opcode。

❺PHP处理模块libpbp5.so执行opcode, 然后把opcode 缓存起来。

❻Apache接收客户端新的PHP程序请求,PHP 引擎直接读取缓存执行opcode文件,并将结果返回。在这一次任务中,就无第4步的编译解析了,从而提升了PHP 编译解析效率。

3、LNMP环境缓冲加速


❶Nginx 接收客户端的PHP程序访问请求。

❷Nginx 根据扩展名等过滤规则将PHP程序请求传递给解析PHP的FCGI (phpfpm)进程。

❸PHP-FPM进程调用PHP解析器读取站点磁盘上的PHP文件,并加载到内存中。

❹PHP解析器将PHP程序编译成为opcode文件,然后把opcode缓存起来。

❺PHP-FPM引擎执行opcode 树后,返回数据给Nginx, 进而返回客户端。

❻Nginx 接收客户新的PHP程序请求, PHP-FPM 引擎就会直接读取缓存中的opcode并执行,将结果返回。该过程中无需第4 步操作,从而提升了PHP 编译解析效率。

❼如果访问数据库,程序首先检查客户端请求的数据在Memcached 服务的缓存中是否存在,如果存在,直接把请求的数据返回,不再请求后端数据库。

❽如果请求的数据在Memcached 缓存中不存在,则程序会去Memcahced 后端的数据库服务。把从数据库中取到的数据返回给客户端。

❾同时把新取到的数据库的数据缓存一份到Memcached 服务缓存中,下次同样的请求就直接从Memcacbed 服务缓存返回数据,从而减轻数据库的访问压力。

二、缓冲加速环境
LNMP完整环境:LNMP架构部署WordPress论坛

1、Linux版本检查
[root@www ~]# cat /etc/redhat-release
CentOS release 6.6 (Final)
[root@www ~]# uname -r
2.6.32-504.30.3.el6.x86_64
[root@www ~]# uname -m
x86_64
1
2
3
4
5
6
2、Nginx版本检查
[root@www ~]# /application/php/sbin/nginx -v
nginx version: nginx/1.6.3
1
2
3、PHP版本检查
[root@www ~]# /application/php/bin/php -v
PHP 5.3.27 (cli) (built: May 14 2021 10:24:11) 
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2013 Zend Technologies
1
2
3
4
4、MySQL版本检查
[root@www ~]# mysqladmin -uroot -pxxxx version
mysqladmin  Ver 8.42 Distrib 5.5.32, for linux2.6 on x86_64
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Server version          5.5.32
Protocol version        10
Connection              Localhost via UNIX socket
UNIX socket             /tmp/mysql.sock
Uptime:                 42 min 54 sec

Threads: 1  Questions: 1  Slow queries: 0  Opens: 35  Flush tables: 1  Open tables: 4  Queries per second avg: 0.000
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
三、插件环境安装配置
1、前置环境
❶环境变量LC_ALL
locale环境与LC_ALL=C
LC_ALL=C:去除所有本地化的LC_*设置
解决报警:perl: warning: Falling back to the standard locale ("C").
[root@www ~]# vim /etc/profile

export LANGUAGE="en_US.UTF-8:zh_CN.UTF-8"
export LANG=en_US:zh_CN.UTF-8
export LC_ALL=C
"/etc/profile" 81L, 1895C 已写入                               
[root@www ~]# source /etc/profile
1
2
3
4
5
6
7
❷Perl依赖
ImageMagick的环境
[root@www ~]# yum install perl-devel -y
Installed:
  perl-devel.x86_64 4:5.10.1-136.el6_6.1                                         

Dependency Installed:
  db4-cxx.x86_64 0:4.7.25-19.el6_6                                               
  db4-devel.x86_64 0:4.7.25-19.el6_6                                             
  gdbm-devel.x86_64 0:1.8.0-36.el6                                               
  perl-ExtUtils-MakeMaker.x86_64 0:6.55-136.el6_6.1                              
  perl-ExtUtils-ParseXS.x86_64 1:2.2003.0-136.el6_6.1                            
  perl-Test-Harness.x86_64 0:3.17-136.el6_6.1 
[root@www ~]# yum install perl-CPAN -y
Installed:
  perl-CPAN.x86_64 0:1.9402-136.el6_6.1                                          

Dependency Installed:
  perl-Digest-SHA.x86_64 1:5.47-136.el6_6.1 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2、数据库缓冲
❶Memcached
简介:一个开源的、支持高性能、高并发及分布式的内存缓存服务软件。
原理:将网站中经常被读取的对象或数据,保存到事先规划好的系统内存空间。当用户访问时,先检查Memcached服务中的缓冲,如果存在直接返回。如果不存在再访问数据库。访问数据库时,也会将数据缓冲一份到Memcached服务中,下次请求时直接返回。
优点:降低高峰期前端业务对数据库的直接高并发访问,提升大规模网站集群中动态服务的并发访问能力。
Memcached分为服务端和客户端两部分。
以下为客户端安装步骤。
配置参数:./configure --enable-memcache --with-php-config=/application/php/bin/php-config
[root@www ~]# cd /home/centos/tools
[root@www tools]# rz
[root@www tools]# tar zxf memcache-2.2.7.tgz
[root@www tools]# cd memcache-2.2.7
[root@www memcache-2.2.7]# /application/php/bin/phpize
Configuring for:
PHP Api Version:         20090626
Zend Module Api No:      20090626
Zend Extension Api No:   220090626
[root@www memcache-2.2.7]# ./configure --enable-memcache --with-php-config=/application/php/bin/php-config
[root@www memcache-2.2.7]# make
[root@www memcache-2.2.7]# make install
Installing shared extensions:     /application/php/lib/php/extensions/no-debug-non-zts-20090626/
[root@www memcache-2.2.7]# cd ..
[root@www tools]# ls -l /application/php/lib/php/extensions/no-debug-non-zts-20090626/ 
total 244
-rwxr-xr-x. 1 root root 246472 May 17 06:41 memcache.so
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
❷PDO_MYSQL
简介:为PHP访问数据库定义了一个轻量级一致性的接口
作用:提供一个数据访问抽象层,通过这个抽象层,无论使用的是什么数据库,都可以通过一致的函数执行查询并获取数据。
配置参数:./configure --with-php-config=/application/php/bin/php-config --with-pdo-mysql=/application/mysql
[root@www tools]# rz
[root@www tools]# tar xf PDO_MYSQL-1.0.2.tgz
[root@www tools]# cd PDO_MYSQL-1.0.2/
[root@www PDO_MYSQL-1.0.2]# /application/php/bin/phpize
Configuring for:
PHP Api Version:         20090626
Zend Module Api No:      20090626
Zend Extension Api No:   220090626
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
[root@www PDO_MYSQL-1.0.2]# ./configure --with-php-config=/application/php/bin/php-config --with-pdo-mysql=/application/mysql
[root@www PDO_MYSQL-1.0.2]# make
[root@www PDO_MYSQL-1.0.2]# make install
Installing shared extensions:     /application/php/lib/php/extensions/no-debug-non-zts-20090626/
[root@www PDO_MYSQL-1.0.2]# ls -l /application/php/lib/php/extensions/no-debug-non-zts-20090626/
total 400
-rwxr-xr-x. 1 root root 246472 May 17 06:41 memcache.so
-rwxr-xr-x. 1 root root 155836 May 17 06:51 pdo_mysql.so
[root@www PDO_MYSQL-1.0.2]# cd ..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
3、图片处理扩展
❶lmageMagick
简介:一套功能强大、稳定而且免费的图片工具集和开发包
用途:用来读、写和处理超过89 种基本格式的图片文件,包括流行的tiff 、jpeg 、gif 、png 、pdf, 以及PhotoCD 等。
依赖:perl-devel库
配置参数./configure
[root@www tools]# rz
[root@www tools]# tar xf ImageMagick-6.7.9-10.tar.xz
[root@www tools]# cd ImageMagick-6.7.9-10/
[root@www ImageMagick-6.7.9-10]# ./configure
[root@www ImageMagick-6.7.9-10]# make
make  all-am
make[1]: Entering directory `/root/PDO_MYSQL-1.0.2/ImageMagick-6.7.9-10'

make[1]: Leaving directory `/root/PDO_MYSQL-1.0.2/ImageMagick-6.7.9-10'

[root@www ImageMagick-6.7.9-10]# make install
make  install-am
make[1]: Entering directory `/root/PDO_MYSQL-1.0.2/ImageMagick-6.7.9-10'
make[2]: Entering directory `/root/PDO_MYSQL-1.0.2/ImageMagick-6.7.9-10'

make[2]: Leaving directory `/root/PDO_MYSQL-1.0.2/ImageMagick-6.7.9-10'
make[1]: Leaving directory `/root/PDO_MYSQL-1.0.2/ImageMagick-6.7.9-10'
[root@www ImageMagick-6.7.9-10]#  cd ..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
❷imagick
简介:一个供PHP调用ImageMagick功能的扩展模块
依赖:ImageMagick
配置参数:./configure --with-php-config=/application/php/bin/php-config
[root@www tools]# rz
[root@www tools]# tar zxf imagick-2.3.0.tgz
[root@www tools]# cd imagick-2.3.0
[root@www imagick-2.3.0]# /application/php/bin/phpize
Configuring for:
PHP Api Version:         20090626
Zend Module Api No:      20090626
Zend Extension Api No:   220090626
[root@www tools]# ./configure --with-php-config=/application/php/bin/php-config
[root@www tools]# make
[root@www imagick-2.3.0]# make install
Installing shared extensions:     /application/php/lib/php/extensions/no-debug-non-zts-20090626/
[root@www imagick-2.3.0]# ls -l /application/php/lib/php/extensions/no-debug-non-zts-20090626/
total 1448
-rwxr-xr-x. 1 root root 1072953 May 17 07:29 imagick.so
-rwxr-xr-x. 1 root root  246472 May 17 06:41 memcache.so
-rwxr-xr-x. 1 root root  155836 May 17 06:51 pdo_mysql.so
[root@www imagick-2.3.0]# cd ..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
4、配置Memcache/ PDO _MYSQL/ imagick
⑴ 配置
备份配置文件cp php.ini php.ini.bak
添加模块引用路径extension_dir = "/application/php/lib/php/extensions/no-debug-non-zts-20090626/"
引用模块extension=memcache.so
[root@www xcache-3.2.0]# cd /application/php/lib
[root@www lib]# ls -l
[root@www lib]# cp php.ini php.ini.bak
[root@www lib]# ls -l
total 140
drwxr-xr-x. 15 root root  4096 May 17 06:41 php
-rw-r--r--.  1 root root 69627 May 14 10:40 php.ini
-rw-r--r--.  1 root root 69627 May 17 08:03 php.ini.bak
[root@www lib]# vim php.ini
 819 extension_dir = "/application/php/lib/php/extensions/no-debug-non-zts-20090626/"
 820 extension=memcache.so
 821 extension=pdo_mysql.so
 822 extension=imagick.so
1
2
3
4
5
6
7
8
9
10
11
12
13
⑵ 重启
结束进程pkill php-fpm
重启进程/application/php/sbin/php-fpm
检查进程ps -ef | grep php-fpm
[root@www lib]# pkill php-fpm
[root@www lib]# ps -ef | grep php-fpm | grep -v grep
[root@www lib]# /application/php/sbin/php-fpm       
[root@www lib]# ps -ef | grep php-fpm                       
root      43759      1  0 08:47 ?        00:00:00 php-fpm: master process (/application/php/etc/php-fpm.conf)
nginx     43760  43759  0 08:47 ?        00:00:00 php-fpm: pool www            
nginx     43761  43759  0 08:47 ?        00:00:00 php-fpm: pool www            
root      43771   6577  0 08:48 pts/1    00:00:00 grep php-fpm
[root@www lib]# ps -ef | grep php-fpm | grep -v grep | wc -l
3
1
2
3
4
5
6
7
8
9
10
⑶ 编写
编写测试程序vim /application/nginx/html/vps/realm_name/blog/viem_info.php
[root@www lib]# vim /application/nginx/conf/vps/realm_name/blog.conf
server {
        listen       80;
        server_name  blog.test_lnmp.org;

        location / {
            root   html/vps/realm_name/blog;
            index  index.php index.html index.htm;
        }

        location ~ .*\.(php|php5)?$ {
            root            html/vps/realm_name/blog;
            fastcgi_pass    127.0.0.1:9000;
            fastcgi_index   index.php;
            include         /application/nginx/conf/fastcgi.conf;
        }

        access_log logs/access_blog.log main gzip buffer=32k flush=5s;
    }
[root@www lib]# ls -l /application/nginx/html/vps/realm_name/blog
[root@www lib]# vim /application/nginx/html/vps/realm_name/blog/viem_info.php
<?php phpinfo () ; ?>
[root@www lib]# /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx/conf/nginx.conf test is successful 
[root@www lib]# /application/nginx/sbin/nginx -s reload
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
⑷ 测试
访问网址:blog.test_lnmp.org/viem_info.php
页内搜索:memcache、pdo_mysql、imagick


四、插件安装配置
    插件选取:根据实际业务需求,搭建相关环境进行压力测试,然后根据实际测试结果进行选择。

    测试环境:以下三者都可安装,但不能一起配置使用

    实际环境:安装一款与相关服务搭配最合适的即可

1、eAccelerator
⑴ 简介
描述:一款免费、开源的PHP加速、优化及缓存扩展插件。
原理:缓存PHP程序编译后的中间代码文件( opcode) 、session 等数据,存放在共享内存里,然后每次从内存里调用执行,也可以设定把一些不适合放在内存里缓存的编译结果存储到磁盘上。
优点:加快PHP 程序的执行速度,降低服务器负载压力。
⑵ 安装
脚本/application/php/bin/phpize,在./configure前执行。
通过phpize脚本可以建立PHP的外挂模块。在PHP安装编译时未配置的模块,可以通过phpize脚本添加。
配置参数./configure --enable-eaccelerator=shared --with-php-config=/application/php/bin/php-config;PHP安装目录--with-php-config=/application/php/bin/php-config
[root@www tools]# rz
[root@www tools]# tar xf eaccelerator-0.9.6.1.tar.bz2
[root@www tools]# cd eaccelerator-0.9.6.1/
[root@www eaccelerator-0.9.6.1]# /application/php/bin/phpize
Configuring for:
PHP Api Version:         20090626
Zend Module Api No:      20090626
Zend Extension Api No:   220090626
[root@www eaccelerator-0.9.6.1]# ./configure --enable-eaccelerator=shared --with-php-config=/application/php/bin/php-config
[root@www eaccelerator-0.9.6.1]# make
[root@www eaccelerator-0.9.6.1]# make install
[root@www eaccelerator-0.9.6.1]# ls /application/php/lib/php/extensions/no-debug-non-zts-20090626/
eaccelerator.so  imagick.so  memcache.so  pdo_mysql.so
[root@www eaccelerator-0.9.6.1]# cd ..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
⑶ 配置
❶ 缓冲目录及权限
缓冲目录mkdir -p /tmp/eaccelerator
权限chown -R nginx.nginx /tmp/eaccelerator
[root@www lib]# mkdir -p /tmp/eaccelerator
[root@www lib]# chown -R nginx.nginx /tmp/eaccelerator
[root@www lib]# ls -ld /tmp/eaccelerator
drwxr-xr-x. 2 nginx nginx 4096 May 17 09:23 /tmp/eaccelerator
1
2
3
4
❷ 配置参数
参数    说明
[eaccelerator]    eaccelerator加速模块
extension=eaccelerator.so    加载eAccelcrator加速模块。路径相对于extension_dir的配置。
eaccelerator.shm_size=“64”    存储缓存数据的共享内存大小。如果为0, 则最大值看内核配置/proc/sys/kerael/shmmax。
eaccelerator.cache_dir="/tmp/eaccelerator"    磁盘缓存存储路径。缓存内容为precompiled code、session、data、content 和user entries。默认路径为“/tmp/eaccelerator"。
eaccelerator.enable=“1”    eAccelerator 缓存生效开关。1为默认值,即生效;0 为关闭,不生效。
eaccelerator.optimizer=“1”    加速PHP 代码执行速度,1为默认值,表示激活;0 为不激活。用于缓存前的代码加速。
eaccelerator.check_mtime=“1”    检查缓存修改时间,决定代码是否需要重新编译,1为激活,是默认值。
eaccelerator.debug=“0”    缓存加速调试,0为关闭,1为打开,打开后可以看到缓存加速信息。
eaccelerator.filter=""    设定对象是否缓存规则,空表示不设定。
eaccelerator.shm_max=“0”    可以被放罚的缓存最大值,0是不限制。
eaccelerator.shm_ttl=“3600”    缓存文件的生存期。
eaccelerator.shm_prune_period=“3600”    当共享内存空间不够时,从共享内存中移除旧数据的时间周期。
eaccelerator.shm_only=“0”    是否允许缓存数据到磁盘。0为允许,但是对于session data and content caching 无影响。
eaccelerator.compress=“1”    是否开启压缩。1为开启。
eaccelerator.compress_level=“9”    压缩级别。9为最高。
eaccelerator.keys=“shm_and_disk”    控制keys缓存位置。
shm_and_disk: 在共享内存和磁盘上缓存数据(默认值);
shm: 如果共享内存已满或数据大于内核设定值,则在共享内存或磁盘两者择一;
shm_only: 在共享内存上缓存数据;
disk_only: 在磁盘上缓存数据;
none: 不缓冲
eaccelerator.sessions=“shm_and_disk”    控制sessions缓存位置
eaccelerator.content=“shm_and_disk”    控制内容缓存位置
[root@www lib]# vim php.init
[eaccelerator]
extension=eaccelerator.so
eaccelerator.shm_size="64"
eaccelerator.cache_dir="/tmp/eaccelerator"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
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"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
⑷ 测试
测试缓冲配置/application/php/bin/php -v
[root@www lib]# /application/php/bin/php -v 
PHP 5.3.27 (cli) (built: May 14 2021 10:24:11) 
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2013 Zend Technologies
    with eAccelerator v0.9.6.1, Copyright (c) 2004-2010 eAccelerator, by eAccelerator
1
2
3
4
5
重启PHP服务
[root@www lib]# killall php-fpm   
[root@www lib]# ps -ef | grep php-fpm             
[root@www lib]# /application/php/sbin/php-fpm 
[root@www lib]# ps -ef | grep php-fpm 
1
2
3
4
访问检查
访问网址:blog.test_lnmp.org/viem_info.php
页内搜索:eaccelerator


查看缓冲目录

find /tmp/eaccelerator/ -type f | xargs file

查找普通类型(f)的文件find /tmp/eaccelerator/ -type f;

xargs捕获上一个命令find /tmp/eaccelerator/ -type f的结果,传递下一个命令file;

file辨识文件类型。

/tmp/eaccelerator/4/6/eaccelerator-1502.386231: data

内容为eaccelerator-1502.386231

类型为data

[root@www lib]# ls -l /tmp/eaccelerator/
total 64
drwxrwxrwx. 18 root root 4096 May 17 09:29 0
drwxrwxrwx. 18 root root 4096 May 17 09:29 1
drwxrwxrwx. 18 root root 4096 May 17 09:29 2
drwxrwxrwx. 18 root root 4096 May 17 09:29 3
drwxrwxrwx. 18 root root 4096 May 17 09:29 4
drwxrwxrwx. 18 root root 4096 May 17 09:29 5
drwxrwxrwx. 18 root root 4096 May 17 09:29 6
drwxrwxrwx. 18 root root 4096 May 17 09:29 7
drwxrwxrwx. 18 root root 4096 May 17 09:29 8
drwxrwxrwx. 18 root root 4096 May 17 09:29 9
drwxrwxrwx. 18 root root 4096 May 17 09:29 a
drwxrwxrwx. 18 root root 4096 May 17 09:29 b
drwxrwxrwx. 18 root root 4096 May 17 09:29 c
drwxrwxrwx. 18 root root 4096 May 17 09:29 d
drwxrwxrwx. 18 root root 4096 May 17 09:29 e
drwxrwxrwx. 18 root root 4096 May 17 09:29 f
[root@www lib]# find /tmp/eaccelerator/ -type f | xargs file
/tmp/eaccelerator/4/6/eaccelerator-1502.386231: data
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2、XCache
⑴ 简介
描述:一款开源、快速、稳定的PHP优化及缓存器。
原理:把PHP程序编译后的数据(opcode ) 缓存到共享内存里,避免相同的程序重复编译。用户请求相同的PHP 程序时,可以直接使用缓存中已编译好的数据。
优点:提高PHP 的访问速度,降低服务器负载开销。
⑵ 安装
配置参数:./configure --enable-xcache --with-php-config=/application/php/bin/php-config
[root@www tools]# rz
[root@www tools]# tar xf xcache-3.2.0.tar_2.gz
[root@www tools]# cd xcache-3.2.0/
[root@www xcache-3.2.0]# /application/php/bin/phpize
Configuring for:
PHP Api Version:         20090626
Zend Module Api No:      20090626
Zend Extension Api No:   220090626
[root@www xcache-3.2.0]# ./configure --enable-xcache --with-php-config=/application/php/bin/php-config
[root@www xcache-3.2.0]# make
[root@www xcache-3.2.0]# make install
[root@www xcache-3.2.0]# ls /application/php/lib/php/extensions/no-debug-non-zts-20090626/
eaccelerator.so  imagick.so  memcache.so  pdo_mysql.so  xcache.so
[root@www xcache-3.2.0]# cd ..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
⑶ 配置
参数    说明
[xcache-common]
extension=xcache.so    加载xcache.so, 路径相对于extension_dir的配置。
[xcache.admin]
xcache.admin.enable_auth=On
xcache.admin.user="mOo"
xcache.admin.pass=“md5 encrypted password”    激活管理员认证。指定XCache管理员用户名和密码,密码根据http://xcache.lighttpd.net/demo/cacher/mkpassword.php地址产生,留空表示禁止管理页面
[xcache]    XCache缓存参数配置段
xcache.shm_scheme=“mmap”    设置XCache 如何从系统分配共享内存
xcache.size=256M    0为禁止缓存,非0则启用缓存。需要注意系统所允许的mmap最大值
xcache.count=2    指定将Cache切分成多少块,官方推荐设置为服务器CPU 的数量
xcache.slots=8K    hash槽个数的参考值,缓冲超过此数值时也没有任何问题
xcache.ttl=86400    设定Cache对象的生存期TTL (time to live ), 0 为永不过期
xcache.gc_interval=3600    回收器扫描过期的对象回收内存空间的间隔,0为不扫描,其他值的单位是秒
xcache.var_size=64M
xcache.var_count=1
xcache.var_slots=8K
xcache.var_ttl=0
xcache.var_gc_interval=300    变量缓存
xcache.readonly_protection=Off    如果启用了该参数,将略微降低性能,但会提高一定的安全系数。这个选项对于xcacbe.mmap_patb = /dev/zero无效
xcache.mmap_path="/dev/zero"    对于nix, xcache.mmap_path是一个文件路径而非目录。如果要启用该参数,请使用"/tmp/xcache"这样的路径,而不是"/dev/"。如果开启了xcache. readonly_protection参数,不同进程组的PHP将不会共享同一个/tmp/xcache路径
xcache.coredump_directory=""    当XCache crash后,是否把数据保存到指定路径
xcache.disable_on_crash=Off    当XCache发生crash 时,自动关闭XCache 缓存
去除注释egrep -v ";|^$" /home/centos/tools/xcache-3.2.0/xcache.ini
匹配所有注释";|^$":注释符号;,^$匹配从开头到结尾
-v参数反选
[root@www lib]# egrep -v ";|^$" /home/centos/tools/xcache-3.2.0/xcache.ini
[root@www lib]# vim php.ini
[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
xcache.var_count =             1
xcache.var_slots =            8K
xcache.var_ttl   =             0
xcache.var_maxttl   =          0
xcache.var_gc_interval =     300
xcache.var_namespace_mode =    0
xcache.var_namespace =        ""
xcache.readonly_protection = Off
xcache.mmap_path =    "/dev/zero"
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 = ""
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
⑷ 测试
测试缓冲配置/application/php/bin/php -v
[root@www lib]# /application/php/bin/php -v 
PHP 5.3.27 (cli) (built: May 14 2021 10:24:11) 
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2013 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
1
2
3
4
5
6
生成及配置用户账号密码
MD5信息摘要算法进行加密echo -n "xxxx" | md5sum
[root@www lib]# echo -n "xxxx" | md5sum
81dc9bdb52d04dc20089dbd8313ed055  -
[root@www lib]# vim php.ini
[xcache.admin]
xcache.admin.enable_auth = On
xcache.admin.user = "centos"
xcache.admin.pass = "81dc9bdb52d04dc20089dbd8313ed055"
1
2
3
4
5
6
7
复制XCacbe软件目录的缓存加速管理程序到站点目录下
cp -a htdocs/ /application/nginx/html/vps/realm_name/blog/Xcache
赋予Nginx执行权限
chown -R nginx.nginx /application/nginx/html/vps/realm_name/blog/Xcache
[root@www xcache-3.2.0]# cd /home/centos/tools/xcache-3.2.0
[root@www xcache-3.2.0]# cp -a htdocs/ /application/nginx/html/vps/realm_name/blog/Xcache
[root@www xcache-3.2.0]# chown -R nginx.nginx /application/nginx/html/vps/realm_name/blog/Xcache
1
2
3
重启PHP服务
[root@www xcache-3.2.0]# pkill php-fpm
[root@www xcache-3.2.0]# /application/php/sbin/php-fpm 
1
2
访问测试:blog.test_lnmp.org/Xcache/index.php
用上边设置的账号密码登录

3、配置ZendOpcache
⑴ 简介
描述:官方PHP缓存加速插件
原理:把PHP程序编译后的数据(opcode ) 缓存到共享内存里,避免相同的程序重复编译。用户请求相同的PHP 程序时,可以直接使用缓存中已编译好的数据。
⑵ 安装
在安装PHP时安装ZendOpcache,添加配置参数 --enable-opcacbe,即可。
配置参数:./configure --enable-opcache --with-php-config=/application/php/bin/php-config
[root@www tools]# rz
[root@www tools]# tar xf zendopcache-7.0.5.tgz
[root@www tools]# cd zendopcache-7.0.5
[root@www zendopcache-7.0.5]# /application/php/bin/phpize
Configuring for:
PHP Api Version:         20090626
Zend Module Api No:      20090626
Zend Extension Api No:   220090626
[root@www zendopcache-7.0.5]# ./configure --enable-opcache --with-php-config=/application/php/bin/php-config
[root@www zendopcache-7.0.5]# make
[root@www zendopcache-7.0.5]# make install
Installing shared extensions:     /application/php/lib/php/extensions/no-debug-non-zts-20090626/
[root@www zendopcache-7.0.5]# ls /application/php/lib/php/extensions/no-debug-non-zts-20090626/
eaccelerator.so  imagick.so  memcache.so  opcache.so  pdo_mysql.so  xcache.so
[root@www zendopcache-7.0.5]# cd ..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
⑶ 配置
参数    说明
opcache.memory_consumption=128    OPcache共享内存空间大小,用于存放precompiled PHP code,默认为64, 单位为Mbytes
opcache.interned_strings_buffer=8    默认值为4,interned strings 内存的数量,单位是M
opcache.max_accelerated_files=4000    默认值为2000, OPcache散列表的key的最大数量
opcache.revalidate_freq=60    默认值为2, 检查文件时间戳的频率,用于共享内存分配的变化
opcache.fast_shutdown=1    默认值为0,如果激活,一个快速的关闭队列将被用来加速代码
opcache.enable_cli=1    默认值为0,激活PHP_CLI的OPcache,用于测试和调试
extension=opcache.so必须注释掉,否则报错
[root@www ~]# cd /application/php/lib
[root@www lib]# vim php.ini
[opcache]
zend_extension=/application/php/lib/php/extensions/no-debug-non-zts-20090626/opcache.so
;extension=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
1
2
3
4
5
6
7
8
9
10
11
⑷ 测试
测试缓冲配置/application/php/bin/php -v
[root@www lib]# /application/php/bin/php -v
PHP Warning:  PHP Startup: Invalid library (maybe not a PHP library) 'opcache.so' in Unknown on line 0
PHP 5.3.27 (cli) (built: May 14 2021 10:24:11) 
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2013 Zend Technologies
    with Zend OPcache v7.0.5, Copyright (c) 1999-2015, by Zend Technologies
1
2
3
4
5
6
重启PHP服务
[root@www lib]# killall php-fpm  
[root@www lib]# /application/php/sbin/php-fpm 
1
2
访问测试
访问网站:blog.test_lnmp.org/viem_info.php
页内搜索:ZendOpcache


五、缓冲加速优化
1、缓冲目录优化
简介:tmpfs是一种基于内存的文件系统。 通常使用tmpfs作为数据临时存储,比本地磁盘存储快很多。
优化:将/tmp/eaccelerator挂载到tmpfs文件系统上。
[root@www lib]# vim /etc/fstab
tmpfs    /tmp/eaccelerator    tmpfs   rw,relatime,size=16384k    0    0
[root@www lib]# mount -a
[root@www lib]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3        19G  2.9G   15G  17% /
tmpfs           491M     0  491M   0% /dev/shm
/dev/sda1       190M   53M  128M  30% /boot
tmpfs            16M     0   16M   0% /tmp/eaccelerator
[root@www lib]# grep eacc /proc/mounts
tmpfs /tmp/eaccelerator tmpfs rw,rootcontext=unconfined_u:object_r:user_tmp_t:s0,seclabel,relatime,size=16384k 0 0
1
2
3
4
5
6
7
8
9
10
11
2、共享内存优化
XCache和eAccelerator均使用系统的共享内存作为存储空间
优化:调整系统内核与共享内存大小相关参数
[root@www lib]# vim /etc/sysctl.conf
 30 # Controls the default maxmimum size of a mesage queue
 31 kernel.msgmnb = 65536
 32 
 33 # Controls the maximum size of a message, in bytes
 34 kernel.msgmax = 65536
 35 
 36 # Controls the maximum shared segment size, in bytes
 37 kernel.shmmax = 68719476736
 38 
 39 # Controls the maximum number of shared memory segments, in pages
 40 kernel.shmall = 4294967296
1
2
3
4
5
6
7
8
9
10
11
12
六、webbench 压力测试
1、简介
Webbench知名网站压力测试工具。
测试处在相同硬件上不同服务的性能或不同硬件上同一个服务的运行状况。
webbench的标准测试为每秒钟相应请求数和每秒钟传输数据量。
Webbench最多可以模拟3万个并发连接去测试网站的负载能力。
2、安装
依赖:ctags
存在webbench-1.5/Makefile,无需./configure
[root@www ~]# yum install  -y ctags
[root@www ~]# cd /home/centos/tools
[root@www tools]# rz
[root@www tools]# tar xf webbench-1.5.tar_3.gz
[root@www tools]# cd webbench-1.5
[root@www webbench-1.5]# ./configure
-bash: ./configure: No such file or directory
[root@www webbench-1.5]# tree -L 2
.
|-- COPYRIGHT -> debian/copyright
|-- ChangeLog -> debian/changelog
|-- Makefile
|-- debian
|   |-- changelog
|   |-- control
|   |-- copyright
|   |-- dirs
|   `-- rules
|-- socket.c
|-- webbench.1
`-- webbench.c
[root@www webbench-1.5]# make

install: cannot create regular file `/usr/local/man/man1': No such file or directory
make: *** [install] Error 1
[root@www webbench-1.5]# mkdir -p /usr/local/man/man1
[root@www webbench-1.5]# make install
[root@www webbench-1.5]# make clean
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
3、参数
[root@www webbench-1.5]# webbench
webbench [option]... URL
  -f|--force               Don't wait for reply from server.
  -r|--reload              Send reload request - Pragma: no-cache.
  -t|--time <sec>          Run benchmark for <sec> seconds. Default 30.
  -p|--proxy <server:port> Use proxy server for request.
  -c|--clients <n>         Run <n> HTTP clients at once. Default one.
  -9|--http09              Use HTTP/0.9 style requests.
  -1|--http10              Use HTTP/1.0 protocol.
  -2|--http11              Use HTTP/1.1 protocol.
  --get                    Use GET request method.
  --head                   Use HEAD request method.
  --options                Use OPTIONS request method.
  --trace                  Use TRACE request method.
  -?|-h|--help             This information.
  -V|--version             Display program version.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
4、测试
⑴ 测百度
30秒内100并发webbench -c 100 -t 30 http://www.baidu.com/
[root@www tools]# webbench -c 100 -t 30 http://www.baidu.com/
Webbench - Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.

Benchmarking: GET http://www.baidu.com/
100 clients, running 30 sec.

Speed=612 pages/min, 2354904 bytes/sec.
Requests: 264 susceed, 42 failed.
1
2
3
4
5
6
7
8
9
⑵ 测博客


进行域名解析echo "192.168.10.10 blog.test_lnmp.org" >>/etc/hosts
[root@www tools]# webbench -c 100 -t 30 http://blog.test_lnmp.org/
Webbench - Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.

Benchmarking: GET http://blog.test_lnmp.org/
100 clients, running 30 sec.

Connect to server failed. Aborting benchmark.
[root@www tools]# ping http://blog.test_lnmp.org/
ping: unknown host http://blog.test_lnmp.org/
[root@www tools]# echo "192.168.10.10 blog.test_lnmp.org" >>/etc/hosts
[root@www tools]# tail -1 /etc/hosts
192.168.10.10 blog.test_lnmp.org
[root@www tools]# ping -c 3 blog.test_lnmp.org
PING blog.test_lnmp.org (192.168.10.10) 56(84) bytes of data.
64 bytes from blog.test_lnmp.org (192.168.10.10): icmp_seq=1 ttl=64 time=0.009 ms
64 bytes from blog.test_lnmp.org (192.168.10.10): icmp_seq=2 ttl=64 time=0.018 ms
64 bytes from blog.test_lnmp.org (192.168.10.10): icmp_seq=3 ttl=64 time=0.017 ms

--- blog.test_lnmp.org ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 1999ms
rtt min/avg/max/mdev = 0.009/0.014/0.018/0.006 ms
[root@www tools]# webbench -c 100 -t 30 http://blog.test_lnmp.org/
Webbench - Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.

Benchmarking: GET http://blog.test_lnmp.org/
100 clients, running 30 sec.

Speed=2010 pages/min, 226692 bytes/sec.
Requests: 1005 susceed, 0 failed.
[root@www tools]# webbench -c 500 -t 30 http://blog.test_lnmp.org/ 
Webbench - Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.

Benchmarking: GET http://blog.test_lnmp.org/
500 clients, running 30 sec.

Speed=7268 pages/min, 255686 bytes/sec.
Requests: 3634 susceed, 0 failed.
[root@www tools]# webbench -c 1000 -t 30 http://blog.test_lnmp.org/ 
Webbench - Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.

Benchmarking: GET http://blog.test_lnmp.org/
1000 clients, running 30 sec.

Speed=717534 pages/min, 148908 bytes/sec.
Requests: 3428 susceed, 355339 failed.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
5、说明
❶压力测试工作应该放到产品上线之前,而不是上线以后 ;

❷测试的时候,最好把测试机的监控都打开;

❸测试时尽量跨公网进行,而不是内网;

❹测试时并发应当由小逐渐加大,比如并发100时观察一下网站负载是多少、打开是否流畅,并发200时又是多少、网站打开缓慢时并发是多少、网站打不开时并发又是多少 ;

❺应尽量进行单元测试,如B2C网站可以着重测试购物车、推广页面等,因为这些页面占整个网站访问量比重较大;

❻获得的数据越多越准确。

参考
1、跟老男孩学Linux运维:Web集群实战
2、LC_ALL=C的含义
3、WebBench详解
————————————————
版权声明:本文为CSDN博主「我以我剑荡苍穹」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/yueshangxigelou/article/details/116914645

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值