1.将编译完成的php二进制命令加入环境变量中,可以直接调用php命令
[root@server1 ~]# vim ~/.bash_profile
PATH=$PATH:$HOME/bin:/usr/local/lnmp/mysql/bin:/usr/local/lnmp/php/bin
[root@server1 ~]# source ~/.bash_profile
2.解压memcache源码包
[root@server1 ~]# tar zxf memcache-2.2.5.tgz
3.创建预编译环境
[root@server1 ~]# cd memcache-2.2.5
[root@server1 memcache-2.2.5]# ls
[root@server1 memcache-2.2.5]# phpize ##生成对应的configure文件
[root@server1 memcache-2.2.5]# ls
[root@server1 memcache-2.2.5]# ./configure
4.编译并安装memcache
[root@server1 memcache-2.2.5]# make && make install
5.php的配置文件中添加memcache模块
[root@server1 ~]# cd /usr/local/lnmp/php/etc/
[root@server1 etc]# ls
pear.conf php-fpm.conf php-fpm.conf.default php.ini
[root@server1 etc]# vim php.ini
extension=memcache.so
查找memcache模块的路径
[root@server1 etc]# cd /usr/local/lnmp/php/lib/php/extensions/no-debug-non-zts-20131226/
[root@server1 no-debug-non-zts-20131226]# ls
memcache.so opcache.a opcache.so
6.重新加载php服务
[root@server1 etc]# /etc/init.d/php-fpm reload
7.查看php支持的模块
[root@server1 etc]# php -m
[root@server1 etc]# php -m | grep mysql ##支持mysql模块
mysql
mysqli
mysqlnd
pdo_mysql
[root@server1 etc]# php -m | grep memcache ##支持 memcache模块
memcache
8.安装memcached(memcache管理工具)并开启memcache
[root@server1 ~]# yum install memcached -y
[root@server1 ~]# /etc/init.d/memcached start
9.查看memcached的端口号为11211
[root@server1 ~]# netstat -tnlp
10.安装telnet
[root@server1 ~]# yum install telnet -y
[root@server1 ~]# telnet localhost 11211 测试本机11211端口
Trying ::1...
Connected to localhost.
Escape character is '^]'.
stats ##查看状态
STAT pid 10114
STAT uptime 211
STAT time 1556347865
STAT version 1.4.4
STAT pointer_size 64
STAT rusage_user 0.004999
STAT rusage_system 0.003999
STAT curr_connections 10
STAT total_connections 12
STAT connection_structures 11
STAT cmd_get 0
STAT cmd_set 0
STAT cmd_flush 0
STAT get_hits 0
STAT get_misses 0
STAT delete_misses 0
STAT delete_hits 0
STAT incr_misses 0
set name 0 5 6 ##0:表示编号 5:缓存时间 6:限制的字符数
westos
STORED
westossos
ERROR
get name
END
[root@server1 ~]# telnet localhost 11211
Trying ::1...
Connected to localhost.
Escape character is '^]'.
set name 0 10 6
westos
STORED
get name
VALUE name 0 6
westos
END
get name
END
quit
Connection closed by foreign host.
11.将example.php memcache.php 复制到 /usr/local/lnmp/nginx/html,并设置memcache密码
[root@server1 ~]# cd memcache-2.2.5
[root@server1 memcache-2.2.5]# cp example.php memcache.php /usr/local/lnmp/nginx/html
[root@server1 memcache-2.2.5]# cd /usr/local/lnmp/nginx/html
[root@server1 html]# ls
[root@server1 html]# vim memcache.php
12.浏览器中访问172.25.7.1/memcache.php,需要输入用户和密码才能登陆
输入用户名和密码点击ok登陆成功
重新打开一个浏览器,访问172.25.7.1/example.php,并不断刷新
当多次访问example.php后,memcache.php的命中情况会不断提高
13.压力测试:加速页面example.php和没加速页面index.php的对比
(1)访问index.php,显示ab这个命令找不到
[root@server1 ~]# ab -c 10 -n 5000 http://172.25.7.1/index.php
-bash: ab: command not found
(2)安装 httpd-tools
[root@server1 ~]# yum install httpd-tools.x86_64 -y
(3)再次访问index.php
[root@server1 ~]# ab -c 10 -n 5000 http://172.25.7.1/index.php
总结:所用时间为7秒左右,出错个数为345个
(4)访问example.php
[root@server1 ~]# ab -c 10 -n 5000 http://172.25.7.1/example.php
总结:所用时间为2秒左右,出错个数为0个,速度提高了,出错率降低了