cd /opt
# 解压缩
tar zxvf apache-tomcat-9.0.16.tar.gz
# 创建tomcat目录
mkdir /usr/local/tomcat
# 移动解压后的包去刚才创建的目录下,并改名tomcat1
mv apache-tomcat-9.0.16 /usr/local/tomcat/tomcat1
# 复制tomcat1 为 tomcat2
cp -a /usr/local/tomcat/tomcat1 /usr/local/tomcat/tomcat2
#tomcat1
export CATALINA_HOME1=/usr/local/tomcat/tomcat1
export CATALINA_BASE1=/usr/local/tomcat/tomcat1
export TOMCAT_HOME1=/usr/local/tomcat/tomcat1
#tomcat2
export CATALINA_HOME2=/usr/local/tomcat/tomcat2
export CATALINA_BASE2=/usr/local/tomcat/tomcat2
export TOMCAT_HOME2=/usr/local/tomcat/tomcat2
tomcat2和1不能有重复的端口号,这里直接+1
# 第22行,默认为8005修改为8006
<Server port="8006" shutdown="SHUTDOWN">
# 第69行,默认为8080修改为8081
<Connector port="8081" protocol="HTTP/1.1"
# 第116行,默认为8009修改为8010
<Connector port="8010" protocol="AJP/1.3" redirectPort="8443" />
开始
# 第20行下添加
export CATALINA_BASE=$CATALINA_BASE1
export CATALINA_HOME=$CATALINA_HOME1
export TOMCAT_HOME=$TOMCAT_HOME1
停止
第20行下添加
export CATALINA_BASE=$CATALINA_BASE1
export CATALINA_HOME=$CATALINA_HOME1
export TOMCAT_HOME=$TOMCAT_HOME1
开始
# 第20行下添加
export CATALINA_BASE=$CATALINA_BASE2
export CATALINA_HOME=$CATALINA_HOME2
export TOMCAT_HOME=$TOMCAT_HOME2
停止
第20行下添加
export CATALINA_BASE=$CATALINA_BASE2
export CATALINA_HOME=$CATALINA_HOME2
export TOMCAT_HOME=$TOMCAT_HOME2
地址设置:192.168.253.22:80
安装方法
# 关闭防火墙
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
# 解压
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 \
--with-http_ssl_module
# 安装
make -j2 && 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
配置方法
# 关闭防火墙
systemctl stop firewalld
setenforce 0
# 解压缩至 /usr/local 目录
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
# 压缩内容移动到/usr/local目录下并改名tomcat
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
# 创建目录
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 test1 page</title>
</head>
<body>
<% out.println("动态页面 1-hello tomcat1");%> # 网页内容
</body>
</html>
# 进入主配置文件修改
vim /usr/local/tomcat/conf/server.xml
# 替换原host配置
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
<Context docBase="/usr/local/tomcat/webapps/test" path="" reloadable="true">
</Context>
</Host>
# 重启服务
/usr/local/tomcat/bin/shutdown.sh
/usr/local/tomcat/bin/startup.sh
实例1
mkdir /usr/local/tomcat/tomcat1/webapps/test
mkdir /usr/local/tomcat/tomcat2/webapps/test
# 新建动态网页文件
vim /usr/local/tomcat/tomcat1/webapps/test/index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>JSP test2 page</title>
</head>
<body>
<% out.println("动态页面 2-hello tomcat2");%>
</body>
</html>
# 修改主配置文件
vim /usr/local/tomcat/tomcat1/conf/server.xml
# 和tomcat1 一样设置
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
<Context docBase="/usr/local/tomcat/tomcat1/webapps/test" path="" reloadable="true" />
</Host>
# 重启服务
/usr/local/tomcat/tomcat1/bin/shutdown.sh
/usr/local/tomcat/tomcat1/bin/startup.sh
实例2
# 设置动态网页内容
vim /usr/local/tomcat/tomcat2/webapps/test/index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>JSP test3 page</title>
</head>
<body>
<% out.println("动态页面 3-hello tomcat3");%>
</body>
</html>
# 修改主配置文件
vim /usr/local/tomcat/tomcat2/conf/server.xml
# 和实例1设置一样
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
<Context docBase="/usr/local/tomcat/tomcat2/webapps/test" path="" reloadable="true" />
</Host>
# 重启服务
/usr/local/tomcat/tomcat2/bin/shutdown.sh
/usr/local/tomcat/tomcat2/bin/startup.sh
# 准备静态页面和静态图片
echo '闭嘴
' > /usr/local/nginx/html/index.html
# 创建img目录,放置网页图片
mkdir /usr/local/nginx/html/img
# 进入主配置文件
vim /usr/local/nginx/conf/nginx.conf
http {
#gzip on;
upstream tomcat_server {
server 192.168.253.22:8080 weight=1;
server 192.168.253.11:8080 weight=1;
server 192.168.253.11:8081 weight=1;
}
server {
listen 80;
server_name www.cc.com;
charset utf-8; # 取消注释
#access_log logs/host.access.log main;
# 配置Nginx处理动态页面请求
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;
}
# 配置Nginx处理静态图片请求
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|css)$ {
root /usr/local/nginx/html/img;
expires 10d;
}
location / {
root html;
index index.html index.htm;
}
rr 负载均衡模式
least_conn 最少连接
ip_hash 负载均衡模式
fair(第三方)负载均衡模式
url_hash(第三方)负载均衡模式