LAMP架构5-Nginx缓存加速结合php


前言

PHP官网

1.php源码编译(开启php-fpm功能)

根据需求下载相应版本的源代码
php源代码官网下载

1.1 CGI、FastCGI和php-fpm的概念和区别

CGI:CGI(Common Gateway Interface),公共网关接口,它是Web服务器与外部应用程序(CGI程序)之间传递信息的接口标准。通过CGI接口,Web服务器就能够获取客户端提交的信息,并转交给服务器端的CGI程序处理,最后返回结果给客户端。也就是说,CGI实际上是一个接口标准。我们通常所说的CGI是指CGI程序,即实现了CGI接口标准的程序。 只要某种语言具有标准输入、输出和环境变量,如perl、php、C等,就可以用来编写CGI程序。CGI程序的工作方式:Web服务器一般只处理静态文件请求(如 jpg、htm、html),如果碰到一个动态脚本请求(如php),web服务器主进程,就fork出一个新的进程来启动CGI程序,也就是说将动态脚本请求交给CGI程序来处理。启动CGI程序需要一个过程,比如,读取配置文件,加载扩展等。CGI程序启动后,就会解析动态脚本,然后将结果返回给Web服务器,最后Web服务器再将结果返回给客户端,刚才fork的进程也会随之关闭。这样,每次用户请求动态脚本,Web服务器都要重新fork一个新进程,去启动CGI程序,由CGI程序来处理动态脚本,处理完后进程随之关闭。这种工作方式的效率是非常低下的。

FASTCGI:FastCGI也是和语言无关的。其主要行为是将CGI解释器进程保持在内存中并因此获得高效的性能。众所周知,CGI解释器的反复加载是CGI性能低下的主要原因。
FastCGI是一种进程管理工具,它可以在内存中管理CGI进程。
FastCGI进程管理器需要单独启动。启动FastCGI后,会生成一个FastCGI主进程和多个子进程(子进程其实就是CGI解释器进程)。
当客户端请求Web服务器上的动态脚本时,Web服务器会将动态脚本通过TCP协议交给FastCGI主进程,FastCGI主进程根据情况,安排一个空闲的子进程来解析动态脚本,处理完成后将结果返回给Web服务器,Web服务器再将结果返回给客户端。该客户端请求处理完毕后,FastCGI子进程并不会随之关闭,而是继续等待主进程安排工作任务。
由此可知,FastCGI的工作效率是非常高的

php-fpm: fpm是FastCGI Process Manager的缩写,那么,fpm就是FastCGI进程管理器的简称
php-fpm就是php中的FastCGI进程管理器

对于php5.3之前的版本来说,php-fpm是一个第三方的补丁包,旨在将FastCGI进程管理整合进PHP包中。
php5.3之后的版本中,php-fpm不再是第三方的包,它已经被集成到php的源码中了。php-fpm提供了更好的PHP进程管理方式,可以有效控制内存和进程、可以平滑重载PHP配置,比spawn-fcgi具有更多优点,所以php-fpm被PHP官方收购了。

1.2 源码编译php

我使用的是7.4.12版

- 软件编译:
 1. tar jxf php-7.4.12.tar.bz2
 2. cd php-7.4.12
 3. ./configure --prefix=/usr/local/lnmp/php --with-config-file-path=/usr/local/lnmp/php/etc --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx  --with-curl --with-iconv --with-mhash --with-zlib --with-openssl --enable-mysqlnd --with-mysqli --with-pdo-mysql --disable-debug --enable-sockets --enable-soap --enable-inline-optimization --enable-xml --enable-ftp --enable-gd --enable-exif --enable-mbstring  --enable-bcmath --with-fpm-systemd
 4. 对应configure的解释
	软件安装路径,配置文件路径,允许fpm服务,fpm服务的所有者和组是nginx,一些功能模块,不允许debug,一些功能模块,最后是允许使用systemctl控制
 5. make 
 6. make install

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

yum install -y systemd-devel
在这里插入图片描述
yum install libxml2-devel -y
在这里插入图片描述
yum install -y sqlite-devel
在这里插入图片描述
yum install curl-devel -y
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
make编译下
在这里插入图片描述
make install 安装完成
在这里插入图片描述

1.3 拷贝php-fpm配置文件

