LNMP模块——Memcache实现php页面的加速缓存

续我的博文:https://mp.****.net/postedit/87886098即lnmp架构已经搭建好。

 

一、Memcache简介:

 

  • memcache是一套分布式的高速缓存系统,由LiveJournal的Brad Fitzpatrick开发,但目前被许多网站使用以提升网站的访问速度,尤其对于一些大型的、需要频繁访问数据库的网站访问速度提升效果十分显著 [1] 。这是一套开放源代码软件,以BSD license授权发布。
  • Memcache是一个高性能的分布式的内存对象缓存系统,通过在内存里维护一个统一的巨大的hash表,它能够用来存储各种格式的数据,包括图像、视频、文件以及数据库检索的结果等。简单的说就是将数据调用到内存中,然后从内存中读取,从而大大提高读取速度
  • memcached 是高性能的分布式内存缓存服务器。一般的使用目的是,通过缓存数据库查询结果,减少数据库访问次数,以提高动态 Web 应用的速度、提高可扩展性。

 

二、实验环境(rhel6.5版本)

主机环境:rhel6.5 selinux 和iptables 都必须是disabled状态

各主机信息

主机名 IP 服务
server1 172.25.83.1 lnmp架构
物理机 172.25.83.83 用于测试

 

三、Memcache实现php页面的加速缓存

 

1、Memcache的部署:

 

(1)官网下载Memcache安装包并解压

[[email protected] ~]# tar zxf memcache-2.2.5.tgz 
[[email protected] ~]# cd memcache-2.2.5
[[email protected] memcache-2.2.5]# ls
config9.m4   memcache.c                  memcache_queue.h
config.m4    memcache_consistent_hash.c  memcache_session.c
config.w32   memcache.dsp                memcache_standard_hash.c
CREDITS      memcache.php                php_memcache.h
example.php  memcache_queue.c            README

 

LNMP模块——Memcache实现php页面的加速缓存

 

(2)添加php环境变量并刷新

[[email protected] memcache-2.2.5]# vim ~/.bash_profile   #添加php环境变量
PATH=$PATH:$HOME/bin:/usr/local/lnmp/mysql/bin:/usr/local/lnmp/php/bin
[[email protected] memcache-2.2.5]# source ~/.bash_profile   #刷新环境变量


[[email protected] memcache-2.2.5]# php   #按两次tab键补全。来验证php的环境变量是否配置成功
php         php-cgi     php-config  phpize

 

LNMP模块——Memcache实现php页面的加速缓存

 

LNMP模块——Memcache实现php页面的加速缓存

 

(3)配置编译环境

phpize是用来扩展php扩展模块的,通过phpize可以建立php的外挂模块,比如你想在原来编译好的php中加入memcached或者ImageMagick等扩展模块,可以使phpize。

LNMP模块——Memcache实现php页面的加速缓存

 

[[email protected] memcache-2.2.5]# ./configure 

LNMP模块——Memcache实现php页面的加速缓存

 

(4)编译安装

[[email protected] memcache-2.2.5]# make && make install

 

LNMP模块——Memcache实现php页面的加速缓存

 

(5)给php添加Memcache模块

[[email protected] memcache-2.2.5]# cd /usr/local/lnmp/php/etc/
[[email protected] etc]# vim php.ini   #编辑配置文件
 873 extension=memcache.so   #将873行注释去掉
[[email protected] etc]# /etc/init.d/php-fpm reload   #修改完配置文件之后,重新加载配置文件
[[email protected] etc]# php -m | grep memcache   #过滤Memcache模块,发现则表示memcache模块添加成功
memcache

 

LNMP模块——Memcache实现php页面的加速缓存

 

LNMP模块——Memcache实现php页面的加速缓存

 

LNMP模块——Memcache实现php页面的加速缓存

 

(6)安装Memcache服务,并开启

[[email protected] etc]# yum install -y memcached   #其主配置文件为/etc/sysconfig/memcached
[[email protected] etc]# /etc/init.d/memcached start   #开启memcached服务
[[email protected] etc]# netstat -antulpe | grep 11211   #查看11211端口是否存在
tcp        0      0 0.0.0.0:11211               0.0.0.0:*                   LISTEN      498        11434      3809/memcached      
tcp        0      0 :::11211                    :::*                        LISTEN      498        11435      3809/memcached      
udp        0      0 0.0.0.0:11211               0.0.0.0:*                               498        11438      3809/memcached      
udp        0      0 :::11211                    :::*                                    498        11439      3809/memcached   

 

LNMP模块——Memcache实现php页面的加速缓存

 


补充内容:我们现在看到memcached服务所监听的端口为0.0.0.0:11211,如果我们想让memcached服务只监听127.0.0.1:11211,那该如何做呢?

答:修改memcached服务的主配置文件/etc/sysconfig/memcached

[[email protected] etc]# vim /etc/sysconfig/memcached   #将第5行内容修改为如下的内容
  1 PORT="11211"
  2 USER="memcached"
  3 MAXCONN="1024"
  4 CACHESIZE="64"
  5 OPTIONS="-l 127.0.0.1"

