nginx的location配置和LNMP架构

一、NGINX的location配置

使用Nginx Location可以控制访问网站的路径,但一个server可以有多个location配置, 多个location的优先级该如何区分

1.1、语法

Syntax: location [ = | ~ | ~* | ^~ ] uri { ... }
        location @name { ... }
Default:    —
Context:    server, location

1.2、location匹配符

匹配符 匹配规则 优先级
= 精确匹配 1
^~ 以某个字符串开头 2
~ 区分大小写的正则匹配 3
~* 不区分大小写的正则匹配 3
/ 通用匹配,任何请求都会匹配到 4

1.3、优先级验证

[root@web01 ~]# vim /etc/nginx/conf.d/youxianji.conf
server {
    listen 80;

    location / {
        default_type text/html;
        return 200 "location /";
    }
 
    location =/ {
        default_type text/html;
        return 200 "location =/";
    }
 
    location ~ / {
        default_type text/html;
        return 200 "location ~/";
    }
 
    location ^~ / {
      default_type text/html;
      return 200 "location ^~";
    }
}

1.4、location应用场景

# 通用匹配,任何请求都会匹配到
location / {
    ...
}
 
# 严格区分大小写,匹配以.php结尾的都走这个location    
location ~ \.php$ {
    ...
}
 
# 严格区分大小写,匹配以.jsp结尾的都走这个location 
location ~ \.jsp$ {
    ...
}
 
# 不区分大小写匹配,只要用户访问.jpg,gif,png,js,css 都走这条location
location ~* .*\.(jpg|gif|png|js|css)$ {
    ...
}

二、LNMP架构

2.1、简介

LNMP是一套技术的组合,L=Linux、N=Nginx、M~=MySQL、P~=PHP
不仅仅只有这些服务,还有很多
redis\elasticsearch\kibana\logstash\zabbix\git\jenkins\kafka\hbase\hadoop\spark\flink

2.2、LNMP架构工作方式

首先Nginx服务是不能处理动态请求,那么当用户发起动态请求时, Nginx又是如何进行处理的。
    1.静态请求:请求的内容是静态文件就是静态请求
        1)静态文件:文件上传到服务器,永远不会改变的文件就是静态文件
        2)html就是一个标准的静态文件
    2.动态请求:请求的内容是动态的就是动态请求
        1)不是真实存在服务器上的内容,是通过数据库或者其他服务拼凑成的数据
 
当用户发起http请求,请求会被Nginx处理,如果是静态资源请求Nginx则直接返回,如果是动态请求Nginx则通过fastcgi协议转交给后端的PHP程序处理,具体如下图所示

2.3、访问流程

1.浏览器输入域名,浏览器会拿着域名取DNS服务器解析
2.DNS服务器会将域名解析成IP
3.浏览器会去与IP对应服务器建立TCP\IP连接
4.连接建立完成,会向服务器发起请求,请求nginx
5.nginx会判断请求是动态的还是静态的
    #静态请求
    location \.jpg$ {
        root /code;
    }
    #动态请求
    location \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        ... ...
    }
6.如果是静态请求,nginx去code目录获取,直接返回
7.如果是动态请求,nginx会通过fastcgi协议连接PHP服务的php-fpm管理进程
8.php-fpm管理进程会下发工作给 wrapper工作进程
9.wrapper工作进程判断是不是简单的php内容
10.如果只是php内容则使用php解析器解析后直接返回
11.如果还需要读取数据库,wrapper工作进程会去数据库读取数据,再返回数据
12.数据流转过程:
    1)请求:浏览器 > 负载均衡 > nginx > php-fpm > wrapper > mysql
    2)响应:mysql > wrapper > php-fpm > nginx > 负载均衡 > 浏览器

三、LNMP架构搭建

3.1、搭建NGINX

