码迷,mamicode.com
首页 > 其他好文 > 详细

tomcat部署简述以及与nginx实现负载均衡

时间:2018-09-05 17:31:39      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:负载   users   manager   term   版本   class   ble   tst   not   

1.安装jdk环境
[root@hyj ~]# yum -y install java-1.8.0-openjdk java-1.8.0-openjdk-devel
//查看安装的版本
[root@hyj ~]# 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包
[root@hyj ~]# cd /usr/src/
[root@hyj src]# ls
apache-tomcat-9.0.11.tar.gz debug kernels
[root@hyj src]# tar xf apache-tomcat-9.0.11.tar.gz
[root@hyj src]# ls
apache-tomcat-9.0.11 apache-tomcat-9.0.11.tar.gz debug kernels
//创建tomcat存放目录
[root@hyj src]# cd /usr/local/
[root@hyj local]# mkdir tomacat{1,2}
[root@hyj local]# ls
bin etc games include lib lib64 libexec sbin share src tomacat1 tomacat2
//将tomcat解压到存放目录
[root@hyj ~]# cd /usr/src
[root@hyj src]# ls
apache-tomcat-9.0.11.tar.gz debug kernels
[root@hyj src]# tar xf apache-tomcat-9.0.11.tar.gz -C /usr/local/tomacat1
[root@hyj src]# tar xf apache-tomcat-9.0.11.tar.gz -C /usr/local/tomacat2
//创建软链接
[root@hyj ~]# cd /usr/local/tomacat1
[root@hyj tomacat1]# ln -s apache-tomcat-9.0.11/ tomcat1
[root@hyj 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/
[root@hyj tomacat1]# cd ../tomacat2
[root@hyj tomacat2]# ls
apache-tomcat-9.0.11
[root@hyj tomacat2]# ln -s apache-tomcat-9.0.11/ tomcat2
[root@hyj 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/
//创建测试目录并创建测试文件
[root@hyj ~]# mkdir /usr/local/tomacat1/tomcat1/webapps/test
[root@hyj ~]# mkdir /usr/local/tomacat2/tomcat2/webapps/test
[root@hyj ~]# cd /usr/local/tomacat1/tomcat1/webapps/test
[root@hyj test]# vi index.jsp
[root@hyj test]# cat index.jsp
<html>
<head>
<title>test page</title>
</head>
<body>
<%
out.println("i hope not so far");
%>
</body>
</html>

[root@hyj test]# vi index.jsp
[root@hyj 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
[root@hyj ~]# cd /usr/local/tomacat2/tomcat2/
[root@hyj tomcat2]# ls
bin conf lib logs README.md RUNNING.txt webapps
BUILDING.txt CONTRIBUTING.md LICENSE NOTICE RELEASE-NOTES temp work

[root@hyj tomcat2]# cd conf/
[root@hyj 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
[root@hyj conf]# vi server.xml

//启动tomcat
[root@hyj ~]# /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.

[root@hyj ~]# /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.

//关闭防火墙
[root@hyj ~]# systemctl stop firewalld
[root@hyj ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@hyj ~]# 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实现负载均衡,修改配置文件即可。
[root@heyuanjie ~]# 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名可以自定义,但需要做到见名知意,并且两者要对应一样。
修改完配置文件检查语法错误,并重启服务
[root@heyuanjie ~]# 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
[root@heyuanjie ~]# nginx -s reload
//输入192.168.56.11访问
由于默认访问的是webapps目录下ROOT目录下的 index.jsp文件,所以会出现下面界面。
[root@hyj ~]# ls /usr/local/tomacat1/tomcat1/webapps/
docs examples host-manager manager ROOT

技术分享图片

输入192.168.56.11/test则可实现效果技术分享图片

刷新访问界面

技术分享图片

也可以修改ROOT目录下的index.jsp文件
[root@hyj ~]# cd /usr/local/tomacat1/tomcat1/webapps/ROOT/
[root@hyj ROOT]# mv index.jsp index.bak
[root@hyj ROOT]# vim index.jsp
[root@hyj ROOT]# cat index.jsp
<html>
<head>
<title>test page</title>
</head>
<body>
<%
out.println("i hope not so far");
%>
</body>
</html>

[root@hyj ~]# cd /usr/local/tomacat2/tomcat2/webapps/ROOT/
[root@hyj ROOT]# mv index.jsp index.bak
[root@hyj ROOT]# vim index.jsp
[root@hyj 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实现负载均衡

标签:负载   users   manager   term   版本   class   ble   tst   not   

原文地址:http://blog.51cto.com/13729085/2170748

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!