Tomcat multiple instances, load balancing, dynamic and static separation

Tomcat multi-instance deployment

Install jdk
[root@localhost ~]#systemctl stop firewalld.service
[root@localhost ~]#setenforce 0
[root@localhost ~]#cd /opt
[root@localhost opt]#ls
apache-tomcat-8.5.16.tar.gz jdk-8u91-linux-x64.tar.gz rh
[root@localhost opt]#tar xf jdk-8u91-linux-x64.tar.gz
[root@localhost opt]#ls
apache-tomcat-8.5.16.tar.gz jdk1.8.0_91 jdk-8u91-linux-x64.tar.gz rh
[root@localhost opt]#mv jdk
jdk1.8.0_91/ jdk-8u91-linux-x64.tar.gz
[root@localhost opt]#mv jdk1.8.0_91/ /usr/local/
[root@localhost opt]#ls /usr/local/
bin etc games include jdk1.8.0_91 lib lib64 libexec sbin share src
[root@localhost opt]#cd /etc/profile.d/ #Environment variable sub-configuration file
[root@localhost profile.d]#ls
256term.csh colorgrep.csh flatpak.sh less.sh vte.sh
256term.sh colorgrep.sh lang.csh PackageKit.sh which2.csh
abrt-console-notification.sh colorls.csh lang.sh vim.csh which2.sh
bash_completion.sh colorls.sh less.csh vim.sh
[root@localhost profile.d]#vim java.sh
export JAVA_HOME=/usr/local/jdk1.8.0_91
export JRE_HOME=$JAVA_HOME/jre
export CLASSPATH=.:$JAVA_HOME/lib:$JRE_HOME
export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH

[root@localhost profile.d]#source java.sh

Deploy Tomcat
[root@localhost profile.d]#cd /opt
[root@localhost opt]#ls
apache-tomcat-8.5.16.tar.gz jdk-8u91-linux-x64.tar.gz rh
[root@localhost opt]#tar xf apache-tomcat-8.5.16.tar.gz
[root@localhost opt]#ls
apache-tomcat-8.5.16 apache-tomcat-8.5.16.tar.gz jdk-8u91-linux-x64.tar.gz rh
[root@localhost opt]#cp -a apache-tomcat-8.5.16 /usr/local/tomcat1
[root@localhost opt]#cp -a apache-tomcat-8.5.16 /usr/local/tomcat2
[root@localhost opt]#ls /usr/local/
bin games jdk1.8.0_91 lib64 sbin src tomcat2
etc include lib libexec share tomcat1
[root@localhost opt]#vim /usr/local/tomcat2/conf/server.xml
[root@localhost opt]#cd /usr/local/tomcat2
[root@localhost tomcat2]#ls
bin conf lib LICENSE logs NOTICE RELEASE-NOTES RUNNING.txt temp webapps work
[root@localhost tomcat2]#cd bin
[root@localhost bin]#ls
bootstrap.jar configtest.bat setclasspath.sh tomcat-native.tar.gz
catalina.bat configtest.sh shutdown.bat tool-wrapper.bat
catalina.sh daemon.sh shutdown.sh tool-wrapper.sh
catalina-tasks.xml digest.bat startup.bat version.bat
commons-daemon.jar digest.sh startup.sh version.sh
commons-daemon-native.tar.gz setclasspath.bat tomcat-juli.jar

[root@localhost bin]#vim startup.sh
export CATALINA_BASE=/usr/local/tomcat2 #The directory location where Tomcat is stored
export CATALINA_HOME=/usr/local/tomcat2
export TOMCAT_HOME=/usr/local/tomcat2

[root@localhost bin]#vim shutdown.sh
export CATALINA_BASE=/usr/local/tomcat2
export CATALINA_HOME=/usr/local/tomcat2
export TOMCAT_HOME=/usr/local/tomcat2