安装NGINX
[root@web01 ~]# yum install -y nginx
配置NGINX
[root@web01 ~]# vim /etc/nginx/nginx.conf 
user  www;
创建用户
[root@web01 ~]# groupadd www -g 666
[root@web01 ~]# useradd www -u 666 -g 666 -s /sbin/nologin -M
启动NGINX
[root@web01 ~]# systemctl start nginx

3.2、搭建PHP

1、安装yum源
[root@web03 yum.repos.d]# rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
[root@web03 yum.repos.d]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

# 清除之前部署的PHP
[root@web03 yum.repos.d]# yum remove php-mysql-5.4 php php-fpm php-common

2、安装PHP
[root@web03 yum.repos.d]# yum -y install php72w php72w-cli php72w-common php72w-devel php72w-embedded php72w-gd php72w-mcrypt php72w-mbstring php72w-pdo php72w-xml php72w-fpm php72w-mysqlnd php72w-opcache php72w-pecl-memcached php72w-pecl-redis php72w-pecl-mongodb

3、验证PHP
[root@web03 yum.repos.d]# php -v
PHP 7.2.34 (cli) (built: Oct  1 2020 13:37:37) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.2.34, Copyright (c) 1999-2018, by Zend Technologies
    
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.

3.3、NGINX代理PHP

1、准备配置文件
[root@web01 ~]# vim /etc/nginx/conf.d/1.conf 
server {
        listen 80;  
        location / {
            root /mnt/php;
            index index.html index.php;
        }
        location ~* \.php$ {
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_param SCRIPT_FILENAME /mnt/php/$fastcgi_script_name;
            include fastcgi_params;
        }
}

2、编写php文件
[root@web01 ~]# vim /mnt/php/index.php 
<?php
phpinfo();

3、重启PHP与NGINX
[root@web01 ~]# systemctl restart php-fpm nginx

4、测试

3.4、搭建考试页面

3.4.1、上传代码并给目录授权

[root@web01 ~]# cd /mnt
[root@web01 mnt]# mkdir kaoshi
[root@web01 mnt]# cd kaoshi
[root@web01 kaoshi]# rz kaoshi.zip
[root@web01 kaoshi]# unzip kaoshi.zip
[root@web01 kaoshi]# mv kaoshi/* ./
[root@web01 ~]# chown -R www.www /mvt/kaoshi

3.4.2、配置NGINX

[root@web01 ~]# vim /etc/nginx/conf.d/1.conf
server {
        listen 80;

        location / {
                root /mnt/kaoshi;
                index index.html index.php;
        }
        location ~* \.php {
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_param SCRIPT_FILENAME /mnt/kaoshi/$fastcgi_script_name;
                include fastcgi_params;
        }
}

3.4.3、解决上传大小问题

1、修改nginx配置
    [root@web01 ~]# vim /etc/nginx/nginx.conf
    client_max_body_size 200M;
2、修改php配置文件
    [root@web01 ~]# vim /etc/php.ini
    upload_max_filesize = 200M
    post_max_size = 200M

3.4.4、配置PHP文件

[root@web01 ~]# vim /mnt/kaoshi/upload_file.php
$wen="/mnt/kaoshi/upload";  # 这一行更改

3.4.5、重启PHP与NGINX

[root@web01 ~]# systemctl restart php-fpm nginx

3.4.6、测试

3.4.7、一些问题的解决办法

如果上传文件时报错,且是500,那么可以去查看日志
[root@web01 ~]# cat /var/log/nginx/error.log
如果有
2021/08/13 11:46:00 [crit] 1741#1741: *2 open() "/var/lib/nginx/tmp/client_body/0000000002" failed (13: Permission denied), client: 192.168.177.1, server: , request: "POST /upload_file.php HTTP/1.1", host: "192.168.177.7", referrer: "http://192.168.177.7/"
这样的错误日志,我们可以
[root@web01 ~]# chown -R www.www /var/lib/nginx
来解决

四、搭建mariadb

4.1、安装mariadb

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

4.2、启动mariadb服务

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

4.3、连接验证

[root@db01 ~]# 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)]> exit
Bye

