目录
1 环境变量的配置
1.1 jdk环境变量设置
①分别登录三台主机(我们这里是建立了一个centos7的一个集群)
cd /opt/software # 进入到软件安装包路径
wget ftp://172.18.40.149/pub/jdk-8u212-linux-x64.tar.gz # 下载jdk1.8压缩包
tar -zxvf jdk-8u212-linux-x64.tar.gz -C /opt/module # 解压至/opt/module路径下cd /etc/profile.d/
touch my_env.sh # 创建一个环境变量文件②文件编辑好后加载新的配置文件
source /etc/profile
java -version # 任意路径验证jdk1.8是否安装正常
#java version "1.8.0_212"
#Java(TM) SE Runtime Environment (build 1.8.0_212-b10)
#Java HotSpot(TM) 64-Bit Server VM (build 25.212-b10, mixed mode)
2 公网主机安装Nginx
# 公网主机安装nginx
yum install epel-release -y
yum install nginx -y
# 启动nginx
systemctl start nginx
# 设置为开机启动
systemctl enable nginx
# 删除nginx
yum remove -y nginx
# 关闭防火墙
systemctl stop fiewalld
# 设置开机不启动防火墙
systemctl disable fiewalld
# 测试nginx
# 浏览器访问:http://公网主机IP
# 查看nginx配置文件
vim /etc/nginx/nginx.conf
# 记录默认root地址
cd /usr/share/nginx/
# 备份原数据
mv html html.bak
# 创建新的文件夹
mkdir html
cd html
# 下载测试页面
wget ftp://172.18.40.149/pub/html/html/*
cd images
wget ftp://172.18.40.149/pub/html/html/images/
# nginx热加载(相当于刷新)
nginx -s reload
# 浏览器访问:http://公网主机IP,查看内容是否更新
3、三台主机安装Tomcat
# 关闭selinux
setenforce 0
# 修改selinux配置文件,关闭selinux
sed -i "s/^SELINUX=.*$/SELINUX=disabled/" /etc/selinux/config
# 关闭防火墙
systemctl stop firewalld
# 设置开机不启动防火墙
systemctl disable firewalld
# 下载阿里云的epel.repo文件
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
# 更新下载源
yum clean all
yum makecache
# java -version 验证
# 登陆公网主机,下载tomcat
yum install -y tomcat tomcat-webapps tomcat-admin-webapps
# tomcat-docs-webapp tomcat-javadoc # 可选装文档
# 删除tomcat
# yum remove -y tomcat tomcat-webapps tomcat-admin-webapps
# 启动tomcat
systemctl start tomcat
# 设置开机启动
systemctl enable tomcat
# 查看tomcat状态
systemctl status tomcat
# 本机验证tomcat
curl http://localhost:8080
# curl http://127.0.0.1:8080
# curl http://公网IP:8080
# curl http://公网主机名称:8080
# 内网1主机、内网2主机同理安装tomcat
4、Nginx服务器反向代理(Reverse Proxy)
# 公网主机设置反向代理
vim /etc/nginx/conf.d/tomcat_proxy.conf
################################################
server {
listen 8081;
server_name xuehao;location / {
proxy_pass http://xuehao:8080/;
}
·
location /node1 {
proxy_pass http://xuehao1:8080/;
}location /node2 {
proxy_pass http://xuehao2:8080/;
}
}
#################################################
# 注:可修改/var/lib/tomcat/webapps/ROOT/index.jsp做标记
# 修改<h1>${pageContext.servletContext.serverInfo} (主机名称)</h1># 热加载
nginx -s reload# 浏览器测试:
http://公网IP:8081 # 代理本机:8080端口
http://公网IP:8081/node1 # 代理内网1:8080端口
http://公网IP:8081/node2 # 代理内网2:8080端口
![]()
![]()
5、Nginx服务器负载均衡(Load Balance)
# 公网主机设置反向代理
vim /etc/nginx/conf.d/tomcat_lb.conf
################################################
server {
listen 8082;
server_name xuehao;location / {
proxy_pass http://tomcatservers;
}
}
upstream tomcatservers {
server xuehao:8080;
server xuehao1:8080;
server xuehao2:8080;
}
#################################################
# 注:可修改/var/lib/tomcat/webapps/ROOT/index.jsp做标记
# 修改<h1>${pageContext.servletContext.serverInfo} (主机名称)</h1># 热加载
nginx -s reload# 浏览器测试:
http://公网IP:8082 # 每次点刷新查看界面变化
# 修改tomcat_lb.conf中某一主机权重,观察负载均衡效果(谁的权重越高,谁就访问时出现次数最多)
upstream tomcatservers {
server xuehao:8080;
server xuehao1:8080 weight=5;
server xuehao2:8080;
}
![]()