添加环境变量
[root@server1 ~]# vim ~/.bash\_profile
[root@server1 ~]# source ~/.bash\_profile
测试:
三、php添加memcache功能模块(高速缓存)
memcache介绍
memcache是一套分布式的高速缓存系统,由LiveJournal的Brad Fitzpatrick开发,但目前被许多网站使用以提升网站的访问速度,尤其对于一些大型的、需要频繁访问数据库的网站访问速度提升效果十分显著 [1] 。这是一套开放源代码软件,以BSD license授权发布。
软件下载
1.软件安装:
这里用到的是memcache-4.0.5.2.tgz
[root@server1 ~]# tar zxf memcache-4.0.5.2.tgz
[root@server1 ~]# cd memcache-4.0.5.2/
[root@server1 memcache-4.0.5.2]# phpize
[root@server1 memcache-4.0.5.2]# yum install autoconf
[root@server1 memcache-4.0.5.2]# phpize
[root@server1 memcache-4.0.5.2]# ./configure
[root@server1 memcache-4.0.5.2]# make
[root@server1 memcache-4.0.5.2]# make install
phpize是用来扩展php扩展模块的,通过phpize可以建立php的外挂模块
2.配置php加载模块
[root@server1 no-debug-non-zts-20190902]# cd /usr/local/php/etc/
[root@server1 etc]# vim php.ini
extension=memcache.so
[root@server1 etc]# systemctl reload php-fpm.service
[root@server1 etc]# php -m |grep memcache
memcache
加入memcache模块
[root@server1 etc]# yum install memcached -y
[root@server1 etc]# systemctl start memcached
[root@server1 etc]# netstat -antlp | grep 11211
[root@server1 etc]# cp /root/memcache-4.0.5.2/example.php memcache.php /usr/local/nginx/html/
[root@server1 html]# vim memcache.php
配置memcache登陆信息
测试
此时的命中率为93,再次访问时,查看命中率的改变:(即进入example.php界面刷新,然后再次进入memcache.php界面,查看命中率):此时,命中率有了一定的提高,意思是,所读取的内容是从缓存中进行拿取的,提高了速度,也减轻了后台服务器的压力
[root@server1 ~]# yum install telnet -y ##高速缓存测试软件
[root@server1 ~]# telnet localhost 11211
add key flag expiretime bytes
key : 给这个值设置一个名字;
flag : 标志,是一个整数;
expiretime : 有效期,以秒为单位,0表示没有延迟;
bytes : 这是一个需要存储在memcached的数据的长度;
value : 是一个需要存储的数据。数据需要将通过在新的一行后输入
四、构建nginx高速缓存
使用memc-nginx和srcache-nginx模块构建高效透明的缓存机制
传统缓存策略
高效缓存策略
1.软件下载(openresty)
OpenResty(又称:ngx_openresty) 是一个基于 NGINX 的可伸缩的 Web 平台,由中国人章亦春发起,提供了很多高质量的第三方模块。
OpenResty 是一个强大的 Web 应用服务器,Web 开发人员可以使用 Lua 脚本语言调动 Nginx 支持的各种 C 以及 Lua 模块,更主要的是在性能方面,OpenResty可以 快速构造出足以胜任 10K 以上并发连接响应的超高性能 Web 应用系统。
360,UPYUN,阿里云,新浪,腾讯网,去哪儿网,酷狗音乐等都是 OpenResty 的深度用户。
软件下载
软件安装:
# wget https://openresty.org/package/rhel/openresty.repo
# mv openresty.repo /etc/yum.repos.d/
# yum install -y openresty
# rpm -ql openresty-1.19.3.1-1.el7.x86\_64 ##查看文件配置信息
# ps aux | grep nginx ##不能存在nginx 的进程,如果有的话,kill掉
软件配置:
# cd /usr/local/openresty/nginx/conf
# vim nginx.conf
其余部分和nginx 配置一样,以下仅展示不同处
upstream memcache {
server 127.0.0.1:11211;
keepalive 512; //保持512个不立即关闭的连接用于提升性能
}
location /memc {
internal; //表示只接受内部访问
memc_connect_timeout 100ms;
memc_send_timeout 100ms;
memc_read_timeout 100ms;
set $memc\_key $query\_string; //使用内置的$query\_string来作为key
set $memc\_exptime 300; //表示缓存失效时间
memc_pass memcache;
}
location ~ \.php$ {
set $key $uri$args;
srcache_fetch GET /memc $key;
srcache_store PUT /memc $key;
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
#fastcgi\_param SCRIPT\_FILENAME /scripts$fastcgi\_script\_name;
include fastcgi.conf;
}
当所请求的uri以“.php”结尾时,首先到memcache中查询有没有以$uri$args为key的数据,如果有则直接返回;否则,执行location的逻辑,如果返回的http状态码为200,则在输出前以$uri$args为key,将输入结果存入memcache。
# systemctl start openresty
# vim /usr/local/openresty/nginx/html/index.php
<?php
phpinfo()
?>
测试:
ab -c 100 -n 10000 http://172.25.1.1/index.php
我们可以看到压测速度很快,且没有报错,速度很快。
通过来回切换观察两者速度,及出错数目。
nginx结合lua
location @client {
proxy_pass http://172.25.0.3;
}
location ~ \.php$ {
default_type text/html;
content_by_lua 'ngx.say("this is westos.org")';
access_by_lua '
if ngx.var.remote\_addr == "172.25.0.250" then
ngx.exec("@client")
**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**
**[需要这份系统化的资料的朋友,可以点击这里获取!](https://bbs.csdn.net/topics/618542503)**
**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**