[root@localhost bin]#cd /usr/local/tomcat1
[root@localhost tomcat1]#ls
bin conf lib LICENSE logs NOTICE RELEASE-NOTES RUNNING.txt temp webapps work
[root@localhost tomcat1]#cd bin
[root@localhost bin]#ls
bootstrap.jar configtest.bat setclasspath.sh tomcat-native.tar.gz
catalina.bat configtest.sh shutdown.bat tool-wrapper.bat
catalina.sh daemon.sh shutdown.sh tool-wrapper.sh
catalina-tasks.xml digest.bat startup.bat version.bat
commons-daemon.jar digest.sh startup.sh version.sh
commons-daemon-native.tar.gz setclasspath.bat tomcat-juli.jar
[root@localhost bin]#vim startup.sh
export CATALINA_BASE=/usr/local/tomcat1
export CATALINA_HOME=/usr/local/tomcat1
export TOMCAT_HOME=/usr/local/tomcat1
[root@localhost bin]#vim shutdown.sh
export CATALINA_BASE=/usr/local/tomcat1
export CATALINA_HOME=/usr/local/tomcat1
export TOMCAT_HOME=/usr/local/tomcat1

[root@localhost bin]#/usr/local/tomcat1/bin/startup.sh
Using CATALINA_BASE: /usr/local/tomcat1
Using CATALINA_HOME: /usr/local/tomcat1
Using CATALINA_TMPDIR: /usr/local/tomcat1/temp
Using JRE_HOME: /usr/local/jdk1.8.0_91/jre
Using CLASSPATH: /usr/local/tomcat1/bin/bootstrap.jar:/usr/local/tomcat1/bin/tomcat-juli.jar
Tomcat started.
[root@localhost bin]#/usr/local/tomcat2/bin/startup.sh
Using CATALINA_BASE: /usr/local/tomcat2
Using CATALINA_HOME: /usr/local/tomcat2
Using CATALINA_TMPDIR: /usr/local/tomcat2/temp
Using JRE_HOME: /usr/local/jdk1.8.0_91/jre
Using CLASSPATH: /usr/local/tomcat2/bin/bootstrap.jar:/usr/local/tomcat2/bin/tomcat-juli.jar
Tomcat started.

[root@localhost bin]#netstat -ltnp |grep java
tcp6 0 0 :::8080 :::* LISTEN 40828/java
tcp6 0 0 :::8081 :::* LISTEN 40885/java
tcp6 0 0 127.0.0.1:8005 :::* LISTEN 40828/java
tcp6 0 0 127.0.0.1:8006 :::* LISTEN 40885/java
tcp6 0 0 :::8009 :::* LISTEN 40828/java
tcp6 0 0 :::8010 :::* LISTEN 40885/java

Enter http://192.168.65.102:8080 on the web page
The screen can appear at http://192.168.65.102:8081

NGINX + Tomcat load balancing, dynamic and static separation

Deploy Tomcat page

Tomcat1
[root@localhost ~]#cd /usr/local/tomcat1/webapps/
[root@localhost webapps]#ls
docs examples host-manager manager ROOT
[root@localhost webapps]#mkdir mm
[root@localhost webapps]#cd mm
[root@localhost mm]#vim test.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>JSP tomcat1 page</title>
</head>
<body>
<% out.println("Dynamic page 1,Hello Tomcat1");%>
</body>
</html>


Tomcat2
[root@localhost mm]#cd /usr/local/tomcat2/webapps/
[root@localhost webapps]#mkdir mm
[root@localhost webapps]#cd mm
[root@localhost webapps]#vim test.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>JSP tomcat2 page</title>
</head>
<body>
<% out.println("Dynamic page 2,Hello Tomcat2");%>
</body>
</html>

[root@www webapps]#cd /usr/local/tomcat/webapps/
[root@www webapps]#ls
docs examples host-manager manager mm ROOT test1 test2
[root@www webapps]#cd mm
[root@www mm]#vim test.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>JSP tomcat3 page</title>
</head>
<body>
<% out.println("Dynamic page 2,Hello Tomcat3");%>
</body>
</html>

