LNMP架构memcache+php缓存拓展和Openresty配置nginx前端缓存

实验环境:前面我们搭建好的LNMP架构

基于LNMP架构添加Memcached缓存

Memcached简介

memcached是一种缓存软件,以键值的形式缓存数据,支持缓存数据大大小为48kb到1Mb,memcached只是将缓存数据写在内存中,不会写入硬盘,同时,memcached缓存不可架设高可用,但是可以部署分布式memcached缓存服务器。而对于同样具有缓存功能的redis来说。redis是一种负载型数据结构缓存,如一键多值,redis也在内存中缓存,但是它可以将数据写入磁盘,是一种数据结构性NOSQL型数据库,不支持SQL语句查询

架构示意图

在这里插入图片描述

LNMP和LAMP架构的区别

在这里插入图片描述
1、LNMP方式的优点:占用VPS资源较少,Nginx配置起来也比较简单,利用fast-cgi的方式动态解析PHP脚本。缺点:php-fpm组件的负载能力有限,在访问量巨大的时候,php-fpm进程容易僵死,容易发生502 bad gateway错误。

2、基于 LAMP 架构设计具有成本低廉、部署灵活、快速开发、安全稳定等特点,是 Web 网络应用和环境的优秀组合。若是服务器配置比较低的个人网站,当然首选 LNMP 架构。当然,在大流量的时候。把Apache和Nginx结合起来使用,也不失为一个不错选择。

添加memcache

1.获取安装包,解压

在这里插入图片描述

tar zxf memcache-2.2.5.tgz 
cd memcache-2.2.5
 vim ~/.bash_profile

在这里插入图片描述

  source ~/.bash_profile 
   phpize
[root@server1 memcache-2.2.5]# phpize
Configuring for:
PHP Api Version:         20131106
Zend Module Api No:      20131226
Zend Extension Api No:   220131226

2.编译安装

cd memcache-2.2.5
./configure 
make && make install 

3.修改配置文件

cd /usr/local/lnmp/php/etc/
 vim php.ini 

在这里插入图片描述

/etc/init.d/php-fpm reload

4.打开memcache端口

  php -m

在这里插入图片描述

[root@server1 no-debug-non-zts-20131226]# php -m | grep memcachememcache

5.安装memcached

yum install -y memcached
vim /etc/sysconfig/memcached
在这里插入图片描述

  /etc/init.d/memcached start
[root@server1 ~]# netstat -tnlp   ##查看端口
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   
tcp        0      0 0.0.0.0:11211               0.0.0.0:*                   LISTEN      3909/memcached      
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      1226/nginx          
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      882/sshd            
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      960/master          
tcp        0      0 127.0.0.1:9000              0.0.0.0:*                   LISTEN      3879/php-fpm        
tcp        0      0 :::11211                    :::*                        LISTEN      3909/memcached      
tcp        0      0 :::22                       :::*                        LISTEN      882/sshd            
tcp        0      0 ::1:25                      :::*                        LISTEN      960/master          
tcp        0      0 :::3306                     :::*                        LISTEN      1182/mysqld         

6.修改发布页

 cd memcache-2.2.5
 cp example.php memcache.php /usr/local/lnmp/nginx/html/
 vim /usr/local/lnmp/nginx/html/memcache.php  ##设置登陆密码及端口
 文件修改内容如下:
 $VERSION='$Id: memcache.php,v 1.2 2008/09/11 19:21:06 mikl Exp $';

define('ADMIN_USERNAME','memcache');    // Admin Username
define('ADMIN_PASSWORD','redhat');      // Admin Password
define('DATE_FORMAT','Y/m/d H:i:s');
define('GRAPH_SIZE',200);
define('MAX_ITEM_DUMP',50);

$MEMCACHE_SERVERS[] = '172.25.61.1:11211'; // add more as an array   ##本机ip

7.测试

访问example页面

在这里插入图片描述

访问memcache页面,输入密码

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

在上面我们可以看到命中率,不断刷新example页面会提高命中率

在这里插入图片描述

8.换一台客户机对比速度

对页面进行10个并发,50000次请求,可以对比时间差异

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

Document Path:          /index.php
Document Length:        84192 bytes

Concurrency Level:      10
Time taken for tests:   10.604 seconds
Complete requests:      5000
Failed requests:        495

ab -c 10 -n 5000 http://172.25.61.1/example.php

