@LNMP的架构体系

LNMP

什么是LNMP?

LNMP是一套技术的组合,L=Linux、N=Nginx、M~=MySQL、P~=PHP、(ES、redis、kafka、zookeeper....

LNMP工作方式

首先Nginx服务是不能处理动态请求,那么当用户发起动态请求时, Nginx又是如何进行处理的。
	静态请求:请求静态文件或者html页面,服务器上存在的html文件
		静态文件:上传时什么样子,访问时还是什么样子;
	动态请求:请求的是动态内容,带参数的请求
			动态页面不存在于服务器上,他可能是取数据库或者redis等地方取值拼凑成的页面
	
当用户发起http请求,请求会被Nginx处理,如果是静态资源请求Nginx则直接返回,如果是动态请求Nginx则通过fastcgi协议转交给后端的PHP程序处理

1.访问流程

1.浏览器发起请求,请求到达nginx;
2.nginx先判断请求是动态还是静态;
	#静态请求
	location / {
		root /code;
		index index.html;
	}
	
	#动态请求
	location ~* \.php$ {
		fastcgi_pass 127.0.0.1:9000;
	}
3.如果是静态请求,nginx直接返回
4.如果是动态请求,nginx会通过fastcgi协议去找php-fpm管理进程;
5.php-fpm管理进程会去调用或者下发工作给wrapper工作进程;
6.wrapper工作进程判断php内容是否可以直接返回内容;
7.如果只是php内容,wrapper工作进程直接解析,并返回结果;
8.如果还需要访问数据库,则wrapper会去请求数据库获取数据,再返回。
9.最后数据由, 数据库mysql->wrapper->php-fpm->nginx->http->浏览器。

LNMP架构搭建

1.安装nginx

1)配置官方源

[root@web01 ~]# vim /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1

2)安装nginx

[root@web01 ~]# yum install -y nginx

3)配置nginx

[root@web01 ~]# vim /etc/nginx/nginx.conf
user www;

4)创建用户

[root@web01 ~]# groupadd www -g 666
[root@web01 ~]# useradd www -u 666 -g 666 -s /sbin/nologin -M

5)启动nginx,并加入开机自启

[root@web01 ~]# systemctl start nginx
[root@web01 ~]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.

6)验证启动

[root@web01 ~]# ps -ef | grep nginx
root      23769      1  0 11:26 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
www       23770  23769  0 11:26 ?        00:00:00 nginx: worker process
root      23816   8453  0 11:27 pts/1    00:00:00 grep --color=auto nginx

2.安装php

1)配置php第三方源

[root@web01 ~]# vim /etc/yum.repos.d/php.repo
[php-webtatic]
name = PHP Repository
baseurl = http://us-east.repo.webtatic.com/yum/el7/x86_64/
gpgcheck = 0

2)卸载已安装php

[root@web01 ~]# yum remove php-mysql-5.4 php php-fpm php-common

3)安装php 7.1版本

[root@web01 ~]# yum -y install php71w php71w-cli php71w-common php71w-devel php71w-embedded php71w-gd php71w-mcrypt php71w-mbstring php71w-pdo php71w-xml php71w-fpm php71w-mysqlnd php71w-opcache php71w-pecl-memcached php71w-pecl-redis php71w-pecl-mongodb
#yum安装会有报错
1.上传压缩包(yum缓存)
[root@web01 ~]# cd /tmp/
[root@web01 tmp]# rz php.tar.gz
2.解压代码包
[root@web01 tmp]# tar xf php.tar.gz
3.安装本地rpm包
[root@web01 tmp]# yum localinstall -y *.rpm

4)配置php

[root@web01 ~]# vim /etc/php-fpm.d/www.conf
user = www
group = www

5)启动php

[root@web01 ~]# systemctl start php-fpm
[root@web01 ~]# systemctl enable php-fpm
Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service.

6)验证启动

[root@web01 ~]# ps -ef | grep php
root      24062      1  0 11:45 ?        00:00:00 php-fpm: master process (/etc/php-fpm.conf)
www       24063  24062  0 11:45 ?        00:00:00 php-fpm: pool www
www       24064  24062  0 11:45 ?        00:00:00 php-fpm: pool www
www       24065  24062  0 11:45 ?        00:00:00 php-fpm: pool www
www       24066  24062  0 11:45 ?        00:00:00 php-fpm: pool www
www       24067  24062  0 11:45 ?        00:00:00 php-fpm: pool www
root      24089   8453  0 11:46 pts/1    00:00:00 grep --color=auto php
[root@web01 ~]#

3.安装mariadb

1)安装

[root@web01 ~]# yum install -y mariadb-server

2)启动

[root@web01 ~]# systemctl start mariadb
[root@web01 ~]# systemctl enable mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.

3)验证

[root@web01 ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.64-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

MariaDB [(none)]>

4.关联NP

1)配置nginx

[root@web01 conf.d]# vim php.conf
server {
    listen 80;
    server_name www.php.com;

    location / {
        root /code;
        index index.html;
    }
}

2)创建目录

[root@web01 ~]# mkdir /code
[root@web01 ~]# cd /code
[root@web01 ~]# chown -R www.www /code

3)上传作业代码

[root@web01 ~]# cd /code
[root@web01 code]# rz kaoshi.zip
[root@web01 code]# tar xf kaoshi.zip
[root@web01 code]# vim /code/upload_file.php
$wen="/code/upload";
#报错为413,因为上传文件过大,nginx默认上传文件大小为1m,超过1m就报413
							413 Request Entity Too Large
解决方法:
[root@web01 conf.d]# vim /etc/nginx/nginx.conf
http {
	... ...
	client_max_body_size 20m;
	... ...
}

#报错为405,因为nginx服务没有办法解析动态请求,没有跟php做关联

4)配置nginx关联php

[root@web01 conf.d]# vim php.conf 
server {
    listen 80;
    server_name www.php.com;

    location / {
        root /code;
        index index.html;
    }

    location ~* \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        #fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /code/$fastcgi_script_name;
        include fastcgi_params;
    }
}

5)nginx关联php语法

1> fastcgi_pass
#语法
Syntax:	fastcgi_pass address;
Default:	—
Context:	location, if in location

fastcgi_pass 127.0.0.1:9000;
2> fastcgi_index
#语法
Syntax:	fastcgi_index name;
Default:	—
Context:	http, server, location

fastcgi_index index.php
3>fastcgi_param
#语法
Syntax:	fastcgi_param parameter value [if_not_empty];
Default:	—
Context:	http, server, location

#语法模块	  开始定义(标准格式)站点目录  php文件名字
fastcgi_param SCRIPT_FILENAME /code/$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

6)php上传文件限制

[root@web01 ~]# vim /etc/php.ini
#默认post请求字符串内容不超过8m
post_max_size = 20M
#默认上传文件大小不超过2m
upload_max_filesize = 20M

[root@web01 ~]# systemctl restart php-fpm
posted @ 2021-04-01 19:49  ଲ一笑奈&何  阅读(29)  评论(0编辑  收藏  举报