1. LNMP架构概述
1. 什么是LNMP
LNMP是一套技术的组合,L=LINUX,N=nginx,M~=Mysql,P~=PHP
2. LNMP架构是如何工作的
首先nginx服务是不能处理动态请求,那么当用户发起动态请求时,nginx又是如何进行处理的?
当用户发起http请求,请求会被nginx处理,如果是静态资源请求nginx则直接返回,如果是动态请求nginx则通过fastcgi协议转交给后端的PHP程序处理,具体如下:
3. nginx与fastcgi详细工作流程如下图所示:
- 用户通过http协议发起请求,请求会先抵达LNMP架构中的nginx
- nginx会根据用户的请求进行Location规则匹配
- location如果匹配到请求是静态,则由nginx读取本地直接返回
- location如果匹配到请求的动态,则由nginx将请求转发给fastcgi协议
- fastcgi收到后会将请求交给php-fpm管理进程,php-fpm管理进程接收到后会调用具体的工作进程warrap
- warrap进程会调用php程序进行解析,如果只是解析代码,php直接返回
- 如果有查询数据库操作,则由php连接数据库(用户、密码、ip)发起查询操作
- 最终数据有mysql->php->php-fpm->fastcgi->nginx->http->user
2. LNMP架构环境部署
2.1 使用官方仓库安装nginx
[root@study ~]# cat /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.
# 注意:默认情况下,默认情况下,储存库对于稳定的服务器软件包是优先使用 如果你想使用的主服务器的软件包, 运行以下命令:
yum-config-manager --enable nginx-mainline
# 安装nginx
[root@study ~]# yum install -y nginx
2.2 启动nginx,并加入开启自启
[root@study ~]# systemctl start nginx
[root@study ~]# systemctl enable nginx
2.3 使用第三方扩展源安装php7.1
# 首先移除系统中自带的旧版本
[root@study ~<