Concurrency Level:      10
Time taken for tests:   1.497 seconds
Complete requests:      5000
Failed requests:        0
Write errors:           0
Total transferred:      1400000 bytes
HTML transferred:       615000 bytes
Requests per second:    3340.88 [#/sec] (mean)
Time per request:       2.993 [ms] (mean)
Time per request:       0.299 [ms] (mean, across all concurrent requests)
Transfer rate:          913.52 [Kbytes/sec] received

Openresty的引入

OpenResty是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项。用于方便地搭建能够处理超高并发、扩展性极高的动态 Web 应用、Web 服务和动态网关。

OpenResty 通过汇聚各种设计精良的 Nginx 模块(主要由 OpenResty 团队自主开发),从而将 Nginx 有效地变成一个强大的通用 Web 应用平台。这样,Web 开发人员和系统工程师可以使用 Lua 脚本语言调动 Nginx 支持的各种 C 以及 Lua 模块,快速构造出足以胜任 10K 乃至 1000K 以上单机并发连接的高性能 Web 应用系统。

OpenResty目标是让你的Web服务直接跑在 Nginx 服务内部,充分利用 Nginx 的非阻塞 I/O 模型,不仅仅对 HTTP 客户端请求,甚至于对远程后端诸如 MySQL、PostgreSQL、Memcached 以及 Redis 等都进行一致的高性能响应。

1.获取安装包,解压

在这里插入图片描述

tar zxf openresty-1.13.6.1.tar.gz
cd openresty-1.13.6.1
./configure

2.编译安装

 gmake && gmake install
 nginx -s stop  ##停掉之前的nginx服务
  cd /usr/local/openresty/
cd nginx/html/
cp /usr/local/lnmp/nginx/html/index.php .
 cp /usr/local/lnmp/nginx/html/example.php .

3.修改配置文件

vim /usr/local/openresty/nginx/html/index.php

<?php
phpinfo()
?>

vim /usr/local/openresty/nginx/conf/nginx.conf

对文件做以下修改
  2 user  nginx nginx;
  3 worker_processes  auto;
  4 
  5 #error_log  logs/error.log;
  6 #error_log  logs/error.log  notice;
  7 #error_log  logs/error.log  info;
  8 
  9 #pid        logs/nginx.pid;
 10 
 11 
 12 events {
 13     worker_connections  1024;
  17 http {
 18     include       mime.types;
 19     default_type  application/octet-stream;
 20         upstream memcache {
 21         server localhost:11211;
 22         keepalive 512;
 23 
 24 }
 40     server {
 41         listen       80;
 42         server_name  localhost;
 43 
 44         #charset koi8-r;
 45 
 46         #access_log  logs/host.access.log  main;
 47 
 48         location / {
 49             root   html;
 50             index  index.html index.htm;
 51         }
 52         location /memc {
 53                 internal;
 54                 memc_connect_timeout 100ms;
 55                 memc_send_timeout 100ms;
 56                 memc_read_timeout 100ms;
 57                 set $memc_key $query_string;
 58                 set $memc_exptime 300;
 59                 memc_pass memcache;
 60 }

 79         location ~ \.php$ {
 80             set $key $uri$args;
 81             srcache_fetch GET /memc $key;
 82             srcache_store PUT /memc $key;
 83             root           html;
 84             fastcgi_pass   127.0.0.1:9000;
 85             fastcgi_index  index.php;
 86         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
 87             include        fastcgi.conf;
 88         }

4.测试

[root@foundation61 yasuo]# ab -c 10 -n 5000 http://172.25.61.1/index.php
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 172.25.61.1 (be patient)
Completed 500 requests
Completed 1000 requests
Completed 1500 requests
Completed 2000 requests
Completed 2500 requests
Completed 3000 requests
Completed 3500 requests
Completed 4000 requests
Completed 4500 requests
Completed 5000 requests
Finished 5000 requests


Server Software:        openresty/1.13.6.1    ##可以看到这里使用的openresty服务
Server Hostname:        172.25.61.1
Server Port:            80

Document Path:          /index.php
Document Length:        179 bytes

Concurrency Level:      10
Time taken for tests:   0.818 seconds
Complete requests:      5000
Failed requests:        0
Write errors:           0
Non-2xx responses:      5000
Total transferred:      1685000 bytes

##

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值