In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Foreword:
Tomcat server is a free open source web application server, which is a lightweight application server. It is widely used in small and medium-sized systems and not many concurrent users. It is the first choice for developing and testing JSP programs. Generally speaking, although Tomcat is the same as apache or Nginx web servers, it has the function of dealing with HTML pages, but its ability to deal with static pages is far less than apache or Nginx, so Tomcat generally runs on the back end as a servlet and JSP container.
For descriptions of the various directories and configuration files of the Tomcat service, please refer to the blog post: Tomcat server configuration file for details, or visit its Tomcat official website to read the official documentation.
Blog outline:
First, deploy Tomcat
Define multiple instances of Tomcat
Configure Tomcat custom directory
4. Enable Tomcat directory traversal
Use Tomcat to monitor the use of JVM virtual machine resources in real time
6. Enable JMX remote debugging function
7. Open the mangent management page
8. Open the host-mangent management page
IX. Cutting the running log of Tomcat
Define the page returned by Tomcat 404 error
11. Memory optimization of Tomcat service
12. Optimization of Tomcat thread pool
XIII. Modify the request method of Tomcat service
First, deploy Tomcat
Environmental preparation:
Centos 7, IP: 192.168.20.5 download the required source code package I provided, and upload to the centos 7 server.
Note: in the download link above, there is a file named "tomcat", which is the control script of the tomcat service. You can upload it to the / etc/init.d/ directory of the Tomcat server, and then use the chmod command to give it execution permission. You can use this script to start, stop, restart and other operations on the Tomcat service.
1 、 Start deploying [root@tomcat ~] # rpm-qa | grep jdk # query the default JAVA package java-1.8.0-openjdk-1.8.0.102-4.b14.el7.x86_64java-1.8.0-openjdk-headless-1.8.0.102-4.b14.el7.x86_64java-1.7.0-openjdk-1.7.0.111-2.6.7.8.el7.x86copyright 64copyjdklyconfigs- 1.2-1.el7.noarchjava-1.7.0-openjdk-headless-1.7.0.111-2.6.7.8.el7.x86_64 [root@tomcat ~] # rpm-e java-1.8.0-openjdk-headless-- nodeps # Uninstall Java1.8 [root@tomcat ~] # rpm-e java-1.7.0-openjdk-headless--nodeps # Uninstall Java1.7 [root@tomcat ~] # cd / usr/ Src [root@tomcat src] # rz # upload the two source code packages provided by me [root@tomcat src] # ls # source code package as follows: apache-tomcat-8.5.35.tar.gz jdk-8u211-linux-x64.tar.gz [root@tomcat ~] # tar zxf jdk-8u211-linux-x64.tar.gz-C / usr/local/ # decompress JDK to the specified path [root@tomcat src] # tar zxf apache-tomcat-8.5 .35.tar.gz # extract the Tomcat source package [root@tomcat src] # mv apache-tomcat-8.5.35 / usr/local/tomcat # move the extracted directory to the specified directory and name it tomcat [root@tomcat ~] # vim / etc/profile # Edit environment variables Add the following content export JAVA_HOME=/usr/local/jdk1.8.0_211export JRE_HOME=/usr/local/jdk1.8.0_211/jreexport CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jarexport PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH [root@tomcat ~] # at the end of the configuration file. / etc/profile # reload the environment variable [root@tomcat ~] # java-version # View java version java version "1.8.0211" Java (TM) SE Runtime Environment (build 1.8.0_211-b12) Java HotSpot (TM) 64-Bit Server VM (build 25.211-b12 Mixed mode) [root@tomcat ~] # / usr/local/tomcat/bin/startup.sh # start the Tomcat service Using CATALINA_BASE: / usr/local/tomcatUsing CATALINA_HOME: / usr/local/tomcatUsing CATALINA_TMPDIR: / usr/local/tomcat/tempUsing JRE_HOME: / usr/local/jdk1.8.0_211/jreUsing CLASSPATH: / usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/ Bin/tomcat-juli.jarTomcat started. [root@tomcat src] # netstat-anput | grep 8080 # make sure port 8080 is listening
At this point, the Tomcat service is running normally. You can use the client to access the Tomcat server IP+8080 port, and you can see the following page:
Define multiple instances of Tomcat
The so-called multi-instance is running multiple Tomcat services on a single server, so it is important to note that when defining multiple instances of Tomcat, port conflicts must not occur.
[root@tomcat src] # cd / usr/local/ # change to the installation directory of the Tomcat service [root@tomcat local] # cp-r tomcat tomcat2 # and make a copy of the first Tomcat directory and rename it tomcat2. [root@tomcat local] # vim tomcat2/conf/server.xml # modify all port numbers in the master configuration file to prevent port conflicts # all port numbers in the configuration file, except those with comments, need to be modified, # as long as the configuration item is marked with "port" and followed by a number, it is the port number # because it is scattered on each line, it is not written here, you can modify it yourself. Here I use its + 1 [root@tomcat local] # cd [root@tomcat ~] # / usr/local/tomcat2/bin/startup.sh # to run Tomcat2 [root@tomcat ~] # netstat-natp using the Tomcat2 startup script | grep 8081 # make sure the modified port is listening
At this point, the second Tomcat instance is running normally. You can use the client to access the Tomcat server IP+8081 port, and you can see the following page (the page you see is the same as the Tomcat directory before the full replication):
3. Configure Tomcat custom directory [root@tomcat ~] # mkdir-p / data/project # create directory [root@tomcat ~] # cat/ data/project/index1.jsp # first web page file aaaaaaaaaaaaaaaaaaaaaaa [root@tomcat ~] # cat/ data/project/index2.jsp # second web page file bbbbbbbbbbbbbbbbbbbbbbbbbbb [root@tomcat ~] # vim / usr/local/tomcat/conf/server.xml # Edit Tomcat main configuration file .. # add the following # after adding the configuration above, you can save the exit # another way to write the tag above is # the configuration added above is explained as follows: # Path: indicates the path of client access # docBase: specify the storage path of the web page; # Debug: specify that the debug level is 0, with the least output information, and 9 provide the most information; # reloadable: true means that when the web.xml file is changed, it is automatically loaded without restarting the service; # CorssContext: when true, different context share a session (session pool). # restart access to http://192.168.20.5:8080/shop/index1/2.jsp to verify [root@tomcat ~] # / usr/local/tomcat/bin/shutdown.sh # stop Tomcat [root@tomcat ~] # / usr/local/tomcat/bin/startup.sh # start Tomcat
Visit 192.168.20.5:8080/shop/index1.jsp to verify:
Visit 192.168.20.5:8080/shop/index2.jsp to verify:
4. Enable Tomcat directory traversal
In apache or Nginx, their directory traversal is on by default, while the Tomcat service, whose directory traversal is off by default, in some cases, we need to turn on its directory traversal function.
1. If you directly access 192.168.20.5:8080/shop/ for testing, an error of 404 will be reported:
2. Then, turn on its directory traversal function:
[root@tomcat ~] # vim / usr/local/tomcat/conf/web.xml # Edit this file # navigate to the tag, and listings true# modifies the original false to true# after about 110lines Save and exit # restart Tomcat service [root@tomcat ~] # / usr/local/tomcat/bin/shutdown.sh [root@tomcat ~] # / usr/local/tomcat/bin/startup.sh
3. After restarting, refresh the page you just visited, and you will find that the page displays all the files in the directory specified by its shop/, as follows:
5. Use Tomcat to monitor the real-time use of JVM virtual machine resources [root@tomcat ~] # vim / usr/local/tomcat/conf/server.xml # add a line below # add, save and exit [root@tomcat ~] # mkdir / data/monit [root@tomcat ~] # vim / data/monit/index.jsp # write a monitoring script and write the following content # after saving Save and exit # restart [root@tomcat ~] # / usr/local/tomcat/bin/shutdown.sh [root@tomcat ~] # / usr/local/tomcat/bin/startup.sh
When you visit 192.168.20.5:8080/monit/, you will see the following page:
Enable the JMX remote debugging function [root@tomcat ~] # vim / usr/local/tomcat/bin/catalina.sh # to edit its file, and jump to line 306 of the file export JDK_JAVA_OPTIONS # to add the following below the line Define its listening port as "8888" CATALINA_OPTS= "$CATALINA_OPTS-Dcom.sun.management.jmxremote-Dcom.sun.management.jmxremote.port=8888-Dcom.sun.management.jmxremote.ssl=false-Dcom.sun.management.jmxremote.authenticate=true" # after adding Save and exit [root@tomcat ~] # cd / usr/local/jdk1.8.0_211/jre/lib/management/ [root @ tomcat management] # cp jmxremote.password.template jmxremote.password [root@tomcat management] # vim jmxremote.password # write the user to be created at the end of the file as follows: ljz 123.com# needs to use the command "wq!" after writing because it is a read-only file. To force save [root@tomcat management] # vim jmxremote.access # to edit this file, give user defined permissions # jump to the end of the file, add the following configuration items, or directly modify the template ljz readwrite\ # at the beginning of this line specifies the user name create javax.management.monitor.*,javax.management.timer.*\ unregister# after writing the above Save and exit [root@tomcat management] # chmod 600 jmxremote.access jmxremote.password # must change its permission to 600, otherwise it will fail # restart the Tomcat service [root@tomcat ~] # / usr/local/tomcat/bin/shutdown.sh [root@tomcat ~] # / usr/local/tomcat/bin/startup.sh
To verify its remote debugging function, because it needs graphical support, it needs to enter its graphical desktop environment. The following operations cannot be done on xshell:
[root@tomcat ~] # jconsole # execute this command # if the above command cannot be executed, you need to execute the ". / etc/profile" command in order to reload the environment variables
After executing the jconsole command, the following interface pops up:
Then click as follows:
You can see the following interface, which shows the resource usage of the JVM virtual machine:
7. Open the mangent management page
1. When accessing the default interface of Tomcat, there is a "Manager App" box, as follows:
2. After clicking to enter, you will see the following 403 error page (translated as follows), and we need to copy the code it prompts:
This error page has clearly written the reason and how to configure it.
3. Now the configuration opens its manager management page:
[root@tomcat ~] # vim / usr/local/tomcat/conf/tomcat-users.xml # edit the file and jump to the end of the file # add two lines to the closed label to add the configuration item added by user #, with "username" as the user name and "password" as the user's password. [root@tomcat ~] # vim / usr/local/tomcat/webapps/manager/META-INF/context.xml # Edit this file and add the allowed IP allow= "192.168.20.254 | 192.168.20.5 | 127\.\ d +\.\ d +\.\ d + |: 1 | 0VOV0VOVOVOVOVOVOU1" / > # add the allowed IP after "allow=" above Verify the restart service [root@tomcat ~] # / usr/local/tomcat/bin/shutdown.sh [root@tomcat ~] # / usr/local/tomcat/bin/startup.sh with the pipe character "|"
4. Use the client to access its default home page, and then click "Manager App". You will see the following interface and enter your user name and password:
5. After logging in, you can see the custom directory under our Tomcat root directory:
6. On the drop-down page, you can add a custom directory through the war package:
1) prepare the war package:
[root@tomcat ~] # vim index.jsp # write jsp web page file testtest [root@tomcat ~] # jar-cvf test.war index.jsp # to make a war package named "test.war"
2) in the web graphical interface, add the war package:
3) after deployment, you can see the following:
4) the client can access TomcatIP+8080 port + / aaa for access testing, and its content is the content of the war package we have done, as follows:
Open the host-mangent management page 1, visit the following page, and then copy the code prompted by it:
[root@tomcat ~] # vim / usr/local/tomcat/conf/tomcat-users.xml # Edit its configuration file # add copied code at the end of the line And modify the user name and password [root@tomcat ~] # vim / usr/local/tomcat/webapps/host-manager/META-INF/context.xml # modify this file # add IP sources allowed to be accessed on this line # modify and save and exit # restart verification [root@tomcat ~] # / usr/local/tomcat/bin/shutdown.sh [root@tomcat ~] # / usr/local/tomcat/bin/startup.sh2, client verification:
Click again to prompt for the password:
At this point, you can manage the domain name information of tomcat through this page, but this feature is not recommended in production environments.
IX. Cutting the running log of Tomcat
Cronolog is a third-party log cutting tool, which is more convenient and fast, and is a powerful tool for log cutting. This third-party tool is used here.
1. Download the cronolog-1.6.2.tar.gz source code package in the link at the beginning of uploading the blog post to the Tomcat server. [root@tomcat ~] # tar zxf cronolog-1.6.2.tar.gz-C / usr/src # unpack [root@tomcat ~] # cd / usr/src/cronolog-1.6.2/ [root@tomcat cronolog-1.6.2] #. / configure & & make & & make install # compile and install [root@tomcat cronolog-1.6.2] # which cronolog # confirm that the command / usr/local/sbin/cronolog [root] can be found @ tomcat cronolog-1.6.2] # cd / usr/local/tomcat/ # switch to the Tomcat home directory [root@tomcat tomcat] # vim bin/catalina.sh # Edit this file to search for touch And comment out, for example: # touch "$CATALINA_OUT" and then jump to about line 470. modify it as follows: org.apache.catalina.startup.Bootstrap "$@" start 2 > & 1 | / usr/local/sbin/cronolog / usr/local/tomcat/logs/test.%Y-%m-%d.out > > / dev/null & and comment out the line around line 473 For example: # > > "$CATALINA_OUT" 2 > & 1 "&" # restart verification [root@tomcat ~] # / usr/local/tomcat/bin/shutdown.sh [root@tomcat ~] # / usr/local/tomcat/bin/startup.sh# verification: [root@tomcat tomcat] # ls logs/ # confirm that there is "test.2019-10-28.out" in this directory to catalina.2019-10-28.log localhost_access_ Log.2019-10-28.txtcatalina.out manager.2019-10-28.loghost-manager.2019-10-28.log test.2019-10-28.outlocalhost.2019-10-28.log 10, Define the page returned by the Tomcat 404 error
By default, when the client accesses a directory that does not exist on the Tomcat server, it will prompt an error of 404 and display the version information of our Tomcat, which is taboo. The error page returned by default is as follows:
The solution is to redirect the 404 status code so that we can customize the page information returned as follows:
[root@tomcat tomcat] # the error page customized by cat/ usr/local/tomcat/webapps/ROOT/error.jsp# is as follows: Access Error... [root@tomcat ~] # vim / usr/local/tomcat/conf/web.xml # Edit the file version= "3.1" > # add the following below this line (near line 22) 404/error.jsp# restart verification [root@tomcat ~] # / usr/local/tomcat/bin/shutdown.sh [root@tomcat ~] # / usr/local/tomcat/bin/startup.sh
At this point, visit a URL that does not exist again, and you will see the following page, as follows:
11. Memory optimization of Tomcat service [root@tomcat ~] # vim / usr/local/tomcat/bin/catalina.sh # Edit this file #! / bin/sh # add the following below the first line JAVA_OPTS= "- Dfile.encoding=UTF-8-server-Xms1023M-Xmx1024M-XX:NewSize=512M-XX:MaxNewSize=1020M-XX:NewRatio=2-XX:ParallelGCThreads=15-XX:+DisableExplicitGC" # after adding, save and exit, and restart Tomcat, it will take effect # you can use the previous real-time monitoring JVM virtual machine resources to view and verify # if the version before Tomcat 8.0, you can also add the following two configuration items, the version after 8.0 will be removed: #-XX:PermSize=256M#-XX:MaxPermSize=356M
The added configuration items are explained as follows:
Therefore, it is generally recommended that the maximum value of the heap be set to 80% of the maximum available memory. -Xmn: set the minimum memory of JVM (128256 is enough, generally not)-XX:NewSize: set the memory size of the new generation. In the new generation, the memory space allocated when the object is newly created, and the object is still not reclaimed after the garbage collector is collected, the object will be moved to the old generation-XX:PermSize: set the persistent memory size for storing static files, now Java classes, methods, and so on. Persistent generation has no significant effect on garbage collection, but some should
With the possibility of dynamically generating or calling some class, the persistence zone is also known as the method zone, which stores the structural information of each java class: such as runtime constant pool, field and method data, bytecode contents of constructors and ordinary methods, and special methods used for initialization of classes, instances, interfaces, and other data, JDK8 has removed the persistent generation (PermGen Space) and replaced the metaspace (Metaspace). Metaspace consumes local memory and no longer consumes virtual machine memory. -XX:MaxPermSize: set the maximum persistent generation memory size, permanent generation does not belong to heap memory, heap memory only contains the new generation and the old age-XX:NewRatio: change the proportion of new and old space, meaning that the size of the new space is 1x8 of the old space (default is 8)-XX:ParallelGCThreads can be used to increase parallelism, requires more cpu-XX:+DisableExplicitGC: turn off System.gc () The simple understanding is to turn off the memory recovery mechanism in java 12. Optimization of Tomcat thread pool [root@tomcat] # vim / usr/local/tomcat/conf/server.xml # Edit the main configuration file # add the following under line 53
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.