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


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

    环境说明

    主机名IP地址应用系统
    nginx192.168.183.135nginx负载均衡centos8
    RS1192.168.183.136LNMPcentos8
    RS2192.168.183.137apachecentos8

    1、在RS2主机上安装apache服务

    //修改主机名关闭防火墙和selinux
    [root@localhost ~]# hostnamectl set-hostname RS2
    [root@localhost ~]# bash
    [root@RS2 ~]# systemctl disable --now firewalld
    Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
    Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
    [root@RS2 ~]# setenforce 0
    [root@RS2 ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
    
    //安装httpd
    [root@RS2 ~]# dnf -y install httpd
    
    //开启并设置开机自启httpd
    [root@RS2 ~]# systemctl enable --now httpd
    Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
    [root@RS2 ~]# 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
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    访问测试

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-BVPcw31b-1666246372524)(C:\Users\shuai\AppData\Roaming\Typora\typora-user-images\image-20221019154227969.png)]

    2、在RS1主机上部署LNMP架构

    //修改主机名关闭防火墙和selinux
    [root@localhost ~]# hostnamectl set-hostname RS1
    [root@localhost ~]# bash
    [root@RS1 ~]# setenforce 0
    [root@RS1 ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
    [root@RS1 ~]# systemctl disable --now firewalld
    Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
    Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    1. 安装nginx,版本1.22

    //创建用户
    [root@RS1 ~]# useradd -rMs /sbin/nologin nginx
    
    //安装依赖
    [root@RS1 ~]# dnf -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make wget vim
    
    //下载软件包并解压
    [root@RS1 ~]# wget http://nginx.org/download/nginx-1.20.0.tar.gz
    --2022-10-19 15:50:24--  http://nginx.org/download/nginx-1.20.0.tar.gz
    Resolving nginx.org (nginx.org)... 52.58.199.22, 3.125.197.172, 2a05:d014:edb:5704::6, ...
    Connecting to nginx.org (nginx.org)|52.58.199.22|:80... connected.
    HTTP request sent, awaiting response... 200 OK
    Length: 1061070 (1.0M) [application/octet-stream]
    Saving to: ‘nginx-1.20.0.tar.gz’
    
    nginx-1.20.0.tar.gz 100%[================>]   1.01M   230KB/s    in 5.0s    
    
    2022-10-19 15:50:30 (207 KB/s) - ‘nginx-1.20.0.tar.gz’ saved [1061070/1061070]
    
    [root@RS1 ~]# tar -xf nginx-1.20.0.tar.gz 
    
    //进入解压目录进行编译
    [root@RS1 ~]# cd nginx-1.20.0
    [root@RS1 nginx-1.20.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@RS1 nginx-1.20.0]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install
    
    //安装完成
    [root@RS1 nginx-1.20.0]# cd /usr/local/nginx/
    [root@RS1 nginx]# ls
    conf  html  logs  sbin
    
    //配置环境变量
    [root@RS1 nginx]# cd
    [root@RS1 ~]# echo "export PATH=$PATH:/usr/local/nginx/sbin" > /etc/profile.d/nginx.sh
    [root@RS1 ~]# source /etc/profile.d/nginx.sh
    
    
    //编写service文件启动服务
    [root@RS1 ~]# cat > /usr/lib/systemd/system/nginx.service <
    > [Unit]
    > Description=nginx server daemon
    > After=network.target
    > 
    > [Service]
    > Type=forking
    > ExecStart=/usr/local/nginx/sbin/nginx
    > ExecStop=/usr/local/nginx/sbin/nginx -s stop
    > ExecReload=/bin/kill -HUP \$MAINPID
    > 
    > [Install]
    > WantedBy=multi-user.target
    > EOF
    [root@RS1 ~]# systemctl daemon-reload
    [root@RS1 ~]# systemctl enable --now nginx
    Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
    [root@RS1 ~]# 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
    • 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

    2. 安装mysql,版本8.0

    //安装依赖
    [root@RS1 ~]# dnf -y install ncurses-devel openssl-devel openssl cmake mariadb-devel ncurses-compat-libs
    
    //创建用户
    [root@RS1 ~]# useradd -rMs /sbin/nologin mysql
    
    //下载软件包并解压到/usr/local/目录
    [root@RS1 ~]#  wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz
    --2022-10-19 16:13:40--  https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz
    Resolving dev.mysql.com (dev.mysql.com)... 223.119.203.77, 2600:140e:6:aaf::2e31, 2600:140e:6:a94::2e31
    Connecting to dev.mysql.com (dev.mysql.com)|223.119.203.77|:443... connected.
    HTTP request sent, awaiting response... 302 Moved Temporarily
    Location: https://cdn.mysql.com//archives/mysql-8.0/mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz [following]
    --2022-10-19 16:13:40--  https://cdn.mysql.com//archives/mysql-8.0/mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz
    Resolving cdn.mysql.com (cdn.mysql.com)... 23.36.48.238
    Connecting to cdn.mysql.com (cdn.mysql.com)|23.36.48.238|:443... connected.
    HTTP request sent, awaiting response... 200 OK
    Length: 490922012 (468M) [text/plain]
    Saving to: ‘mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz’
    
    mysql-8.0.20-linux- 100%[================>] 468.18M  8.85MB/s    in 48s     
    
    2022-10-19 16:14:29 (9.67 MB/s) - ‘mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz’ saved [490922012/490922012]
    
    [root@RS1 ~]# tar xf mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz -C /usr/local/
    [root@RS1 ~]# cd /usr/local/
    [root@RS1 local]# ls
    bin  games    lib    libexec                              nginx  share
    etc  include  lib64  mysql-8.0.20-linux-glibc2.12-x86_64  sbin   src
    [root@RS1 local]# mv mysql-8.0.20-linux-glibc2.12-x86_64 mysql
    [root@RS1 local]# ls
    bin  games    lib    libexec  nginx  share
    etc  include  lib64  mysql    sbin   src
    [root@RS1 local]# chown -R mysql.mysql mysql
    [root@RS1 local]# ll
    total 0
    drwxr-xr-x.  2 root  root    6 Jun 22  2021 bin
    drwxr-xr-x.  2 root  root    6 Jun 22  2021 etc
    drwxr-xr-x.  2 root  root    6 Jun 22  2021 games
    drwxr-xr-x.  2 root  root    6 Jun 22  2021 include
    drwxr-xr-x.  2 root  root    6 Jun 22  2021 lib
    drwxr-xr-x.  3 root  root   17 Aug 31 18:58 lib64
    drwxr-xr-x.  2 root  root    6 Jun 22  2021 libexec
    drwxr-xr-x.  9 mysql mysql 129 Oct 19 16:18 mysql
    drwxr-xr-x. 11 root  root  151 Oct 19 15:56 nginx
    drwxr-xr-x.  2 root  root    6 Jun 22  2021 sbin
    drwxr-xr-x.  5 root  root   49 Aug 31 18:58 share
    drwxr-xr-x.  2 root  root    6 Jun 22  2021 src
    
    //配置include、man及环境变量
    [root@RS1 ~]# ln -s /usr/local/mysql/include /usr/include/mysql
    [root@RS1 ~]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
    [root@RS1 ~]# vim /etc/man_db.conf 
    MANDATORY_MANPATH        /usr/local/mysql/man
    [root@RS1 ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
    [root@RS1 ~]# source /etc/profile.d/mysql.sh
    [root@RS1 ~]# which mysql
    /usr/local/mysql/bin/mysql
    
    //建立数据存放目录
    [root@RS1 ~]# mkdir -p /opt/data
    [root@RS1 ~]# chown -R mysql.mysql /opt/data/
    
    //初始化数据库
    [root@RS1 ~]# mysqld --initialize --user mysql --datadir /opt/data
    2022-10-19T08:23:03.589258Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.20) initializing of server in progress as process 35271
    2022-10-19T08:23:03.600911Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
    2022-10-19T08:23:04.045156Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
    2022-10-19T08:23:04.807412Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: s83id>KFskYr
    
    [root@RS1 ~]# echo 's83id>KFskYr' > pass
    
    //生成配置文件
    [root@RS1 ~]# cat > /etc/my.cnf <
    > [mysqld]
    > basedir = /usr/local/mysql
    > datadir = /opt/data
    > socket = /tmp/mysql.sock
    > port = 3306
    > pid-file = /opt/data/mysql.pid
    > user = mysql
    > skip-name-resolve
    > EOF
    
    //配置服务启动脚本
    [root@RS1 ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
    [root@RS1 ~]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld
    [root@RS1 ~]# sed -ri 's#^(datadir=).*#\1/opt/data#g' /etc/init.d/mysqld
    [root@RS1 ~]# chmod +x /etc/init.d/mysqld
    
    //编写service文件启动服务
    [root@RS1 ~]# cat > /usr/lib/systemd/system/mysqld.service <
    > [Unit]
    > Description=mysqld server daemon
    > After=network.target
    > 
    > [Service]
    > Type=forking
    > ExecStart=/etc/init.d/mysqld start
    > ExecStop=/etc/init.d/mysqld stop
    > ExecReload=/bin/kill -HUP \$MAINPID
    > 
    > [Install]
    > WantedBy=multi-user.target
    > EOF
    [root@RS1 ~]# systemctl daemon-reload
    [root@RS1 ~]# systemctl enable --now mysqld
    [root@RS1 ~]# 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                [::]:*                 
    LISTEN  0       128                  *:3306                 *:*         
    //登录mysql
    [root@RS1 ~]# vim .my.cnf
    [root@RS1 ~]# cat .my.cnf
    [client]
    user=root
    password=s83id>KFskYr
    [root@RS1 ~]# mysql
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 12
    Server version: 8.0.20
    
    Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
    
    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> 
    
    • 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
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133

    3. 安装php,版本8.1

    //配置yum源
    [root@RS1 ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
    [root@RS1 ~]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
    
    //安装依赖
    [root@RS1 ~]# yum -y install epel-release
    [root@RS1 ~]# yum -y install openssl-devel pcre-devel expat-devel libtool gcc gcc-c++ wget make ncurses-devel openssl cmake libxm12 libxm12-devel bzip2 bzip2-devel libcurl libcurl-devel libicu-devel libjpeg libjpeg-devel libpng libpng-devel openldap-devel freetype freetype-devel gmp gmp-devel readline readline-devel libxslt libxslt-devel php-mysqlnd libsqlite3x-devel libzip-devel https://dl.rockylinux.org/pub/rocky/9/CRB/x86_64/os/Packages/o/oniguruma-devel-6.9.6-1.el9.5.x86_64.rpm http://mirror.stream.centos.org/9-stream/CRB/x86_64/os/Packages/libzip-devel-1.7.3-7.el9.x86_64.rpm http://mirror.centos.org/centos/8-stream/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm --allowerasing --skip-broken --nobest
    
    //下载php软件包并解压编译
    [root@RS1 ~]# wget https://www.php.net/distributions/php-8.1.11.tar.gz
    --2022-10-19 16:49:32--  https://www.php.net/distributions/php-8.1.11.tar.gz
    Resolving www.php.net (www.php.net)... 185.85.0.29, 2a02:cb40:200::1ad
    Connecting to www.php.net (www.php.net)|185.85.0.29|:443... connected.
    HTTP request sent, awaiting response... 200 OK
    Length: 19725720 (19M) [application/octet-stream]
    Saving to: ‘php-8.1.11.tar.gz’
    
    php-8.1.11.tar.gz   100%[================>]  18.81M  4.66MB/s    in 4.0s    
    
    2022-10-19 16:49:39 (4.66 MB/s) - ‘php-8.1.11.tar.gz’ saved [19725720/19725720]
    
    [root@RS1 ~]# tar xf php-8.1.11.tar.gz 
    [root@RS1 ~]# cd php-8.1.11
    [root@RS1 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@RS1 php-8.1.11]# make
    [root@RS1 php-8.1.11]# make install
    
    //配置环境变量
    [root@RS1 php-8.1.11]# echo 'export PATH=/usr/local/php8/bin:$PATH' > /etc/profile.d/php8.sh
    [root@RS1 php-8.1.11]# source /etc/profile.d/php8.sh
    [root@RS1 php-8.1.11]# which php
    /usr/local/php8/bin/php
    [root@RS1 php-8.1.11]# php -v
    PHP 8.1.11 (cli) (built: Oct 19 2022 17:01:35) (NTS)
    Copyright (c) The PHP Group
    Zend Engine v4.1.11, Copyright (c) Zend Technologies
    
    //配置php-fpm
    [root@RS1 php-8.1.11]# cp php.ini-production /etc/php.ini
    cp: overwrite '/etc/php.ini'? y
    [root@RS1 php-8.1.11]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
    [root@RS1 php-8.1.11]# chmod +x /etc/init.d/php-fpm
    [root@RS1 php-8.1.11]# cp /usr/local/php8/etc/php-fpm.conf.default /usr/local/php8/etc/php-fpm.conf
    [root@RS1 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@RS1 php-8.1.11]# cat > /usr/lib/systemd/system/php.service <
    > [Unit]
    > Description=php server daemon
    > After=network.target
    > 
    > [Service]
    > Type=forking
    > ExecStart=/etc/init.d/php-fpm start
    > ExecStop=/etc/init.d/php-fpm stop
    > ExecReload=/bin/kill -HUP $MAINPID
    > 
    > [Install]
    > WantedBy=multi-user.target
    > EOF
    [root@RS1 php-8.1.11]# systemctl daemon-reload
    [root@RS1 php-8.1.11]# systemctl enable --now php
    Created symlink /etc/systemd/system/multi-user.target.wants/php.service → /usr/lib/systemd/system/php.service.
    [root@RS1 php-8.1.11]# 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          127.0.0.1:9000           0.0.0.0:*              
    LISTEN  0       128               [::]:22                [::]:*              
    LISTEN  0       128                  *:3306                 *:*        
    
    • 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

    4. 配置访问php测试页面

    [root@RS1 ~]# cd /usr/local/nginx/html/
    [root@RS1 html]# cat > index.php <
    > <?php
    >         phpinfo();
    > ?>
    > EOF
    [root@RS1 html]# chown -R nginx.nginx index.php
    [root@RS1 ~]# vim /usr/local/nginx/conf/nginx.conf
    
        	location / {
                root   html;
                index  index.php index.html index.htm;
            }
            ······
    		location ~ \.php$ {
                root           html;
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;
                include        fastcgi_params;
            }
    [root@RS1 ~]# systemctl restart nginx
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    访问测试
    在这里插入图片描述

    3、在nginx主机上配置负载均衡及动静分离

    1. 安装nginx

    //修改主机名关闭防火墙
    [root@localhost ~]# hostnamectl set-hostname nginx
    [root@localhost ~]# bash
    [root@nginx ~]# setenforce 0
    [root@nginx ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
    [root@nginx ~]# systemctl disable --now firewalld
    Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
    Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
    
    //创建用户
    [root@nginx ~]# useradd -rMs /sbin/nologin nginx
    
    //安装依赖包
    [root@nginx ~]# dnf -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make wget vim
    
    //下载nginx软件包并解压
    [root@nginx ~]# wget http://nginx.org/download/nginx-1.20.2.tar.gz
    --2022-10-19 16:55:41--  http://nginx.org/download/nginx-1.20.2.tar.gz
    Resolving nginx.org (nginx.org)... 3.125.197.172, 52.58.199.22, 2a05:d014:edb:5704::6, ...
    Connecting to nginx.org (nginx.org)|3.125.197.172|:80... connected.
    HTTP request sent, awaiting response... 200 OK
    Length: 1062124 (1.0M) [application/octet-stream]
    Saving to: ‘nginx-1.20.2.tar.gz’
    
    nginx-1.20.2.tar.gz 100%[================>]   1.01M   331KB/s    in 3.1s    
    
    2022-10-19 16:55:45 (331 KB/s) - ‘nginx-1.20.2.tar.gz’ saved [1062124/1062124]
    
    [root@nginx ~]# tar -xf nginx-1.20.2.tar.gz
    
    //进入目录配置编译安装
    [root@nginx ~]# cd nginx-1.20.2
    [root@nginx nginx-1.20.2]# ./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@nginx nginx-1.20.2]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install
    
    //配置环境变量
    [root@nginx ~]# echo "export PATH=$PATH:/usr/local/nginx/sbin" > /etc/profile.d/nginx.sh
    [root@nginx ~]# source /etc/profile.d/nginx.sh
    
    //编写service文件启动服务
    [root@nginx ~]# cat > /usr/lib/systemd/system/nginx.service << EOF
    > [Unit]
    > Description=nginx server daemon
    > After=network.target
    >  
    > [Service]
    > Type=forking
    > ExecStart=/usr/local/nginx/sbin/nginx
    > ExecStop=/usr/local/nginx/sbin/nginx -s stop
    > ExecReload=/bin/kill -HUP \$MAINPID
    >  
    > [Install]
    > WantedBy=multi-user.target
    > EOF
    [root@nginx ~]# systemctl daemon-reload
    [root@nginx ~]# systemctl enable --now nginx
    Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
    [root@nginx ~]# 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
    • 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

    2. 在nginx主机上配置负载均衡

    [root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
    
        upstream webservers {
            server 192.168.183.136;
            server 192.168.183.137;
            }
    
        server {
            listen       80;
            server_name  localhost;
        location / {
                proxy_pass http://webservers;
            }
    [root@nginx ~]# 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@nginx ~]# systemctl restart nginx
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    访问测试

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-FfNTbjKH-1666246372525)(C:\Users\shuai\AppData\Roaming\Typora\typora-user-images\image-20221019172917992.png)]

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-2iKxCQ16-1666246372525)(C:\Users\shuai\AppData\Roaming\Typora\typora-user-images\image-20221019173006686.png)]

    3. 在nginx主机上配置动静分离

    [root@nginx ~]# !vim
    vim /usr/local/nginx/conf/nginx.conf
        upstream webservers1 {
            server 192.168.183.136;
        }
    
        upstream webservers2 {
            server 192.168.183.137;
        }
        server {
            listen       80;
            server_name  localhost;
        location / {
            proxy_pass http://webservers2;
            }
    
        location ~ \.php$ {
            proxy_pass http://webservers1;
            }
    
    [root@nginx ~]# 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@nginx ~]# !system
    systemctl restart nginx
    
    
    • 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

    访问测试

    1.访问根目录下的静态资源会跳转到192.168.183.137主机上访问到httpd的主页;

    2.访问根目录下的.php动态资源会跳转到192.168.183.136主机上访问到LNMP的php测试页面;

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4pRJwuK8-1666246372525)(C:\Users\shuai\AppData\Roaming\Typora\typora-user-images\image-20221019180023146.png)]
    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0jnNBhf2-1666246372526)(C:\Users\shuai\AppData\Roaming\Typora\typora-user-images\image-20221019180010434.png)]

  • 相关阅读:
    AI视频批量自动剪辑软件
    每日三题 7.5
    《科研》简介及投稿要求
    红队隧道应用篇之Frp内网穿透(十)
    UE学习笔记-- UE4 相比较 UE5 的 double属性,删除编辑器界面右边的图标。
    对本地的静态html文件发送http请求
    MySQL之InnoDB引擎(六)
    【FAQ】关于华为地图服务定位存在偏差的原因及解决办法
    华为云云耀云服务器L实例评测|centos系统搭建git私服
    第一次在linux服务器上部署项目,遇到的问题总结(包括mysql安装,jar打开遇到的问题)
  • 原文地址:https://blog.csdn.net/qq_65998623/article/details/127426260