架构—扩展php的memcache模块及构建nginx高速缓存"/>
lamp架构—扩展php的memcache模块及构建nginx高速缓存
扩充php模块memcache
[root@foundation50 lamp]# scp memcache-4.0.5.2.tgz server1:/mnt/ 拷贝memcache-4.0.5.2.tgz到server1的mnt里
[root@server1 ~]# cd /mnt/ 进入
[root@server1 mnt]# tar zxf memcache-4.0.5.2.tgz 解压文件
[root@server1 mnt]# cd memcache-4.0.5.2/ 进入解压目录
[root@server1 memcache-4.0.5.2]# ls 发现没有configure执行脚本
cloudbuild.yaml config.m4 CREDITS Dockerfile LICENSE php7 tests
config9.m4 config.w32 docker example.php memcache.php README
[root@server1 memcache-4.0.5.2]# phpize 所以运行phpzie,生成执行脚本
[root@server1 memcache-4.0.5.2]# yum install autoconf -y 安装
[root@server1 memcache-4.0.5.2]# phpize 再次执行phpize 完成预编译环境, 执行这个命令时注意定义php启动路经/usr/local/php/bin
Configuring for:
PHP Api Version: 20190902
Zend Module Api No: 20190902
Zend Extension Api No: 320190902
[root@server1 memcache-4.0.5.2]# ls 可以发现已经生成了configure
[root@server1 memcache-4.0.5.2]# ./configure --help 可以查看帮助
[root@server1 memcache-4.0.5.2]# ./configure --enable-memcache 执行,后面参数不加也可以
[root@server1 memcache-4.0.5.2]# make 编译
[root@server1 memcache-4.0.5.2]# make install 安装
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20190902/
[root@server1 etc]# cd /usr/local/php/etc/ 进入php主配置目录
[root@server1 etc]# vim php.ini 编辑主配置文件
[root@server1 etc]# systemctl reload php-fpm.service 重载服务
[root@server1 etc]# php -m | grep memcache -m表示列出php所有模块
memcache 模块已经存在
[root@server1 etc]# yum install memcached -y 安装memcached
[root@server1 ~]# systemctl start memcached 启动
[root@server1 ~]# netstat -antlp | grep memcache 查看端口,端口已经开启为11211
tcp 0 0 0.0.0.0:11211 0.0.0.0:* LISTEN 7047/memcached
tcp6 0 0 :::11211 :::* LISTEN
[root@server1 ~]# cd /mnt/memcache-4.0.5.2/ 进入原码目录
[root@server1 memcache-4.0.5.2]# cp example.php /usr/local/nginx/html/ 将example.php拷贝到默认发布目录里
访问172.25.50.1/example.php ,可以访问到
[root@server1 memcache-4.0.5.2]# cp memcache.php /usr/local/nginx/html/ 将监控页面memcache.php放到nginx默认发布目录
[root@server1 memcache-4.0.5.2]# cd /usr/local/nginx/html/
[root@server1 html]# vim memcache.php 编辑
访问172.25.50.1/memcacahe.php
以上就是如何扩展php的方法,其他模块网上下载,然后同样按此方法操作
构建nginx高速缓存
传统缓存策略
nginx只能处理静态----动态交给 fastcgi ----> 递交给后端php-fpm 9000处理的时候从 memcache 取 交给nginx ----再返回给client
但是有弊端:nginx高并发并不能体现出来,因为nginx处理动态页面要交给后端php,php处理完成后,才能反馈给nginx
测试:传统缓存方式下进行压测
[root@foundation50 ~]# ab -c10 -n5000 http://172.25.50.1/example.php
高效缓存:
所以我们可以把memcache前置,直接交给nginx ,这样效率就会大大提高,如下图:
如何实现高效缓存,需要nginx有两个模块memc+srcache,现在推荐nginx另外一个发行版openresty
[root@server1 html]# systemctl stop nginx.service 首先停掉nginx ,因为nginx和openresty端口都是80,会起冲突
[root@foundation50 lamp]# scp openresty-1.19.3.1.tar.gz server1:/mnt 将以下载好的openresty包拷贝到server1的mnt里
[root@server1 mnt]# tar zxf openresty-1.19.3.1.tar.gz 解压
[root@server1 mnt]# cd openresty-1.19.3.1/ 进入解压后的目录
[root@server1 openresty-1.19.3.1]# ls
bundle configure COPYRIGHT patches README.markdown README-windows.txt util
[root@server1 openresty-1.19.3.1]# ./configure --prefix=/usr/local/openresty --with-http_ssl_module --with-http_stub_status_module 执行
[root@server1 openresty-1.19.3.1]# make 编译
[root@server1 openresty-1.19.3.1]# make install 安装
[root@server1 ~]# cd /usr/local/openresty/nginx/conf/ 进入配置目录
[root@server1 conf]# vim nginx.conf 编辑配置文件
[root@server1 conf]# /usr/local/openresty/nginx/sbin/nginx -t 检测语法
nginx: the configuration file /usr/local/openresty/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/openresty/nginx/conf/nginx.conf test is successful 成功
[root@server1 conf]# /usr/local/openresty/nginx/sbin/nginx 运行nginx
[root@server1 conf]# cd ..
[root@server1 nginx]# cd html/ 进入nginx默认发布目录
[root@server1 html]# vim index.php 创建php首页
<?php
phpinfo()
?>
访问172.25.50.1/index.php,可以访问成功
[root@server1 nginx]# cd conf/ 进入nginx配置目录
[root@server1 conf]# vim nginx.conf 编辑配置文件
}
原理:首先调用srcache_fetch在自己的mecache定位一下key,看有没有用户需要的数据,有的话直接从memcache取,没有的话通过fastcgi_cgi调用后端的php去取,取回后调用srcache_store上传到memcache,下次调用时直接在memcache去取
[root@server1 conf]# /usr/local/openresty/nginx/sbin/nginx -t 检测语法,成功
nginx: the configuration file /usr/local/openresty/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/openresty/nginx/conf/nginx.conf test is successful
[root@server1 conf]# /usr/local/openresty/nginx/sbin/nginx -s reload 重载服务
[root@server1 conf]# cd ..
[root@server1 nginx]# cd html/ 进入默认发布目录
[root@server1 html]# ls
50x.html index.html index.php
[root@server1 html]# cp /usr/local/nginx/html/example.php . 拷贝
访问 172.25.50.1/example.php,成功
[root@foundation50 Desktop]# ab -c10 -n5000 http://172.25.50.1/example.php 压力测试
可以看出高效缓存每秒访问12994.24,传统缓存每秒访问1865个,提高了10倍
更多推荐
lamp架构—扩展php的memcache模块及构建nginx高速缓存
发布评论