LNMP企业实战案例- php + memcache提高网站访问速度

1.Memcache简介

Memcached是一个自由开源的,高性能,分布式内存对象缓存系统。用于动态Web应用以减轻数据库的负载。它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高了网站访问的速度。

MemCaChe是一个存储键值对的HashMap,在内存中对任意的数据(比如字符串、对象等)所使用的key-value存储,数据可以来自数据库调用、API调用,或者页面渲染的结果。

memcache和memcached区别:

  • memcache是项目的名称
  • memcache是服务器端可以执行的文件的名称

2.php增加memcache模块实验过程

(1)将php的bin目录路径增加到~/.bash_profile中,为了方便调用

[root@server1 ~]# vim ~/.bash_profile

10 PATH=$PATH:$HOME/bin:/usr/local/lnmp/mysql/bin:/usr/local/lnmp/php/bin

[root@server1 memcache-4.0.5.2]# source /root/.bash_profile   #重载bash_profile文件中的环境变量

(2)解压

注意:memcache目录本来没有configure文件,所以没法编译

需要先执行命令:phpizephpize是用来扩展php扩展模块的,通过phpize可以建立php的外挂模块,执行该命令需要先安装autoconf软件:

[root@server1 memcache-4.0.5.2]# yum install autoconf -y
[root@server1 memcache-4.0.5.2]# phpize
Configuring for:
PHP Api Version:         20190902
Zend Module Api No:      20190902
Zend Extension Api No:   320190902

(3)编译安装memcache

[root@server1 memcache-4.0.5.2]# ./configure --with-php-config=/usr/local/lnmp/php/bin/php-config
[root@server1 memcache-4.0.5.2]# make && make install

注意安装完成会生成如下目录
在这里插入图片描述
(4)修改php主配置文件,增加模块

[root@server1 ~]# cd /usr/local/lnmp/php/etc/
[root@server1 etc]# vim php.ini 
修改:
 761 extension_dir = "/usr/local/lnmp/php/lib/php/extensions/no-debug-non-zts-20     190902/"   #修改成编译安装将生成的目录
 912 extension=memcache.so  #添加模块

(5)重新加载或启动php

[root@server1 memcache-4.0.5.2]# /etc/init.d/php-fpm reload			#没有启动时,使用start命令启动
[root@server1 memcache-4.0.5.2]# php -m | grep memcache
memcache

(6)安装memcached

 yum install -y memcached

(7)查看memcached的配置文件

[root@server1 memcache-4.0.5.2]# cd /etc/sysconfig/
[root@server1 sysconfig]# cat memcached 
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS=""					#如果写为OPTIONS=-l 127.0.0.1,即为只监听本机的11211端口

(8)启动memcached

[root@server1 sysconfig]# systemctl start memcached 

(9)使用telnet测试

[root@server1 sysconfig]# yum install telnet -y

[root@server1 sysconfig]# telnet localhost 11211
Trying ::1...
Connected to localhost.
Escape character is '^]'.
set name 0 0 6			# 0编号 0缓存时间,此处0代表不限制时间  6限制字符数
westos
STORED
get name
VALUE name 0 6
westos
END
delete name
DELETED
get name
END
set name 0 5 6			#设置缓存时间为5s,5s后再获取name为空
westos
STORED
get name
END
quit		#退出

(10)编辑测试页面

[root@server1 sysconfig]# cd /root/lnmp/memcache-4.0.5.2
[root@server1 memcache-4.0.5.2]# cp memcache.php example.php /usr/local/lnmp/nginx/html/
[root@server1 memcache-4.0.5.2]# cd /usr/local/lnmp/nginx/html/
[root@server1 html]# vim memcache.php 
#修改密码与memcache服务器地址IP与端口
 
 22 define('ADMIN_USERNAME','memcache');    // Admin Username
 23 define('ADMIN_PASSWORD','westos');      // Admin Password     

 28 $MEMCACHE_SERVERS[] = '172.25.1.1:11211'; // add more as an array
 29 #$MEMCACHE_SERVERS[] = 'mymemcache-server2:11211'; // add more as an array

浏览器输入http://172.25.1.1/memcache.php测试:
在这里插入图片描述

在这里插入图片描述
表示配置成功,若提示:Sorry, the page you are looking for is currently unavailable. Please try aga......,可以使用命令/etc/init.d/php-fpm start启动php-fpm再试即可成功

(11)使用真机客户端测试

测试访问没有memcache的php页面时,发现速度很慢而且很多失败的:

ab -c 10 -n 5000 http://172.25.1.1/index.php

在这里插入图片描述
在这里插入图片描述
测试访问有memcache的php页面时,发现速度很快而且没有失败的:

[root@foundation1 ~]# ab -c 10 -n 5000 http://172.25.1.1/example.php

在这里插入图片描述
在这里插入图片描述

发布了149 篇原创文章 · 获赞 1 · 访问量 3019

猜你喜欢

转载自blog.csdn.net/qq_36417677/article/details/104737297
今日推荐