LNMP架构逻辑,搭建lnmp关联NP,搭建作业页面

发布时间 2023-08-15 00:07:54作者: 战斗小人

LNMP

什么是LNMP?

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

 

LNMP工作方式

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

 

1.访问流程

1.浏览器发起请求,请求到达nginx
2.nginx先判断请求是动态还是静态
    # 静态访问
    location / {
        root /code;
        index index.html;
    }
    
    # 动态请求
    location ~* \.php$ {
        fastcgi_pass 127.0.0.1:9000;    # 关联php服务
    }
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-stable]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=1
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 ~]# usseradd 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.

5)验证启动

[root@web01 ~]# ps -ef |grep nginx
root       7538      1  0 23:27 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
www        7539   7538  0 23:27 ?        00:00:00 nginx: worker process
root       7562   7431  0 23:29 pts/0    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 ~]# rz php.tar.gz
2.解压代码包
[root@web01 ~]# tar xf php.tar.gz
3.安装本地rpm包
[root@web01 ~]# 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 ~]# php-fpm -t
[14-Aug-2023 20:52:19] NOTICE: configuration file /etc/php-fpm.conf test is successful
# 加入开机自启
[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       8865      1  0 20:51 ?        00:00:00 php-fpm: master process (/etc/php-fpm.conf)
www        8866   8865  0 20:51 ?        00:00:00 php-fpm: pool www
www        8867   8865  0 20:51 ?        00:00:00 php-fpm: pool www
www        8868   8865  0 20:51 ?        00:00:00 php-fpm: pool www
www        8869   8865  0 20:51 ?        00:00:00 php-fpm: pool www
www        8870   8865  0 20:51 ?        00:00:00 php-fpm: pool www
root       8902   7555  0 20:56 pts/1    00:00:00 grep --color=auto php

 

3.安装mariadb

1) 安装

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

2)启动

[root@web01 ~]# systemctl start mariadb
# 开机自启
[root@web01 ~]# systemctl enable mariadb

3) 验证

[root@web01 ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.68-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)

 

4.关联NP

1)配置nginx

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

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

2) 创建目录

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

3) 上传作业代码

[root@web01 conf.d]# cd /code
[root@web01 conf.d]# rz kaoshi.zip
[root@web01 conf.d]# tar xf kaoshi.zip
[root@web01 conf.d]# 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;
    ... ...
}
# 注: php里面也有限制上传文件大小的限制,upload_max_filesize = 2M

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

4)配置nginx关联php

cd /etc/nginx/conf.d
[root@web01 conf.d]# vim php.conf --------------------------------------- server { listen 80; server_name localhost; location / { root /code; index index.html; } location ~* \.php$ { # 以.php结尾的文件 # root /code # 站点目录,如果下方使用$document_root变量,这里就要设置 fastcgi_pass 127.0.0.1:9000; # 让php服务去解析 fastcgi_param SCRIPT_FILENAME /code/$fastcgi_script_name;#SCRIPT_FILENAME固定格式,说明要开始定义变量了 $fastcgi_script_name表示访问的php名字,这里就是file.php (这里需要告诉访问php的位置) # fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; #可以用$document_root获取当前的站点目录/code include fastcgi_params; # 变量($fastcgi_script_name)存放的地址,不写路径默认在nginx的安装路径/etc/nginx下找 } }

 

5) nginx关联php语法

1>fastcgi_pass

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

fastcgi_pass 127.0.0.1:9000;

2>fast_index

# 语法
Syntax:    fastcgi_index name;
Default:    —
Context:    http, server, location

# 例如:
server {
    listen 80;
    server_name localhost;

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

    location ~* \.php$ {    # 以.php结尾的文件
        fastcgi_pass 127.0.0.1:9000;
        # 默认页面是index.php,但是这里如果不写会访问上面/指定的路径。一般不写这句话,没什么用
        fastcgi_index index.php;#如果访问的地址是空,那就访问站点下的index.php(但是上面配置/默认访问index.index,所以这句话没什么用)
        fastcgi_param SCRIPT_FILENAME /code/$fastcgi_script_name;
        include fastcgi_params;    
    }
}

3>fastcgi_param

Syntax:    fastcgi_param parameter value [if_not_empty];
Default:    —
Context:    http, server, location

# 语法模块       开始定义(标准格式)  站点目录          php文件名字
fastcgi_param SCRIPT_FILENAME /home/www/scripts/php$fastcgi_script_name;#fastcgi_param SCRIPT_FILENAME 基本格式,后面就写文件地址
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name