tomcat部署简述以及与nginx实现负载均衡
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了tomcat部署简述以及与nginx实现负载均衡相关的知识,希望对你有一定的参考价值。
1.安装jdk环境
[[email protected] ~]# yum -y install java-1.8.0-openjdk java-1.8.0-openjdk-devel
//查看安装的版本
[[email protected] ~]# java -version
openjdk version "1.8.0_181"
OpenJDK Runtime Environment (build 1.8.0_181-b13)
OpenJDK 64-Bit Server VM (build 25.181-b13, mixed mode)
//下载好tomcat包
[[email protected] ~]# cd /usr/src/
[[email protected] src]# ls
apache-tomcat-9.0.11.tar.gz debug kernels
[[email protected] src]# tar xf apache-tomcat-9.0.11.tar.gz
[[email protected] src]# ls
apache-tomcat-9.0.11 apache-tomcat-9.0.11.tar.gz debug kernels
//创建tomcat存放目录
[[email protected] src]# cd /usr/local/
[[email protected] local]# mkdir tomacat{1,2}
[[email protected] local]# ls
bin etc games include lib lib64 libexec sbin share src tomacat1 tomacat2
//将tomcat解压到存放目录
[[email protected] ~]# cd /usr/src
[[email protected] src]# ls
apache-tomcat-9.0.11.tar.gz debug kernels
[[email protected] src]# tar xf apache-tomcat-9.0.11.tar.gz -C /usr/local/tomacat1
[[email protected] src]# tar xf apache-tomcat-9.0.11.tar.gz -C /usr/local/tomacat2
//创建软链接
[[email protected] ~]# cd /usr/local/tomacat1
[[email protected] tomacat1]# ln -s apache-tomcat-9.0.11/ tomcat1
[[email protected] tomacat1]# ll
total 0
drwxr-xr-x. 9 root root 220 Sep 5 11:31 apache-tomcat-9.0.11
lrwxrwxrwx. 1 root root 21 Sep 5 11:44 tomcat1 -> apache-tomcat-9.0.11/
[[email protected] tomacat1]# cd ../tomacat2
[[email protected] tomacat2]# ls
apache-tomcat-9.0.11
[[email protected] tomacat2]# ln -s apache-tomcat-9.0.11/ tomcat2
[[email protected] tomacat2]# ll
total 0
drwxr-xr-x. 9 root root 220 Sep 5 11:31 apache-tomcat-9.0.11
lrwxrwxrwx. 1 root root 21 Sep 5 11:45 tomcat2 -> apache-tomcat-9.0.11/
//创建测试目录并创建测试文件
[[email protected] ~]# mkdir /usr/local/tomacat1/tomcat1/webapps/test
[[email protected] ~]# mkdir /usr/local/tomacat2/tomcat2/webapps/test
[[email protected] ~]# cd /usr/local/tomacat1/tomcat1/webapps/test
[[email protected] test]# vi index.jsp
[[email protected] test]# cat index.jsp
<html>
<head>
<title>test page</title>
</head>
<body>
<%
out.println("i hope not so far");
%>
</body>
</html>
[[email protected] test]# vi index.jsp
[[email protected] test]# cat index.jsp
<html>
<head>
<title>test page</title>
</head>
<body>
<%
out.println("please just don‘t let me die !");
%>
</body>
</html>
//修改tomcat2的端口,否则端口起冲突。配置文件为:server.xml
[[email protected] ~]# cd /usr/local/tomacat2/tomcat2/
[[email protected] tomcat2]# ls
bin conf lib logs README.md RUNNING.txt webapps
BUILDING.txt CONTRIBUTING.md LICENSE NOTICE RELEASE-NOTES temp work
[[email protected] tomcat2]# cd conf/
[[email protected] conf]# ls
catalina.policy jaspic-providers.xml server.xml web.xml
catalina.properties jaspic-providers.xsd tomcat-users.xml
context.xml logging.properties tomcat-users.xsd
[[email protected] conf]# vi server.xml
//启动tomcat
[[email protected] ~]# /usr/local//tomacat1/tomcat1/bin/catalina.sh start
Using CATALINA_BASE: /usr/local/tomacat1/tomcat1
Using CATALINA_HOME: /usr/local/tomacat1/tomcat1
Using CATALINA_TMPDIR: /usr/local/tomacat1/tomcat1/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/tomacat1/tomcat1/bin/bootstrap.jar:/usr/local/tomacat1/tomcat1/bin/tomcat-juli.jar
Tomcat started.
[[email protected] ~]# /usr/local//tomacat2/tomcat2/bin/catalina.sh start
Using CATALINA_BASE: /usr/local/tomacat2/tomcat2
Using CATALINA_HOME: /usr/local/tomacat2/tomcat2
Using CATALINA_TMPDIR: /usr/local/tomacat2/tomcat2/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/tomacat2/tomcat2/bin/bootstrap.jar:/usr/local/tomacat2/tomcat2/bin/tomcat-juli.jar
Tomcat started.
//关闭防火墙
[[email protected] ~]# systemctl stop firewalld
[[email protected] ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[[email protected] ~]# setenforce 0
//输入192.168.56.12:8080 访问
//输入192.168.56.12:8080/test 访问
//第二台tomcat
//输入192.168.56.12:8090访问
//输入192.168.56.12:8090/test访问
//在主机192.168.56.11上搭建nginx
使用nginx实现负载均衡,修改配置文件即可。
[[email protected] ~]# vi /usr/local/nginx/conf/nginx.conf
upstream tomcat {
server 192.168.56.12:8080;
server 192.168.56.12:8090;
}
定义好upstream后,需要在server段内添加如下内容:
location / {
proxy_pass http://tomcat;
}
以上两段内容中的tomcat名可以自定义,但需要做到见名知意,并且两者要对应一样。
修改完配置文件检查语法错误,并重启服务
[[email protected] ~]# 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
[[email protected] ~]# nginx -s reload
//输入192.168.56.11访问
由于默认访问的是webapps目录下ROOT目录下的 index.jsp文件,所以会出现下面界面。
[[email protected] ~]# ls /usr/local/tomacat1/tomcat1/webapps/
docs examples host-manager manager ROOT
输入192.168.56.11/test则可实现效果
刷新访问界面
也可以修改ROOT目录下的index.jsp文件
[[email protected] ~]# cd /usr/local/tomacat1/tomcat1/webapps/ROOT/
[[email protected] ROOT]# mv index.jsp index.bak
[[email protected] ROOT]# vim index.jsp
[[email protected] ROOT]# cat index.jsp
<html>
<head>
<title>test page</title>
</head>
<body>
<%
out.println("i hope not so far");
%>
</body>
</html>
[[email protected] ~]# cd /usr/local/tomacat2/tomcat2/webapps/ROOT/
[[email protected] ROOT]# mv index.jsp index.bak
[[email protected] ROOT]# vim index.jsp
[[email protected] ROOT]# cat index.jsp
<html>
<head>
<title>test page</title>
</head>
<body>
<%
out.println("please just don‘t let me die !");
%>
</body>
</html>
输入ip192.168.56.11访问
刷新下访问界面:
以上是关于tomcat部署简述以及与nginx实现负载均衡的主要内容,如果未能解决你的问题,请参考以下文章