• nginx做负载均衡服务器,配置动静分离


    nginx做负载均衡服务器,配置动静分离

    题目

    后端RS服务器⼀台部署LNMP(nginx1.22+mysql8.0+php8.1),⼀台部署httpd。
    要求nginx和php使⽤编译安装
    最后要通过访问nginx负载均衡服务器的IP看到动静分离的效
    果。
    
    • 1
    • 2
    • 3
    • 4

    环境说明

    主机名IP地址安装服务
    node1192.168.205.144lnmp,动态资源
    node2192.168.205.147nginx,静态资源
    node3192.168.205.150httpd,做负载均衡

    关闭所有主机的防火墙

    [root@node1 ~]# systemctl stop firewalld
    [root@node1 ~]# systemctl disable firewalld
    Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
    Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
    [root@node1 ~]# setenforce 0
    [root@node1 ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
    
    [root@node2 ~]# systemctl stop firewalld
    [root@node2 ~]# systemctl disable firewalld
    Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
    Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
    [root@node2 ~]# setenforce 0
    [root@node2 ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
    
    [root@node3 ~]# systemctl stop firewalld
    [root@node3 ~]# systemctl disable firewalld
    Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
    Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
    [root@node3 ~]# setenforce 0
    [root@node3 ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    在node1主机上部署lnmp,在动态资源

    源码安装nginx

    //上传软件包
    [root@node1 ~]# ls
    anaconda-ks.cfg  nginx-1.22.0.tar.gz
    
    //创建系统用户nginx
    [root@node1 ~]# useradd -rMs /sbin/nologin nginx
    
    //安装依赖环境
    [root@node1 ~]# dnf -y groups mark install 'Development Tools'
    [root@node1 ~]# dnf -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make
    
    //创建日志存放目录
    [root@node1 ~]# mkdir -p /var/log/nginx
    [root@node1 ~]# chown -R nginx.nginx /var/log/nginx/
    [root@node1 ~]# ll -d /var/log/nginx/
    drwxr-xr-x. 2 nginx nginx 6 Oct 19 14:57 /var/log/nginx/
    
    //编译安装
    [root@node1 ~]# tar xf nginx-1.22.0.tar.gz 
    [root@node1 ~]# cd nginx-1.22.0
    [root@node1 nginx-1.22.0]# ./configure \
    >     --prefix=/usr/local/nginx \
    >     --user=nginx \
    >     --group=nginx \
    >     --with-debug \
    >     --with-http_ssl_module \
    >     --with-http_realip_module \
    >     --with-http_image_filter_module \
    >     --with-http_gunzip_module \
    >     --with-http_gzip_static_module \
    >     --with-http_stub_status_module \
    >     --http-log-path=/var/log/nginx/access.log \
    >     --error-log-path=/var/log/nginx/error.log
    
    [root@node1 nginx-1.22.0]# make
    [root@node1 nginx-1.22.0]# make install
    
    //配置环境变量
    [root@node1 ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
    [root@node1 ~]# source /etc/profile.d/nginx.sh
    
    //启动nginx
    [root@node1 ~]# nginx
    [root@node1 ~]# ss -antl
    State   Recv-Q  Send-Q   Local Address:Port   Peer Address:Port  Process  
    LISTEN  0       128            0.0.0.0:80          0.0.0.0:*              
    LISTEN  0       128            0.0.0.0:22          0.0.0.0:*              
    LISTEN  0       128               [::]:22             [::]:*        
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

    访问

    在这里插入图片描述

    yum安装MySQL

    //安装yum源命令
    [root@node1 ~]# ls
    anaconda-ks.cfg                             nginx-1.22.0
    mysql80-community-release-el7-5.noarch.rpm  nginx-1.22.0.tar.gz
    [root@node1 ~]# rpm -Uvh mysql80-community-release-el7-5.noarch.rpm 
    
    //检查是否已经安装好yum源
    [root@node1 ~]# yum repolist enabled | grep "mysql.*-community.*"
    Failed to set locale, defaulting to C.UTF-8
    mysql-connectors-community              MySQL Connectors Community
    mysql-tools-community                   MySQL Tools Community
    mysql80-community                       MySQL 8.0 Community Server
    [root@node1 ~]# yum module disable mysql
    
    //安装MySQL
    [root@node1 ~]# yum -y install mysql-community-server
    
    //启动MySQL
    [root@node1 ~]# systemctl start mysqld
    [root@node1 ~]# systemctl enable mysqld
    Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service.
    [root@node1 ~]# ss -antl
    State   Recv-Q  Send-Q   Local Address:Port    Peer Address:Port Process  
    LISTEN  0       128            0.0.0.0:80           0.0.0.0:*             
    LISTEN  0       128            0.0.0.0:22           0.0.0.0:*             
    LISTEN  0       70                   *:33060              *:*             
    LISTEN  0       128                  *:3306               *:*             
    LISTEN  0       128               [::]:22              [::]:*     
    
    //查看初始密码
    [root@node1 ~]# grep 'temporary password' /var/log/mysqld.log
    2022-10-19T08:24:34.348046Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: .Cr5kaHvn>*3
    
    //登录数据库修改密码
    [root@node1 ~]# mysql -uroot -p
    Enter password: 
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 8
    Server version: 8.0.31
    
    Copyright (c) 2000, 2022, Oracle and/or its affiliates.
    
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Jiang123!';
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> exit
    Bye
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53

    源码安装PHP

    //上传并解压
    [root@node1 ~]# ls
    anaconda-ks.cfg                             nginx-1.22.0.tar.gz
    mysql80-community-release-el7-5.noarch.rpm  php-8.1.11.tar.gz
    nginx-1.22.0
    [root@node1 ~]# tar xf php-8.1.11.tar.gz 
    
    //配置网络源
    [root@node1 ~]# dnf -y install epel-release
    
    //安装依赖包
    [root@node1 ~]# dnf -y install sqlite-devel make wget libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl curl-devel libcurl-devel libicu-devel libjpeg libjpeg-devel libpng libpng-devel openldap-devel pcre-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel php-mysqlnd --allowerasing oniguruma --skip-broken --nobest
    [root@node1 ~]# yum -y install libsqlite3x-devel libxml2-devel libzip-devel
    
    [root@node1 ~]# dnf --enablerepo=powertools install oniguruma-devel
    
    [root@node1 php-8.1.11]#  ./configure --prefix=/usr/local/php8 \
    --with-config-file-path=/etc \
    --enable-fpm \
    --enable-inline-optimization \
    --disable-debug \
    --disable-rpath \
    --enable-shared \
    --enable-soap \
    --with-openssl \
    --enable-bcmath \
    --with-iconv \
    --with-bz2 \
    --enable-calendar \
    --with-curl \
    --enable-exif \
    --enable-ftp \
    --enable-gd \
    --with-jpeg \
    --with-zlib-dir \
    --with-freetype \
    --with-gettext \
    --enable-json \
    --enable-mbstring \
    --enable-pdo \
    --with-mysqli=mysqlnd \
    --with-pdo-mysql=mysqlnd \
    --with-readline \
    --enable-shmop \
    --enable-simplexml \
    --enable-sockets \
    --with-zip \
    --enable-mysqlnd-compression-support \
    --with-pear \
    --enable-pcntl \
    --enable-posix
    [root@node1 php-8.1.11]# make
    [root@node1 php-8.1.11]# make install
    
    //配置环境变量
    [root@node1 php-8.1.11]# echo 'export PATH=/usr/local/php8/bin:$PATH' > /etc/profile.d/php8.sh
    [root@node1 php-8.1.11]# source /etc/profile.d/php8.sh
    [root@node1 php-8.1.11]# which php
    /usr/local/php8/bin/php
    [root@node1 php-8.1.11]# php -v
    PHP 8.1.11 (cli) (built: Oct 19 2022 16:59:13) (NTS)
    Copyright (c) The PHP Group
    Zend Engine v4.1.11, Copyright (c) Zend Technologies
    
    //配置php-fpm
    [root@node1 php-8.1.11]# cp php.ini-production /etc/php.ini
    cp: overwrite '/etc/php.ini'? y
    [root@node1 php-8.1.11]#  cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
    [root@node1 php-8.1.11]# chmod +x /etc/init.d/php-fpm
    [root@node1 php-8.1.11]# cp /usr/local/php8/etc/php-fpm.conf.default /usr/local/php8/etc/php-fpm.conf
    [root@node1 php-8.1.11]# cp /usr/local/php8/etc/php-fpm.d/www.conf.default /usr/local/php8/etc/php-fpm.d/www.conf
    
    //这里写service文件设置开机自启
    [root@node1 php-8.1.11]# vim /usr/lib/systemd/system/php8.service
    [Unit]
    Description=php 
    After=network.target 
    
    [Service]
    Type=forking
    ExecStart=/usr/local/php8/sbin/php-fpm 
    ExecStop=ps -ef |grep php|grep -v grep|awk '{print $2}' |xargs kill -9
    ExecReload=/bin/kill -HUP $MAINPID
    
    [Install]
    WantedBy=multi-user.target
    [root@node1 php-8.1.11]# systemctl daemon-reload
    [root@node1 php-8.1.11]#  systemctl start php-fpm
    [root@node1 php-8.1.11]# ss -antl
    State   Recv-Q  Send-Q   Local Address:Port   Peer Address:Port  Process  
    LISTEN  0       128          127.0.0.1:9000        0.0.0.0:*              
    LISTEN  0       128            0.0.0.0:80          0.0.0.0:*              
    LISTEN  0       128            0.0.0.0:22          0.0.0.0:*
    LISTEN  0       70                   *:33060              *:*             
    LISTEN  0       128                  *:3306               *:*    
    LISTEN  0       128               [::]:22             [::]:*     
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96

    配置nginx

    //创建虚拟主机目录并生成php测试页面
    [root@node1 ~]# mkdir -p /www/abc
    [root@node1 ~]# vim /www/abc/index.php
    
    [root@node1 ~]# chown -R nginx.nginx /www/abc/
    [root@node1 ~]# vim /usr/local/nginx/conf/nginx.conf
     location / {
                root   /www/abc;
                index  index.php index.html index.htm;
    
     
     location ~ \.php$ {
                root           /www/abc;
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  /www/abc$fastcgi_script_name;
                include        fastcgi_params;
    [root@node1 ~]# nginx -s stop
    [root@node1 ~]# nginx
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    访问

    在这里插入图片描述

    在node3主机安装httpd,做静态资源

    [root@node3 ~]# dnf -y install httpd
    [root@node3 ~]# systemctl restart httpd
    [root@node3 ~]# systemctl enable httpd
    Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
    [root@node3 ~]# ss -antl
    State   Recv-Q  Send-Q   Local Address:Port   Peer Address:Port  Process  
    LISTEN  0       128            0.0.0.0:22          0.0.0.0:*              
    LISTEN  0       128                  *:80                *:*              
    LISTEN  0       128               [::]:22             [::]:*     
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    访问

    在这里插入图片描述

    在node2主机源码安装nginx并配置负载均衡器,进行调度

    //创建系统用户nginx
    [root@node2 ~]# useradd -r -M -s /sbin/nologin nginx
    
    //安装依赖环境
    [root@node2 ~]# dnf -y groups mark install 'Development Tools'
    [root@node2 ~]# dnf -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make vim
    
    //创建日志存放目录
    [root@node2 ~]# mkdir -p /var/log/nginx
    [root@node2 ~]# chown -R nginx.nginx /var/log/nginx/
    [root@node2 ~]# ll -d /var/log/nginx/
    drwxr-xr-x. 2 nginx nginx 6 Oct 19 19:18 /var/log/nginx/
    
    //编译安装
    [root@node2 ~]# tar xf nginx-1.22.0.tar.gz 
    [root@node2 ~]# cd nginx-1.22.0
    [root@node2 nginx-1.22.0]# ./configure \
    >     --prefix=/usr/local/nginx \
    >     --user=nginx \
    >     --group=nginx \
    >     --with-debug \
    >     --with-http_ssl_module \
    >     --with-http_realip_module \
    >     --with-http_image_filter_module \
    >     --with-http_gunzip_module \
    >     --with-http_gzip_static_module \
    >     --with-http_stub_status_module \
    >     --http-log-path=/var/log/nginx/access.log \
    >     --error-log-path=/var/log/nginx/error.log
    [root@node2 nginx-1.22.0]# make && make install
    
    //配置环境变量
    [root@node2 ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
    [root@node2 ~]# source /etc/profile.d/nginx.sh
    
    //启动nginx
    [root@node2 ~]# nginx
    [root@node2 ~]# ss -antl
    State   Recv-Q  Send-Q   Local Address:Port   Peer Address:Port  Process  
    LISTEN  0       128            0.0.0.0:80          0.0.0.0:*              
    LISTEN  0       128            0.0.0.0:22          0.0.0.0:*              
    LISTEN  0       128               [::]:22             [::]:*     
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    配置负载均衡

    [root@node2 ~]# vim /usr/local/nginx/conf/nginx.conf
        upstream webserver {
            server 192.168.205.144;
            server 192.168.205.150;
    }
    
        server {
            listen       80;
            server_name  localhost;
    
            location / {
                proxy_pass http://webserver;
    }
    [root@node2 ~]# nginx -t
    nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
    nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
    [root@node2 ~]# nginx -s reload
    [root@node2 ~]# nginx -s stop
    [root@node2 ~]# nginx
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    访问
    在这里插入图片描述

    在这里插入图片描述

    在nginx主机上配置动静分离

    [root@node2 ~]# vim /usr/local/nginx/conf/nginx.conf
       upstream webservers1 {
            server 192.168.205.144;
        }
    
        upstream webservers2 {
            server 192.168.205.150;
        }
            location / {
                proxy_pass http://webservers1;
            }
            location ~ \.php$ {
                proxy_pass   http://webservers2;
            }
    
    [root@node2 ~]# nginx -t
    nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
    nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
    [root@node2 ~]# systemctl restart nginx
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    静态访问

    在这里插入图片描述

    动态访问

    在这里插入图片描述

  • 相关阅读:
    仿真测试断开服务器公网连接
    盲盒商城源码 盲盒开箱源码 潮物盲盒商城源码 仿CSGO盲盒开箱源码
    【C++位图】1. 快速查找某个数据是否在一个集合中 2. 排序(全部插入,遍历一遍) 3. 求两个集合的交集、并集等
    Android进阶——Handler底层fd监听之epoll机制
    SQL Thinking
    pcl 基本数据类型
    03-基于Feign的远程调用,详解Feign的自定义配置和优化,创建Feign模块
    cuda cache相关知识总结
    [Kogel.Subscribe.Mssql]SQL Server增量订阅,数据库变更监听
    Java- IO流
  • 原文地址:https://blog.csdn.net/m0_72915775/article/details/127416499