1.安装 jdk
关闭防火墙,将安装 Tomcat 所需软件包传到/opt目录下
jdk-8u201-linux-x64.rpm
apache-tomcat-9.0.16.tar.gz
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
安装JDK
cd /opt
rpm -qpl jdk-8u201-linux-x64.rpm
rpm -ivh jdk-8u201-linux-x64.rpm
java -version
设置JDK环境变量
vim /etc/profile.d/java.sh
export JAVA_HOME=/usr/java/jdk1.8.0_201-amd64
export CLASSPATH=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar
export PATH=$JAVA_HOME/bin:$PATH
source /etc/profile.d/java.sh
java -version
2.安装 tomcat
cd /opt
tar zxvf apache-tomcat-9.0.16.tar.gz
mkdir /usr/local/tomcat
mv apache-tomcat-9.0.16 /usr/local/tomcat
cp -a /usr/local/tomcat/ /usr/local/tomcat
3.配置 tomcat 环境变量
vim /etc/profile.d/tomcat.sh
#tomcat1
export CATALINA_HOME1=/usr/local/tomcat1
export CATALINA_BASE1=/usr/local/tomcat1
export TOMCAT_HOME1=/usr/local/tomcat1
#tomcat2
export CATALINA_HOME2=/usr/local/tomcat
export CATALINA_BASE2=/usr/local/tomcat
export TOMCAT_HOME2=/usr/local/tomcat
source /etc/profile.d/tomcat.sh
4.修改 tomcat1 中的 server.xml 文件,要求各 tomcat 实例配置不能有重复的端口号
vim /usr/local/tomcat/conf/server.xml
<Server port="8006" shutdown="SHUTDOWN"> #22行,修改Server prot,默认为8005 -> 修改为8006
修改为8081
<Connector port="8010" protocol="AJP/1.3" redirectPort="8443" /> #116行,修改Connector port AJP/1.3,默认为8009 -> 修改为8010
5.修改各 tomcat 实例中的 startup.sh 和 shutdown.sh 文件,添加 tomcat 环境变量
vim /usr/local/tomcat/bin/startup.sh
#Start Script for the CATALINA Server
export CATALINA_BASE=$CATALINA_BASE
export CATALINA_HOME=$CATALINA_HOME
export TOMCAT_HOME=$TOMCAT_HOME
vim /usr/local/tomcat/bin/shutdown.sh
#Stop script for the CATALINA Server
export CATALINA_BASE=$CATALINA_BASE
export CATALINA_HOME=$CATALINA_HOME
export TOMCAT_HOME=$TOMCAT_HOME
vim /usr/local/tomcat1/bin/startup.sh
#Start Script for the CATALINA Server
export CATALINA_BASE=$CATALINA_BASE1
export CATALINA_HOME=$CATALINA_HOME1
export TOMCAT_HOME=$TOMCAT_HOME1
vim /usr/local/tomcat1/bin/shutdown.sh
#Stop script for the CATALINA Server
export CATALINA_BASE=$CATALINA_BASE1
export CATALINA_HOME=$CATALINA_HOME1
export TOMCAT_HOME=$TOMCAT_HOME1
6.启动各 tomcat 中的 /bin/startup.sh
/usr/local/tomcat1/bin/startup.sh
/usr/local/tomcat/bin/startup.sh
7.浏览器访问测试
http://192.168.226.50:8080
http://192.168.226.50:8081
1.部署Nginx 负载均衡器
关闭防火墙,核心防护
systemctl stop firewalld
setenforce 0
yum -y install pcre-devel zlib-devel openssl-devel gcc gcc-c++ make
创建nginx用户,便于管理服务
useradd -M -s /sbin/nologin nginx
解压安装nginx
cd /opt
tar zxvf nginx-1.12.0.tar.gz -C /opt/
cd nginx-1.12.0/
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-file-aio --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module
make && make install
优化路径
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
vim /lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecrReload=/bin/kill -s HUP $MAINPID
ExecrStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
chmod 754 /lib/systemd/system/nginx.service
systemctl start nginx.service
systemctl enable nginx.service
2.部署2台Tomcat 应用服务器
systemctl stop firewalld
setenforce 0
tar zxvf jdk-8u91-linux-x64.tar.gz -C /usr/local/
vim /etc/profile
export JAVA_HOME=/usr/local/jdk1.8.0_91
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH
source /etc/profile
tar zxvf apache-tomcat-8.5.16.tar.gz
mv /opt/apache-tomcat-8.5.16/ /usr/local/tomcat
/usr/local/tomcat/bin/shutdown.sh
/usr/local/tomcat/bin/startup.sh
netstat -ntap | grep 8080
部署两台一样的。
3.动静分离配置
1、Tomcat1 server 配置
mkdir /usr/local/tomcat/webapps/test
vim /usr/local/tomcat/webapps/test/index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>JSP test2 pagetitle>
head>
<body>
<% out.println("动态页面 1,http://www.test1.com");%>
body>
html>
vim /usr/local/tomcat/conf/server.xml
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
<Context docBase="/usr/local/tomcat/webapps/test" path="" reloadable="true" />
Host>
启动服务
/usr/local/tomcat/bin/shutdown.sh
/usr/local/tomcat/bin/startup.sh
优化启动路径
ln -s /usr/local/tomcat/bin/* /usr/local/bin
2、Tomcat2 server 配置
mkdir /usr/local/tomcat/webapps/test
vim /usr/local/tomcat/webapps/test/index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>JSP test2 pagetitle>
head>
<body>
<% out.println("动态页面 2,http://www.test2.com");%>
body>
html>
vim /usr/local/tomcat/conf/server.xml
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
<Context docBase="/usr/local/tomcat/webapps/test" path="" reloadable="true" />
Host>
/usr/local/tomcat/bin/shutdown.sh
/usr/local/tomcat/bin/startup.sh
优化启动路径
ln -s /usr/local/tomcat/bin/* /usr/local/bin
3、Nginx server 配置
#准备静态页面和静态图片
echo '<html><body><h1>这是静态页面h1>body>html>' > /usr/local/nginx/html/index.html
mkdir /usr/local/nginx/html/img
在/usr/local/nginx/html/img下放入一张图片
vim /usr/local/nginx/conf/nginx.conf
http {
upstream tomcat_server {
server 192.168.80.100:8080 weight=1;
server 192.168.80.101:8080 weight=1;
}
server {
listen 80;
server_name www.kgc.com;
charset utf-8;
#access_log logs/host.access.log main;
location ~ .*.jsp$ {
proxy_pass http://tomcat_server;
proxy_set_header HOST $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|css)$ {
root /usr/local/nginx/html/img;
expires 10d;
}
location / {
root html;
index index.html index.htm;
}
}
}
访问192.168.226.70
访问192.168.226.70/game.jpg
访问192.168.226.70/index.jsp,并多次刷新
遇到在配置完Tomcat后无法正常启动问题
注意将
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
<Context docBase="/usr/local/tomcat/webapps/test" path="" reloadable="true" />
Host>
写入 /usr/local/tomcat/conf/server.xml文件后,将文件自带的Host配置删除或是改个名字,不然两个host配置会起到冲突效果。
再重新启动服务后,即可正常运行。
实验所需 链接:https://pan.baidu.com/s/1XXQtZMs52EqVgwOadezrYQ
提取码:jmik