[[email protected] etc]# /etc/init.d/memcached reload   #编辑完配置文件之后重载memcached服务
Stopping memcached:                                        [  OK  ]
Starting memcached:                                        [  OK  ]

[[email protected] etc]# netstat -antulpe | grep 11211   #验证监听的端口是否为127.0.0.1:11211
tcp        0      0 127.0.0.1:11211             0.0.0.0:*                   LISTEN      498        11626      3841/memcached      
udp        0      0 127.0.0.1:11211             0.0.0.0:*                               498        11628      3841/memcached  

 


为了不影响后续的实验,我们将侦听端口该为默认的0.0.0.0:11211

[[email protected] etc]# vim /etc/sysconfig/memcached   #将第5行内容恢复为原来默认的样子
  1 PORT="11211"
  2 USER="memcached"
  3 MAXCONN="1024"
  4 CACHESIZE="64"
  5 OPTIONS=""

[[email protected] etc]# /etc/init.d/memcached reload   #编辑完配置文件之后重载memcached服务
Stopping memcached:                                        [  OK  ]
Starting memcached:                                        [  OK  ]

[[email protected] etc]# netstat -antulpe | grep 11211
tcp        0      0 0.0.0.0:11211               0.0.0.0:*                   LISTEN      498        11811      3868/memcached      
tcp        0      0 :::11211                    :::*                        LISTEN      498        11812      3868/memcached      
udp        0      0 0.0.0.0:11211               0.0.0.0:*                               498        11815      3868/memcached      
udp        0      0 :::11211                    :::*                                    498        11816      3868/memcached 

测试:

[[email protected] etc]# telnet localhost 11211
Trying ::1...
Connected to localhost.
Escape character is '^]'.
stats      #查看状态
STAT pid 3868
STAT uptime 880
STAT time 1556508210
STAT version 1.4.4
STAT pointer_size 64
STAT rusage_user 0.020996
STAT rusage_system 0.024996
STAT curr_connections 10
STAT total_connections 12
...
END
set name 0 0 6     #设定name(相当于字典的键值),其中name这个名字随意给。第一个0:编号;第二个0:缓存时间(0表示一直缓存,不过期);6:字符数
westos
STORED
westosa
ERROR
get name
VALUE name 0 6
westos
END
delete name       #删除name
DELETED
get name
END
set name 0 10 6      #设定缓存时间为10秒(从敲下这行命令就开始计时间)   
westos
STORED
get name
VALUE name 0 6
westos
END
get name
VALUE name 0 6
westos
END
get name   #缓存10秒之后,name的值就不存在了
END
quit   #输入"quit"退出
Connection closed by foreign host.

 

LNMP模块——Memcache实现php页面的加速缓存

 

LNMP模块——Memcache实现php页面的加速缓存

 

LNMP模块——Memcache实现php页面的加速缓存

 

LNMP模块——Memcache实现php页面的加速缓存

 

(7)Memcache实现网页登陆查看

[[email protected] etc]# cd ~/memcache-2.2.5
[[email protected] memcache-2.2.5]# cp memcache.php example.php /usr/local/lnmp/nginx/html/
   #拷贝.php文件到nginx默认发布目录下,以便进行测试
 23 define('ADMIN_PASSWORD','redhat');      // Admin Password   #修改登录密码
 28 $MEMCACHE_SERVERS[] = '172.25.83.1:11211'; // add more as an array   #更改ip
 29 #$MEMCACHE_SERVERS[] = 'mymemcache-server2:11211'; // add more as an array   #注释这行(29行)

 

LNMP模块——Memcache实现php页面的加速缓存

 

2、测试:

 

(1)网页访问:172.25.83.1/example.php

 

LNMP模块——Memcache实现php页面的加速缓存

 

(2)网页访问:172.25.83.1/memcache.php,进行登陆(用户:memcache ;密码:redhat(上面配置文件中修改的密码)),出现memcache页面

 

LNMP模块——Memcache实现php页面的加速缓存
 

LNMP模块——Memcache实现php页面的加速缓存

 

LNMP模块——Memcache实现php页面的加速缓存

 

LNMP模块——Memcache实现php页面的加速缓存

 

右上角可以查看缓存命中率

 

(3)访问速度测试:
 

【1】、访问index.php,可以查看到访问时间和出错情况

[[email protected] Desktop]# ab -c 10 -n 5000 http://172.25.83.1/index.php   #10并发 5000次(10个用户同时访问5000次)

 

LNMP模块——Memcache实现php页面的加速缓存

 

LNMP模块——Memcache实现php页面的加速缓存

 

【2】、真机访问example.php,可以查看到速度非常快,出错少(因为访问example.php时,直接从缓存中读取)

[[email protected] Desktop]# ab -c 10 -n 5000 http://172.25.83.1/example.php  

 

LNMP模块——Memcache实现php页面的加速缓存

 

LNMP模块——Memcache实现php页面的加速缓存

 

网页查看缓存命中率为100%

LNMP模块——Memcache实现php页面的加速缓存