我是靠谱客的博主 健康世界,最近开发中收集的这篇文章主要介绍lnmt实现负载均衡和反向代理,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

需求说明

搭建lnmt环境,实现反向代理和负载均衡

环境说明

ip服务器类型
172.16.11.18tomcat
172.16.11.19nginx
172.16.11.20mysql

①.关闭防火墙

[root@lizihan ~]# systemctl stop firewalld.service 

②.关闭selinux

[root@lizihan ~]# setenforce 0
[root@lizihan ~]# sed -ri 's/^(SELINUX=).*/1disabled/g' /etc/selinux/config 

③.准备好需要的源码包和二进制包,可以自行下载

[root@lizihan ~]# ls
anaconda-ks.cfg   mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz
apache-tomcat-9.0.8.tar.gz  nginx-1.14.0.tar.xz


nginx-1.14.0
mysql-5.7-22
tomcat-9.0.8

④.配置一个网络源

[root@lizihan ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
[root@lizihan yum.repos.d]# sed -i 's/$releasever/7/g' CentOS-Base.repo 

操作步骤

安装

  • 172.16.11.19

安装nginx

//安装开发工具包 ‘Development Tools’
[root@lizihan ~]# yum -y groupinstall 'Development Tools'

//安装依赖包
[root@lizihan ~]# yum -y install openssl-devel pcre-devel openssl-devel gd-devel gcc gcc-c++


//创建nginx的用户和组
[root@lizihan ~]# groupadd -r nginx
[root@lizihan ~]# useradd -r -M -s /sbin/nologin -g nginx nginx

//创建日志存放目录
[root@lizihan ~]# mkdir  -p /var/log/nginx/
[root@lizihan ~]# chown -R nginx.nginx /var/log/nginx/

//编译安装nginx
[root@lizihan ~]# tar -xf nginx-1.14.0
[root@lizihan ~]# cd nginx-1.14.0/
[root@lizihan nginx-1.14.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@lizihan nginx-1.14.0]# make && make install


//将路径写入环境变量
[root@lizihan ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@lizihan ~]# . /etc/profile.d/nginx.sh 

//安装后检查配置是否有错
[root@lizihan nginx-1.14.0]# 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


//启动nginx
[root@lizihan nginx-1.14.0]# nginx
[root@lizihan ~]# ss -antl
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      128          *:22                       *:*                  
LISTEN     0      100    127.0.0.1:25                       *:*                  
LISTEN     0      128         :::80                      :::*                  
LISTEN     0      128         :::22                      :::*                  
LISTEN     0      100        ::1:25                      :::*    
  • 172.16.11.20

安装mysql

//安装依赖包
[root@lizihan ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel

//创建mysql用户和组
[root@lizihan ~]# groupadd -r mysql
[root@lizihan ~]# useradd -r -M -s /sbin/nologin -g mysql mysql
[root@lizihan ~]# id mysql
uid=996(mysql) gid=994(mysql) groups=994(mysql)

//解压二进制包,并创建连接修改属主和属组
[root@lizihan ~]# tar -xf mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@lizihan ~]# cd /usr/local/
[root@lizihan local]# ln -s mysql-5.7.23-linux-glibc2.12-x86_64/ mysql
[root@lizihan local]# chown mysql.mysql mysql

//添加环境变量
[root@lizihan local]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@lizihan ~]# . /etc/profile.d/mysql.sh 
//创建存放数据的目录并修改属主
[root@lizihan local]# mkdir /opt/data 
[root@lizihan local]# chown mysql.mysql /opt/data

//初始化数据库
[root@lizihan local]# mysqld --initialize --user=mysql --datadir=/opt/data

//安装后配置
[root@lizihan local]# ln -s /usr/local/mysql/include/ /usr/local/include/mysql
[root@lizihan local]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.con

//生成配置文件
[root@lizihan local]# cat > /etc/my.cnf << EOF
> [mysqld]
> datadir=/opt/data
> basedir = /usr/local/mysql
> socket = /tmp/mysql.sock
> port = 3306
> pid-file = /opt/data/mysql.pid
> user = mysql 
> skip-name-resolve
> EOF

//配置服务启动脚本
[root@lizihan local]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@lizihan local]# vim /etc/init.d/mysqld 
basedir=/usr/local/mysql
datadir=/opt/data