[root@server1 etc]# pwd
/usr/local/lnmp/php/etc
[root@server1 etc]# cp php-fpm.conf.default php-fpm.conf
[root@server1 etc]# vim php-fpm.conf

[root@server1 php-fpm.d]# pwd
/usr/local/lnmp/php/etc/php-fpm.d
[root@server1 php-fpm.d]# cp www.conf.default www.conf

[root@server1 php-fpm.d]# cd ~/php-7.4.12/
[root@server1 php-7.4.12]# cp php.ini-production  /usr/local/lnmp/php/etc/php.ini

[root@server1 fpm]# pwd
/root/php-7.4.12/sapi/fpm
[root@server1 fpm]# cp php-fpm.service /etc/systemd/system
[root@server1 fpm]# systemctl daemon-reload 

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

1.4 修改php-fpm启动文件并启动php-fpm

# vim /etc/systemd/system/php-fpm.service
ProtectSystem=false	//默认php-fpm启动时只读挂载/usr目录
# systemctl daemon-reload
# systemctl start php-fpm

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
可以看到监听本机9000端口
在这里插入图片描述

2、nginx结合php-fpm

2.1 修改php时区

[root@server1 etc]# pwd
/usr/local/lnmp/php/etc
[root@server1 etc]# vim php.ini
[root@server1 etc]# systemctl reload php-fpm.service  
[root@server1 etc]# systemctl restart php-fpm.service 

在这里插入图片描述

2.2 修改nginx配置文件

vim  /usr/local/nginx/conf/nginx.conf
修改nginx配置文件:
	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;
	        }


在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

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

在这里插入图片描述
成功访问php

在这里插入图片描述

3、 php添加memcache功能模块(缓存)

3.1 memcache缓存简单介绍

memcache是一个高性能的分布式的内存对象缓存系统,用于动态Web应用以减轻数据库负担。它通过在内存中缓存数据和对象,来减少读取数据库的次数。从而提高动态、数据库驱动网站速度。

memcache通过在内存里维护一个统一的巨大的hash表,它能够用来存储各种格式的数据,包括图像、视频、文件以及数据库检索的结果等。memcache主要用于分担数据库负的压力,memcache将数据调用到内存中,然后从内存中读取,从而大大提高读取速度。

传统缓存机制信号流
在这里插入图片描述

使用memc-nginx和srcache-nginx模块构建高效透明的缓存机制信号流(重点部署这个高效版)
在这里插入图片描述

3.2 部署传统缓存机制

在这里插入图片描述

3.2.1 配置下环境变量

[root@server1 html]# cd  /usr/local/lnmp/php/bin
[root@server1 bin]# ls
phar  phar.phar  php  php-cgi  php-config  phpdbg  phpize

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

在这里插入图片描述

在这里插入图片描述

3.2.2 下载memcache源码安装包,进行编译安装

下载链接

[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]# yum install -y 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
Installing shared extensions:     /usr/local/lnmp/php/lib/php/extensions/no-debug-non-zts-20190902/

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

在这里插入图片描述

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

3.3.3 配置php加载模块

[root@server1 memcache-4.0.5.2]# cd /usr/local/lnmp/php/lib/php/extensions//no-debug-non-zts-20190902/
[root@server1 no-debug-non-zts-20190902]# ls
memcache.so  opcache.a  opcache.so
[root@server1 no-debug-non-zts-20190902]# cd /usr/local/lnmp/php/etc/
[root@server1 etc]# ls
php-fpm.conf  php-fpm.conf.default  php-fpm.d  php.ini
[root@server1 etc]# vim php.ini 
[root@server1 etc]# systemctl reload php-fpm.service 
[root@server1 etc]# php -m | grep memcache

[root@server1 etc]# yum install -y memcached.x86_64 
[root@server1 etc]# systemctl enable --now memcached.service
[root@server1 etc]# netstat -antlp | grep memcache

在这里插入图片描述

在这里插入图片描述

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

3.3.4 测试memcached