Configure NGINX service

yum install NGINX
[root@localhost opt]# cd /etc/yum.repos.d
[root@localhost yum.repos.d]# ls
CentOS-Base.repo CentOS-Debuginfo.repo CentOS-Media.repo CentOS-Vault.repo
CentOS-CR.repo CentOS-fasttrack.repo CentOS-Sources.repo

[root@localhost yum.repos.d]# mkdir repo.bak
[root@localhost yum.repos.d]# mv *.repo repo.bak
[root@localhost yum.repos.d]# ls
repo.bak
[root@localhost yum.repos.d]# vim nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[root@localhost yum.repos.d]# yum -y install nginx
  Verifying: 1:nginx-1.24.0-1.el7.ngx.x86_64 1/1
Installed:
  nginx.x86_64 1:1.24.0-1.el7.ngx
complete!

Deploy NGINX page
[root@localhost ~]# cd /usr/share/nginx/html/
[root@localhost html]#ls
50x.html index.html
[root@localhost html]# mkdir mm
[root@localhost html]# cd mm
[root@localhost html]# echo '<h1>this is nginx static web page!</h1>' >test.html
[root@localhost mm]# ls
test.html

Achieve dynamic and static separation

[root@localhost html]# cd /etc/nginx/
[root@localhost nginx]# vim nginx.conf
   Add on the next line #gzip on;
    upstream tomcat {<!-- -->
      server 192.168.65.101:8080 weight=1
      server 192.168.65.102:8080 weight=1
      server 192.168.65.102:8082 weight=1
    }

