nginx实现负载均衡和动静分离!!

实验环境!!!

服务IP
nginx192.168.160.128
httpd192.168.160.129
lnmp192.168.160.130

1. 部署nginx!!!

[root@lvs1 ~]# useradd -r -M -s /sbin/nologin nginx
[root@lvs1 ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ wget make

  pkgconf-pkg-config-1.4.2-1.el8.x86_64   wget-1.19.5-10.el8.x86_64                 xorg-x11-proto-devel-2020.1-3.el8.noarch  
  zlib-devel-1.2.11-17.el8.x86_64        

完毕!
[root@lvs1 ~]# yum -y groups mark install 'Development Tools'


[root@lvs1 ~]# tar xf nginx-1.20.1.tar.gz
[root@lvs1 ~]# cd nginx-1.20.1/
[root@lvs1 nginx-1.20.1]# ./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@lvs1 nginx-1.20.1]# make && make install

[root@lvs1 nginx-1.20.1]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@lvs1 nginx-1.20.1]# . /etc/profile.d/nginx.sh
[root@lvs1 nginx-1.20.1]# nginx
[root@lvs1 nginx-1.20.1]# 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                           [::]:*

在这里插入图片描述

2. 部署httpd!!!!

[root@lvs2 ~]# yum -y install openssl-devel pcre-devel expat-devel libtool gcc-c++ libstdc++-devel make
CentOS Stream 8 - AppStream                                                                     1.1 MB/s |  10 MB     00:09    
CentOS Stream 8 - BaseOS                                                                        988 kB/s | 6.8 MB     00:07    
CentOS Stream 8 - Extras                                                                        5.4 kB/s |  13 kB     00:02    
上次元数据过期检查:0:00:01 前,执行于 2021年06月29日 星期二 23时33分48秒。
依赖关系解决。
================================================================================================================================
 软件包                           架构             版本                                               仓库                 大小
================================================================================================================================
安装:
 expat-devel                      x86_64           2.2.5-4.el8     

[root@lvs2 ~]# ls
anaconda-ks.cfg  apr-1.7.0.tar.bz2  apr-util-1.6.1.tar.bz2  httpd-2.4.43.tar.bz2
  
[root@lvs2 ~]# tar xf apr-1.7.0.tar.bz2 
[root@lvs2 ~]# tar xf apr-util-1.6.1.tar.bz2 
[root@lvs2 ~]# tar xf httpd-2.4.43.tar.bz2 

[root@lvs2 apr-1.7.0]# vim configure
    cfgfile=${ofile}T
    trap "$RM \"$cfgfile\"; exit 1" 1 2 15
#    $RM "$cfgfile"

[root@lvs2 apr-1.7.0]# ./configure --prefix=/usr/local/apr
[root@lvs2 apr-1.7.0]# make && make install
[root@lvs2 ~]# cd apr-util-1.6.1
[root@lvs2 apr-util-1.6.1]#  ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[root@lvs2 apr-util-1.6.1]# make && make install


[root@lvs2 httpd-2.4.43]# ./configure --prefix=/usr/local/apache \
--sysconfdir=/etc/httpd24 \
--enable-so \
--enable-ssl \
--enable-cgi \
--enable-rewrite \
--with-zlib \
--with-pcre \
--with-apr=/usr/local/apr \
--with-apr-util=/usr/local/apr-util/ \
--enable-modules=most \
--enable-mpms-shared=all \
--with-mpm=prefork
[root@lvs2 httpd-2.4.43]# make
[root@lvs2 httpd-2.4.43]# make install

[root@lvs2 local]# groupadd -r apache 
[root@lvs2 local]# useradd -r -M -s /sbin/nologin -g apache apache
[root@lvs2 local]# chown -R apache.apache /usr/local/apache/
[root@lvs2 ~]# sed -i '/#ServerName/s/#//g' /etc/httpd24/httpd.conf 


