LAMP服务架构之传统缓存机制(Ngins+PHP+Memcache)

在这里插入图片描述nginx - fastcgi - php - memcache 协同下的 请求的完整访问过程

  1. 用户发送http请求报文给nginx服务器
  2. nginx会根据文件url和后缀来判断请求
  3. 如果请求的是静态内容,nginx会将结果直接返回给用户; 如果请求的是动态内容,nginx会将请求交给 fastcgi客户端 ,通过 fastcgi_pass 将这个请求发送给 php-fpm
  4. php-fpm 会将请求交给 wrapper
  5. wrapper 收到请求会生成新的线程调用 php动态程序解析服务器
  6. 如果用户请求的是博文、或者内容、PHP会请求 MySQL查询结果; 如果用户请求的是图片、附件、PHP会请求 nfs存储查询结果
  7. php会将查询到的结果交给Nginx
  8. nginx会生成一个响应报文返还给用户

fastcgi的主要优点:

  • 把动态语言和http服务器分离开来,使nginx可以处理静态请求和向后转发动态请求,而php/php-fpm服务器转移解析PHP动态请求

使用fastcgi的原因:

  • Nginx 不支持对外部动态程序的直接调用或者解析 ,所有的外部程序(包括PHP)必须通过FastCGI接口来调用。

FastCGI的重要特点总结:

  • 是HTTP服务器和动态脚本语言间通信的接口或者工具
  • 优点就是把动态语言解析和HTTP服务器分离了开来
  • Nginx、Apache、lighttpd以及多数动态语言都支持FastCGI。
  • 接口方式采用C/S结构,分为HTTP服务器(客户端)和动态语言解析服务器(服务端)
  • PHP动态语言服务端可以启动多个FastCGI的守护进程例如:php-fpm(fastcgi process mangemnt)
  • http服务器通过例(Nginx fastgi_pass)FastCGI客户端和动态语言FastCGI服务端通信(例如:php-fpm)

Memcache —— 是一个高性能的分布式的内存对象缓存系统,通过在内存里维护一个统一的巨大的hash表,它能够用来存储各种格式的数据,包括图像、视频、文件以及数据库检索的结果等。简单的说就是将数据调用到内存中,然后从内存中读取,从而大大提高读取速度。

步骤一 , 安装

nginx 的源码安装(略)

php 的源码安装

yum install -y bzip2                        # 安装 压缩与解压缩 工具

yum install -y systemd-devel libxml2-devel sqlite-devel  libcurl-devel libpng-devel         # 解决安装依赖性
yum install -y oniguruma-6.8.2-1.el7.x86_64.rpm  oniguruma-devel-6.8.2-1.el7.x86_64.rpm     # 解决安装依赖性
tar jxf php-7.4.6.tar.bz2                   # 解压缩php
cd php-7.4.6
./configure --prefix=/usr/local/php --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx  --with-curl --with-iconv  # 编译 添加参数
make -j2 && make install

PHP 配置

cp php.ini-development /usr/local/php/php.ini         # 重命名
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm        # 创建 php 服务脚本(php-fpm)
chmod +x /etc/init.d/php-fpm                          # 为php启动脚本添加可执行权限
cd /usr/local/php/etc
cp php-fpm.conf.default php-fpm.conf                  # 用模板生成 php 配置文件
cp php-fpm.d/www.conf.default php-fpm.d/www.conf
ln -s /usr/local/php/bin/* /usr/local/bin/            # 设置软连接
ln -s /usr/local/php/sbin/* /usr/local/sbin/          # 设置软连接

cd /etc/init.d/
./php-fpm start                                       # 启动php服务

memcache 的安装

步骤二,Nginx 与 PHP 的结合配置

Nginx配置:

  • vim /usr/local/nginx/conf/nginx.conf
在server字段下:
location ~ \.php$ {
            root           html;            
            fastcgi_pass   127.0.0.1:9000;            
            fastcgi_index  index.php;            
            #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;            include        
            fastcgi.conf;        
            }
  • vim /usr/local/nginx/html/index.php
<?php
phpinfo()
?>
  • nginx -s reload

PHP 配置:

  • vim /usr/local/php/etc/php-fpm.conf
pid=run/php-fpm.pid
  • cd /etc/init.d/
    ./php-fpm reload # 重新加载PHP服务配置文件

测试:

  • http://172.25.1.3/index.php

步骤三,PHP 与 Memcache 的结合配置

  • vim .bash_profile
PATH=$PATH:$HOME/bin:/usr/local/php/bin
#php增加memcache模块将php的bin目录路径添加到~./bash_profile中,为了方便调用
  • source .bash_profile
yum install autoconf -y
cd memcache-4.0.5.2/
/usr/local/php/binphpize
./configure --with-php-config=/usr/local/php/bin/php-config
make
make install

php中增加缓存模块,并重新加载:

  • vim /usr/local/php/lib/php.ini
extension=memcache.so
  • systemctl reload php-fpm

php -m|grep memcache
yum install -y memcached
systemctl start memcached.service
systemctl status memcached.service
cd /memcache-4.0.5.2/
cp example.php memcache.php /usr/local/nginx/html/vim memcache.php

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值