//启动mysql
[root@lizihan local]# service mysqld start
Starting MySQL.Logging to '/opt/data/lizihan.err'.
 SUCCESS! 

//修改密码
mysql> set password=password('lzh123!');
Query OK, 0 rows affected, 1 warning (0.00 sec)
  • 172.16.11.18
    安装tomcat
    ①.安装开发环境
[root@lizihan ~]# yum -y install java-1.8.0-openjdk java-1.8.0-openjdk-devel

②.解压两次tomcat包来做负载均衡

[root@lizihan ~]# mkdir /usr/local/tomcat{1,2}
[root@lizihan ~]# tar -xf apache-tomcat-9.0.8.tar.gz -C /usr/local/tomcat1
[root@lizihan ~]# tar -xf apache-tomcat-9.0.8.tar.gz -C /usr/local/tomcat2
[root@lizihan ~]# ln -s /usr/local/tomcat1/apache-tomcat-9.0.8/ /usr/local/tomcat1/tomcat
[root@lizihan ~]# ln -s /usr/local/tomcat2/apache-tomcat-9.0.8/ /usr/local/tomcat2/tomcat

③.分别在两个tomcat里面写测试文件

[root@lizihan ~]# mkdir /usr/local/tomcat1/tomcat/webapps/test
[root@lizihan ~]# mkdir /usr/local/tomcat2/tomcat/webapps/test
[root@lizihan webapps]# vim /usr/local/tomcat1/tomcat/webapps/test/index.jsp
<html>
<head>
        <title>test page</title>
</head>
<body>
        <%
            out.println("Hellow World");
        %>
</body>
</html>
[root@lizihan webapps]# vim /usr/local/tomcat2/tomcat/webapps/test/index.jsp
<html>
<head>
        <title>test page</title>
</head>
<body>
        <%
            out.println("Hellow World too");
        %>
</body>
</html>


配置

  • 172.16.11.18

修改tomcat2的配置文件,启动服务

[root@lizihan ~]# vim /usr/local/tomcat2/tomcat/conf/server.xml 
<Server port="8006" shutdown="SHUTDOWN">       //都修改为跟tomcat1里面不一样的
<Connector port="8081" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8444" />
<Connector port="8019" protocol="AJP/1.3" redirectPort="8444" />
[root@lizihan ~]# /usr/local/tomcat1/tomcat/bin/catalina.sh start
[root@lizihan ~]# /usr/local/tomcat2/tomcat/bin/catalina.sh start
[root@lizihan ~]# ss -antl
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      128           *:22                        *:*                  
LISTEN     0      100    127.0.0.1:25                        *:*                  
LISTEN     0      100          :::8080                     :::*                  
LISTEN     0      100          :::8081                     :::*                  
LISTEN     0      100          :::8019                     :::*                  
LISTEN     0      128          :::22                       :::*                  
LISTEN     0      100         ::1:25                       :::*                  
LISTEN     0      1      ::ffff:127.0.0.1:8005                     :::*                  
LISTEN     0      1      ::ffff:127.0.0.1:8006                     :::*                  
LISTEN     0      100          :::8009                     :::*     
  • 172.16.11.19

修改配置文件,启动服务

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
    upstream web.com {                                    //配置在http段
        server 172.16.11.18:8080;
        server 172.16.11.18:8081;
    }
        location ~ .jsp$ {                              //添加反向代理的location
            proxy_pass http://web.com;
        }

[root@localhost ~]# nginx


测试

使用nginx服务器的ip访问静态页面
在这里插入图片描述

使用nginx服务器的ip通过反向代理访问动态页面
在这里插入图片描述

最后

以上就是健康世界为你收集整理的lnmt实现负载均衡和反向代理的全部内容,希望文章能够帮你解决lnmt实现负载均衡和反向代理所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(36)

评论列表共有 0 条评论

立即
投稿
返回
顶部