用nginx做负载均衡,配置动静分离。


用nginx做负载均衡,配置动静分离。

主机名ip服务
lnmp192.168.159.100lnmp架构
LB192.168.159.103nginx
httpd192.168.159.104httpd

lnmp部署

  • nginx源码部署
//关闭防火墙&selinux
[root@lnmp ~]# systemctl disable --now firewalld
[root@lnmp ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/sysconfig/selinux
[root@lnmp ~]# getenforce
Disabled

//下载nginx安装包&解压
[root@lnmp ~]# wget https://nginx.org/download/nginx-1.22.0.tar.gz
[root@lnmp ~]# ls
anaconda-ks.cfg  nginx-1.22.0.tar.gz
[root@lnmp ~]# tar -xf nginx-1.22.0.tar.gz

//创建nginx用户
[root@lnmp ~]# useradd -rMs /sbin/nologin nginx
[root@lnmp ~]# id nginx
uid=995(nginx) gid=992(nginx) groups=992(nginx)

//安装依赖包和工具包组
[root@lnmp ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++
[root@lnmp ~]# yum -y groups mark install 'Development Tools'

//编译安装
[root@lnmp ~]# cd nginx-1.22.0/
[root@lnmp 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@lnmp nginx-1.22.0]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install

//配置环境变量
[root@lnmp ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh

//写一个service文件
[root@lnmp nginx]# vim /usr/lib/systemd/system/nginx.service
[root@lnmp nginx]# cat /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx server daemon
After=network.target sshd-keygen.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

[root@lnmp nginx]# systemctl restart nginx.service
[root@lnmp nginx]# ss -anlt
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                 [::]:*

  • 安装mysql
[root@lnmp ~]# dnf -y install mysql*
//修改密码
mysql> ALTER user 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY '123';
Query OK, 0 rows affected (0.14 sec)

mysql> quit
Bye

[root@lnmp php]# mysql -uroot -p123
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.26 Source distribution

Copyright (c) 2000, 2021, 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>

  • 部署php
//下载php安装包
[root@lnmp ~]# wget https://www.php.net/distributions/php-8.1.11.tar.gz
//解压
[root@lnmp ~]# tar -xf php-8.1.11.tar.gz
[root@lnmp ~]# ls
anaconda-ks.cfg  nginx-1.22.0  nginx-1.22.0.tar.gz  php-8.1.11  php-8.1.11.tar.gz
//下载依赖包
[root@lnmp ~]# dnf -y install wget gcc gcc-c++ make libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libicu-devel libjpeg libjpeg-devel libpng libpng-devel openldap-devel  pcre-devel freetype freetype-devel gmp gmp-devel  readline readline-devel libxslt libxslt-devel  php-mysqlnd  libzip-devel  sqlite-devel
[root@lnmp php-8.1.11]# rpm -ivh https://download-ib01.fedoraproject.org/pub/epel/8/Everything/x86_64/Packages/l/libsqlite3x-devel-20071018-26.el8.x86_64.rpm 
[root@lnmp php-8.1.11]# dnf -y install http://mirror.centos.org/centos/8-stream/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm 
[root@lnmp php-8.1.11]# dnf -y install https://download-ib01.fedoraproject.org/pub/epel/8/Everything/x86_64/Packages/m/mhash-devel-0.9.9.9-20.el8.x86_64.rpm 
[root@lnmp php-8.1.11]# dnf -y install https://download-ib01.fedoraproject.org/pub/epel/8/Everything/x86_64/Packages/l/libmcrypt-2.5.8-26.el8.x86_64.rpm 
[root@lnmp php-8.1.11]# dnf -y install https://download-ib01.fedoraproject.org/pub/epel/8/Everything/x86_64/Packages/l/libmcrypt-devel-2.5.8-26.el8.x86_64.rpm 
[root@lnmp php-8.1.11]# dnf -y install https://download-ib01.fedoraproject.org/pub/epel/8/Everything/x86_64/Packages/m/mhash-0.9.9.9-20.el8.x86_64.rpm 

//编译安装
[root@lnmp php-8.1.11]#./configure --prefix=/usr/local/php8  \
--with-config-file-path=/etc \
--enable-fpm \
--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-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@lnmp php-8.1.11]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install
//配置服务
[root@lnmp php-8.1.11]# cd /usr/local/php/
[root@lnmp php]# cp etc/php-fpm.conf.default etc/php-fpm.conf
[root@lnmp php]# cp etc/php-fpm.d/www.conf.default etc/php-fpm.d/www.conf
//配置php的service配置文件
[root@lnmp php]# vim /usr/lib/systemd/system/php.service
[root@lnmp php]# cat /usr/lib/systemd/system/php.service
[Unit]
Description=php server daemon
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/php/sbin/php-fpm
ExecStop=ps -ef |grep php |grep -v grep|awk '{print}'|xargs kill -9
ExecReload=/bin/kill -HUP

[Install]
WantedBy=multi-user.target

[root@lnmp php]# systemctl daemon-reload
[root@lnmp php]# systemctl enable --now php.service
Created symlink /etc/systemd/system/multi-user.target.wants/php.service → /usr/lib/systemd/system/php.service.

[root@lnmp php]# ss -anlt
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                  [::]:*

//编写测试主页php端
[root@lnmp php]# mkdir -p /usr/local/nginx/html
[root@lnmp php]# cat > /usr/local/nginx/html/index.php << EOF
> <?php
>     phpinfo();
> ?>
> EOF
//创建一个虚拟主机
[root@lnmp conf]# pwd
/usr/local/nginx/conf
[root@lnmp conf]# vim nginx.conf
    server {
        listen 82;
        location / {
            root html/static;
            index index.html;
        }

    }
[root@lnmp html]# mkdir static
[root@lnmp html]# echo 'nginx' > static/index.html
[root@lnmp html]# ls
50x.html  index.html  index.html.bak  index.php

[root@lnmp conf]# systemctl restart nginx.service

//将php与nginx连接
[root@lnmp php]# vim /usr/local/nginx/conf/nginx.conf
  location / {
            root   html;
            index  index.html index.htm index.php;//添加index.php
        }

……	//取消注释下面的一段
        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;  		      			 			fastcgi_param  SCRIPT_FILENAME 			$document_root$fastcgi_script_name;#将/script改为$document_root	
            include        fastcgi_params;
        }

配置lamp

[root@httpd ~]# dnf -y install httpd		
[root@httpd ~]# systemctl enable --now httpd
[root@httpd ~]# dnf -y module install mariadb*
[root@httpd ~]# systemctl enable --now mariadb
[root@httpd ~]# dnf -y install php
[root@httpd ~]# systemctl enable --now php-fpm.service
//配置httpd
[root@httpd conf]# vim /etc/httpd/conf/httpd.conf
<IfModule dir_module>
    DirectoryIndex index.html index.php	//新增index.php
</IfModule>
//写一个php测试页面
[root@httpd conf]# cd /var/www/html/
[root@httpd html]# cat >index.php <<EOF
> <?php
>   phpinfo();
> ?>
> EOF

//把vhost的模板copy到conf.d目录下
[root@httpd html]# find / -name httpd-vhost*
/usr/share/doc/httpd/httpd-vhosts.conf
[root@httpd ~]# cd /etc/httpd/conf.d/
[root@httpd conf.d]# cp /usr/share/doc/httpd/httpd-vhosts.conf .
//配置虚拟主机
[root@httpd ~]# cd /etc/httpd/conf.d/
[root@httpd conf.d]# cp /usr/share/doc/httpd/httpd-vhosts.conf .
[root@httpd conf.d]# vim httpd-vhosts.conf
[root@httpd conf.d]# cat httpd-vhosts.conf
Listen 82
<VirtualHost 192.168.159.104:82>
    DocumentRoot "/var/www/vhost1"
    ErrorLog "/var/log/httpd/error_log"
    CustomLog "/var/log/httpd/access_log" combined
</VirtualHost>

//创建vhost的网页存放目录
[root@httpd conf.d]# cd /var/www
[root@httpd www]# mkdir vhost1
[root@httpd www]# echo 'apache' >vhost1/index.html
[root@httpd www]# systemctl restart httpd
[root@httpd www]# ss -anlt
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                      *:82                    *:*
LISTEN   0        128                   [::]:22                 [::]:*
LISTEN   0        80                       *:3306                  *:*

配置LB

[root@LB ~]# dnf -y install nginx


..........
    upstream static {
        server 192.168.159.104:82;
        server 192.168.159.100:82;
    }

    upstream dynamic {
        server 192.168.159.104;
        server 192.168.159.100;
    }

。。。。。。。。
        location / {
            proxy_pass http://static;
        }

        location ~ \.php$ {
            proxy_pass http://dynamic;
        }

[root@LB nginx]# systemctl restart nginx.service	

测试访问

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

1we11

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值