[root@lvs2 ~]# vim /etc/systemd/system/httpd.service 
[root@lvs2 ~]# cat /etc/systemd/system/httpd.service 
cription=Start httpd
[Service]
Type=simple
EnvironmentFile=/etc/httpd24/httpd.conf
ExecStart=/usr/local/apache/bin/httpd -k start -DFOREGROUND
ExecReload=/usr/local/apache/bin/httpd -k graceful
ExecStop=/bin/kill -WINCH ${MAINPID}
[Install]
WantedBy=multi-user.target


[root@lvs2 ~]# systemctl start httpd
[root@lvs2 ~]# systemctl disable firewalld.service 
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@lvs2 ~]# systemctl stop firewalld.service 
[root@lvs2 ~]# setenforce 0
[root@lvs2 local]# 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                           [::]:*                          
[root@lvs2 local]# 

3. 部署 lnmp!!!

3.1 安装 nginx

#创建nginx用户和组
[root@lnmp ~]# useradd -r -M -s /sbin/nologin nginx
#安装依赖环境
[root@lnmp ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ wget make
[root@lnmp ~]# yum -y groups mark install 'Development Tools'
#创建日志存放目录
[root@lnmp ~]# mkdir -p /var/log/nginx
[root@lnmp ~]# chown -R nginx.nginx /var/log/nginx
#下载nginx
[root@lnmp ~]# wget https://nginx.org/download/nginx-1.20.1.tar.gz
#编译安装l
[root@lnmp ~]# tar xf nginx-1.20.1.tar.gz
[root@lnmp ~]# cd nginx-1.20.1/
[root@lnmp nginx-1.20.1]# ./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.20.1]# make && make install
#配置环境变量
[root@lnmp nginx-1.20.1]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@lnmp nginx-1.20.1]# . /etc/profile.d/nginx.sh
#启动nginx
[root@lnmp ~]# nginx 
[root@lnmp ~]# 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                         [::]:*

3.2 安装mysql

#安装依赖包
[root@lnmp ~]# dnf -y install ncurses-devel openssl-devel openssl cmake mariadb-devel

#创建mysql服务的用户和组
[root@lnmp ~]# useradd -r -M -s /sbin/nologin mysql

#下载二进制格式的mysql软件包
[root@lnmp ~]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz

#解压到/usr/local/里
[root@lnmp ~]# tar xf mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@lnmp ~]# cd /usr/local/
[root@lnmp local]# ls
apache  apr  apr-util  bin  etc  games  include  lib  lib64  libexec  mysql-5.7.33-linux-glibc2.12-x86_64  sbin  share  src
[root@lnmp local]# mv mysql-5.7.33-linux-glibc2.12-x86_64/ mysql
[root@lnmp local]# ls
apache  apr  apr-util  bin  etc  games  include  lib  lib64  libexec  mysql  sbin  share  src

#修改属主和属组
[root@lnmp local]# chown -R mysql.mysql mysql/

#添加环境变量
[root@lnmp ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@lnmp ~]# source /etc/profile.d/mysql.sh 

#给头文件做软连接
[root@lnmp ~]# ln -s /usr/local/mysql/include /usr/include/mysql

#库文件
[root@lnmp ~]# vim /etc/ld.so.conf.d/mysql.conf
/usr/local/mysql/lib		#添加这个
[root@lnmp ~]# ldconfig 

#man文档
[root@lnmp ~]# vim /etc/man_db.conf 
MANDATORY_MANPATH                       /usr/man
MANDATORY_MANPATH                       /usr/share/man
MANDATORY_MANPATH                       /usr/local/share/man
MANDATORY_MANPATH                       /usr/local/apache/man
MANDATORY_MANPATH                       /usr/local/mysql/man		#添加这一行内容

#创建数据存放目录
[root@lnmp ~]# mkdir /opt/data
[root@lnmp ~]# chown -R mysql.mysql /opt/data