4.4、设置数据库密码

[root@db01 ~]# mysqladmin -uroot password '123456'

# 使用密码连接数据库
[root@db01 ~]# mysql -uroot -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 5
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)]> exit
Bye

五、使用PHP连接Mariadb

5.1、编写PHP文件

来到web01机器
[root@web01 ~]# vim /mnt/kaoshi/mysql.php
<?php
        $servername = "172.16.1.51";  # 数据库服务器IP
        $username = "root";  # 数据库用户名
        $password = "123456";  # 数据库密码

        // 创建连接
        $conn = mysqli_connect($servername, $username, $password);

        // 检测连接
        if (!$conn) {
            die("Connection failed: " . mysqli_connect_error());
        }
        echo "小哥哥,php可以连接MySQL...";
    ?>

5.2、修改数据库远程连接权限

虽然web01服务器配置好了,但此时我们连接依然连接不上

是因为需要我们修改数据库远程连接权限(在Mysql命令行中执行)
MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

此时我们在用PHP连接数据库就可以了

5.3、搭建wordpress

# wordpress官网:https://cn.wordpress.org/download/
# 下载链接:https://cn.wordpress.org/latest-zh_CN.tar.gz

1、下载代码包
[root@web01 mnt]# wget https://cn.wordpress.org/latest-zh_CN.tar.gz
--2021-08-13 17:09:37--  https://cn.wordpress.org/latest-zh_CN.tar.gz
Resolving cn.wordpress.org (cn.wordpress.org)... 198.143.164.252
Connecting to cn.wordpress.org (cn.wordpress.org)|198.143.164.252|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 15784827 (15M) [application/octet-stream]
Saving to: ‘latest-zh_CN.tar.gz’

100%[==========================================>] 15,784,827  6.23MB/s   in 2.4s   

2021-08-13 17:09:40 (6.23 MB/s) - ‘latest-zh_CN.tar.gz’ saved [15784827/15784827]

2、解压
[root@web01 mnt]# tar -xf latest-zh_CN.tar.gz
[root@web01 mnt]# ll
total 15420
drwxr-xr-x. 2 www  www         6 Jun 29 16:55 hgfs
drwxr-xr-x  4 www  www       142 Aug 13 16:55 kaoshi
-rw-r--r--  1 root root 15784827 Aug 13 06:00 latest-zh_CN.tar.gz
drwxr-xr-x  2 root root       23 Aug 13 10:51 php
drwxr-xr-x  5 1006 1006     4096 Aug 13 06:00 wordpress  # WordPress目录

3、授权
[root@web01 mnt]# chown -R www.www wordpress/
[root@web01 mnt]# ll
total 15420
drwxr-xr-x. 2 www  www         6 Jun 29 16:55 hgfs
drwxr-xr-x  4 www  www       142 Aug 13 16:55 kaoshi
-rw-r--r--  1 root root 15784827 Aug 13 06:00 latest-zh_CN.tar.gz
drwxr-xr-x  2 root root       23 Aug 13 10:51 php
drwxr-xr-x  5 www  www      4096 Aug 13 06:00 wordpress

4、编写配置文件
[root@web01 mnt]# vim /etc/nginx/conf.d/wordpress.conf
server {
    listen 80;
    server_name www.test.com;
    location / {
        root /mnt/wordpress/;
		index index.html index.php;
    }
    location ~* \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME /mnt/wordpress/$fastcgi_script_name;
        include fastcgi_params;
    }
}

5、配置Windows的hosts文件
192.168.177.7 www.test.com

6、重启NGINX
[root@web01 mnt]# systemctl restart nginx

7、由于需要数据库名,我们去数据库设置一下
MariaDB [(none)]> create database wordpress;

8、连接

#点击现在开始

#点击提交

#点击运行安装程序

#填写完后点击安装WordPress

#登陆

#这样就完成了
posted @ 2021-08-13 18:47  小丶凡  阅读(69)  评论(0编辑  收藏  举报
1