1. 程式人生 > >tomcat部署簡述以及與nginx實現負載均衡

tomcat部署簡述以及與nginx實現負載均衡

index.jsp bbb tom prop mark 修改 安裝 htm mode

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實現負載均衡