Telnet工具基本介绍

  • Telnet协议是TCP/IP协议族中的一员,是Internet远程登录服务的标准协议和主要方式。它为用户提供了在本地计算机上完成远程主机工作的能力。
  • 在终端使用者的电脑上使用telnet程序,用它连接到服务器。终端使用者可以在telnet程序中输入命令,这些命令会在服务器上运行,就像直接在服务器的控制台上输入一样。可以在本地就能控制服务器。
  • 要开始一个telnet会话,必须输入用户名和密码来登录服务器。
  • Telnet是常用的远程控制Web服务器的方法

安装Telnet工具

yum install -y telnet

在这里插入图片描述
telnet用法

telnet [-8acdEfFKLrx][-b<主机别名>][-e<脱离字符>][-k<域名>][-l<用户名称>][-n<记录文件>][-S<服务类型>][-X<认证形态>][主机名称或IP地址<通信端口>]

-8 允许使用8位字符资料,包括输入与输出。
-a 尝试自动登入远端系统。
-b<主机别名> 使用别名指定远端主机名称。
-c 不读取用户专属目录里的.telnetrc文件。
-d 启动排错模式。
-e<脱离字符> 设置脱离字符。
-E 滤除脱离字符。
-f 此参数的效果和指定"-F"参数相同。
-F 使用Kerberos V5认证时,加上此参数可把本地主机的认证数据上传到远端主机。
-k<域名> 使用Kerberos认证时,加上此参数让远端主机采用指定的领域名,而非该主机的域名。
-K 不自动登入远端主机。
-l<用户名称> 指定要登入远端主机的用户名称。
-L 允许输出8位字符资料。
-n<记录文件> 指定文件记录相关信息。
-r 使用类似rlogin指令的用户界面。
-S<服务类型> 设置telnet连线所需的IP TOS信息。
-x 假设主机有支持数据加密的功能,就使用它。
-X<认证形态> 关闭指定的认证形态。

在这里插入图片描述

测试高速缓存memcache+php

[root@server1 etc]# cd /root/memcache-4.0.5.2/   #进到memcached的目录
复制文件到nginx发布目录
[root@server1 memcache-4.0.5.2]# cp example.php memcache.php /usr/local/nginx/html/

编辑nginx发布目录里的发布文件,其中example.php不修改,只修改memcache.php
[root@server1 memcache-4.0.5.2]# cd /usr/local/nginx/html/
[root@server1 html]# vim memcache.php 

在这里插入图片描述

在这里插入图片描述
测试(要保证nginx开启,php-fpm服务开启)
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

3.3.5 传统memcache机制与没有缓存机制对比测试

是否使用memcache高速缓存会快一点,我们来压测一下
先压测通过memcache缓存访问php文件,可以看到速度很快,很流畅,并且没有访问失败。

在这里插入图片描述
在这里插入图片描述
接下来测试不通过memcache,直接访问本地php文件
在这里插入图片描述

在这里插入图片描述

3.3 构建nginx高速缓存(优化传统memcache)

在这里插入图片描述

3.3.1 openresy的安装

rhel安装步骤参考
在这里插入图片描述

# add the yum repo:
 wget https://openresty.org/package/rhel/openresty.repo
 mv openresty.repo /etc/yum.repos.d/

# update the yum index:
 yum check-update
 yum install -y openresty

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

3.3.2 软件配置

 # cd /usr/local/openresty/nginx/conf
  # vim nginx.conf
	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。

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

在这里插入图片描述
在这里插入图片描述
访问成功
在这里插入图片描述

3.3.3 配置高速缓存

[root@server1 html]# pwd
/usr/local/openresty/nginx/html
[root@server1 html]# cd  ../conf/
[root@server1 conf]# vim nginx.conf
[root@server1 conf]# systemctl reload openresty.service 

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

3.3.4 配置完进行压力测试

在这里插入图片描述
不使用openresty缓存速度会慢,前面已经测试过了

4、nginx结合lua

[root@server1 html]# pwd
/usr/local/openresty/nginx/html
[root@server1 html]# cd  ../conf/
[root@server1 conf]# vim nginx.conf
[root@server1 conf]# systemctl reload openresty.service 
  location @client {
	proxy_pass  http://172.25.200.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.200.250" then
	ngx.exec("@client")
	end
	';
	}

在这里插入图片描述
测试(保证172.25.200.3 apache服务法开)
在这里插入图片描述
在这里插入图片描述

172.25.200.250访问
在这里插入图片描述

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值