#初始化数据库
root@lnmp ~]# mysqld --initialize --user=mysql --datadir=/opt/data/
2021-05-12T14:25:38.875806Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-05-12T14:25:39.516844Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-05-12T14:25:39.602382Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-05-12T14:25:39.658834Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: ed087633-b32d-11eb-b3ef-000c29a3d1ed.
2021-05-12T14:25:39.666061Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-05-12T14:25:40.418925Z 0 [Warning] CA certificate ca.pem is self signed.
2021-05-12T14:25:40.521392Z 1 [Note] A temporary password is generated for root@localhost: D!;GR)Q%o8Cs		#记住这个密码后面会用到
[root@lnmp ~]# cat > /etc/my.cnf <<EOF
[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@lnmp ~]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@lnmp ~]# vim /etc/init.d/mysqld
basedir=/usr/local/mysql
datadir=/opt/data

#启动脚本并修改密码
[root@lnmp ~]# service mysqld start 
Starting MySQL.Logging to '/opt/data/localhost.localdomain.err'.
 SUCCESS! 
[root@lnmp ~]# 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               80                                   *:3306                              *:*                            
LISTEN          0               128                                  *:80                                *:*                            
LISTEN          0               128                               [::]:22                             [::]:*                            
[root@lnmp ~]# dnf -y install ncurses-compat-libs
[root@lnmp ~]# mysql -uroot -p'D!;GR)Q%o8Cs'
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 2
Server version: 5.7.33

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> set password = password('asd123321');
Query OK, 0 rows affected, 1 warning (0.00 sec)

#设置开机自启
[root@lnmp ~]# chkconfig --add mysqld
[root@lnmp ~]# chkconfig --list

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

mysqld         	0:off	1:off	2:on	3:on	4:on	5:on	6:off

3.3 安装php!!!

#安装依赖包
[root@lnmp ~]# dnf -y install 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 libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel

#安装php
[root@lnmp ~]# dnf -y install php*
[root@lnmp ~]# dnf -y remove httpd

#启动php-fpm
[root@lnmp ~]# service php-fpm start
#设置开机自启
[root@lnmp ~]# systemctl enable php-fpm
Created symlink /etc/systemd/system/multi-user.target.wants/php-fpm.service → /usr/lib/systemd/system/php-fpm.service.
#编辑配置文件
[root@lnmp ~]# vim /etc/php-fpm.d/www.conf
; Note: This value is mandatory.
;listen = /run/php-fpm/www.sock
listen = 0.0.0.0:9000		#添加这一个把上面的用‘;’注释掉
[root@localhost ~]# systemctl restart php-fpm
[root@localhost ~]# ss -antl
State           Recv-Q          Send-Q                   Local Address:Port                   Peer Address:Port         Process         
LISTEN          0               128                            0.0.0.0:9000                        0.0.0.0:*                            
LISTEN          0               128                            0.0.0.0:22                          0.0.0.0:*                            
LISTEN          0               80                                   *:3306                              *:*                            
LISTEN          0               128                                  *:80                                *:*                            
LISTEN          0               128                               [::]:22   

3.4 配置nginx!!!

[root@lnmp ~]# vim /usr/local/nginx/conf/nginx.conf
        location / {
            root   html;
            index  index.php index.html index.htm;
        }


      location ~ \.php$ {
            root           /usr/local/nginx/html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

3.5 配置nginx主机的nginx配置文件!!!

[root@lvs1 nginx-1.20.1]# vim /usr/local/nginx/conf/nginx.conf
        location / {
#            root   html;
            index  index.html index.htm;
        }

     upstream html {
            server 192.168.160.129;
    }
    upstream php {
            server 192.168.160.130;
    }

        location / {
            proxy_pass http://html;
        }


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

        # pass

3.6 添加静态与动态!!!

#httpd的主机上
[root@lvs2 local]# cd /usr/local/apache/htdocs/
[root@lvs2 htdocs]# echo 'laolin' > index.html
[root@lvs2 htdocs]# 

#lnmp主机上!
[root@lnmp ~]# cd /usr/local/nginx/html/
[root@lnmp html]# vim index.php
[root@lnmp html]# ls
50x.html  index.html  index.php
[root@lnmp html]# mv index.html /opt/
[root@lnmp html]# ls
50x.html  index.php

4. 测试!!!

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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值