[root@localhost nginx]# cd conf.d/
[root@localhost conf.d]# ls
default.conf
[root@localhost conf.d]# vim default.conf
location ~* .*\.jsp$ {<!-- -->
       proxy_pass http://tomcat;
       proxy_set_header HOST $host;
       proxy_set_header X-Real-IP $remote_addr;
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

[root@localhost nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@localhost conf.d]# systemctl restart nginx

Load balancing

Copy nginx.repo of 192.168.65.104 host to 192.168.65.103
[root@localhost ~]# cd /etc/yum.repos.d
[root@localhost yum.repos.d]# ls
nginx.repo repo.bak
[root@localhost yum.repos.d]# scp nginx.repo 192.168.65.103:`pwd`
The authenticity of host '192.168.65.103 (192.168.65.103)' can't be established.
ECDSA key fingerprint is SHA256:63CrYDGmi + o9Wnfxyu + ceZXb8PaFjKl3Ob3EEWD50ag.
ECDSA key fingerprint is MD5:6e:96:3c:ab:13:8b:b9:d2:cd:34:80:46:3c:52:8e:bc.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.65.103' (ECDSA) to the list of known hosts.
[email protected]'s password:
nginx.repo 100% 178 134.9KB/s 00:00

192.168.65.103
[root@localhost ~]#cd /etc/yum.repos.d/
[root@localhost yum.repos.d]#ls
CentOS-Base.repo CentOS-fasttrack.repo CentOS-Vault.repo
CentOS-CR.repo CentOS-Media.repo epel.repo
CentOS-Debuginfo.repo CentOS-Sources.repo epel-testing.repo
[root@localhost yum.repos.d]#mkdir repo.bak
[root@localhost yum.repos.d]#mv *.repo repo.bak/
[root@localhost yum.repos.d]#ls
repo.bak
[root@localhost yum.repos.d]#ls
nginx.repo repo.bak
[root@localhost yum.repos.d]# yum -y install nginx
  Verifying: 1:nginx-1.24.0-1.el7.ngx.x86_64 1/1
Installed:
  nginx.x86_64 1:1.24.0-1.el7.ngx
complete!

104
[root@localhost yum.repos.d]# cd /etc/nginx/
[root@localhost nginx]# ls
conf.d fastcgi_params mime.types modules nginx.conf scgi_params uwsgi_params
[root@localhost nginx]# scp -r conf.d/ nginx.conf 192.168.65.103:`pwd`
[email protected]'s password:
default.conf 100% 1292 324.4KB/s 00:00
nginx.conf 100% 804 630.6KB/s 00:00
[root@localhost nginx]# cd /usr/share/nginx/html/
[root@localhost html]#ls
50x.html index.html mm
[root@localhost html]# vim mm/test.html
<h1>This is nginx static web1 page!</h1>
[root@localhost html]# scp -r mm/ 192.168.65.103:`pwd`
[email protected]'s password:
test.html 100% 40 12.1KB/s 00:00


103
[root@localhost yum.repos.d]# cd /usr/share/nginx/html/
[root@localhost html]#ls
50x.html index.html mm
[root@localhost html]# vim mm/test.html
<h1>This is nginx static web2 page!</h1>
[root@localhost html]# cd /etc/nginx/
[root@localhost nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@localhost nginx]# systemctl start nginx

Enter the following URL and the following image will appear
http://192.168.65.103/mm/test.html
http://192.168.65.103/mm/test.jsp

192.168.65.105
[root@localhost ~]# cd /opt
[root@localhost opt]# ls
nginx-1.18.0 .tar.gz rh
[root@localhost opt]# useradd -M -s /sbin/nologin nginx
[root@localhost opt]# tar xf nginx-1.18.0\ .tar.gz
[root@localhost opt]# ls
nginx-1.18.0 nginx-1.18.0 .tar.gz rh
[root@localhost opt]# cd nginx-1.18.0/
[root@localhost nginx-1.18.0]# ./configure --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-http_stub_status_module \
> --with-stream
[root@localhost nginx-1.18.0]# make -j2 & amp; & amp; make install
[root@localhost conf]# cd /usr/local/nginx/
[root@localhost nginx]# ls
conf html logs sbin
[root@localhost nginx]# cd conf/
[root@localhost conf]# ls
fastcgi.conf koi-utf nginx.conf uwsgi_params
fastcgi.conf.default koi-win nginx.conf.default uwsgi_params.default
fastcgi_params mime.types scgi_params win-utf
fastcgi_params.default mime.types.default scgi_params.default
[root@localhost conf]# vim nginx.conf
Add a line above http
stream {<!-- -->
    upstream nginx_server{<!-- -->
       server 192.168.65.103:80;
       server 192.168.65.104:80;
     }
     server {<!-- -->
        listen 80;
        proxy_pass nginx_server;
    }
}
Change the listening port 80 below to 8333
[root@localhost conf]# cd ..
[root@localhost nginx]# cd sbin/
[root@localhost sbin]# ./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@localhost sbin]# /usr/local/nginx/sbin/nginx #Start NGINX
[root@localhost sbin]# netstat -lnpt | grep nginx
tcp 0 0 0.0.0.0:8333 0.0.0.0:* LISTEN 5480/nginx: master
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 5480/nginx: master
[root@localhost sbin]# cd ..
[root@localhost nginx]# ls
client_body_temp conf fastcgi_temp html logs proxy_temp sbin scgi_temp uwsgi_temp
[root@localhost nginx]# cd conf
[root@localhost conf]# ls
fastcgi.conf koi-utf nginx.conf uwsgi_params
fastcgi.conf.default koi-win nginx.conf.default uwsgi_params.default
fastcgi_params mime.types scgi_params win-utf
fastcgi_params.default mime.types.default scgi_params.default
[root@localhost conf]# vim nginx.conf
keepalive_timeout 0; #Modify the long connection time to 0, 103 and 104 are also modified similarly
#keepalive_timeout 65;
[root@localhost conf]# /usr/local/nginx/sbin/nginx -s reload #Restart the service
105 Page not set, but other static and dynamic pages can be accessed