Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

CentOS7 how to install Tomcat and set Tomcat as the boot startup item

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how to install Tomcat and set Tomcat as the startup item for CentOS7, I believe most people do not know much about it, so share this article for your reference. I hope you will gain a lot after reading this article. Let's learn about it together.

Install Tomcat to download Tomcat package

Tomcat has versions such as Tomcat7,Tomcat8 and Tomcat9. At present, Tomcat8 is widely used by enterprises, so Tomcat8 is listed here.

Go to Tomcat8 download website: Tomcat8 download URL https://tomcat.apache.org/download-80.cgi

Click on the corresponding version under Download on the left. Here I download apache-tomcat-8.5.47.tar.gz, the compressed package of the Linux environment.

There are three main installed versions of Tomcat

Compression package in tar.gz:Linux environment, free of installation

Windows.zip:Windows compression package, no installation, decompression is ready to use, and pay attention to download the corresponding package according to whether your computer is a 64-bit system or a 32-bit system

Windows Service Installer:Windows installation package for both 32-bit and 64-bit versions of Windows systems

Install Tomcat

Put the downloaded apache-tomcat-8.5.47.tar.gz in the specified directory and / usr/local/tomcat here, as shown in the following figure

Enter the / usr/local/tomcat directory and extract the Tomcat package

# enter the / usr/local/tomcat directory cd / usr/local/tomcat# to extract the Tomcat package tar-zxvf apache-tomcat-8.5.47.tar.gz to start Tomcat

Go to the bin directory of Tomcat and start Tomcat

# enter the bin directory of Tomcat, start Tomcatcd apache-tomcat-8.5.47/bin/# and start Tomcat./startup.sh

Check to see if Tomcat starts successfully

# check whether Tomcat starts to execute ps-ef successfully | grep tomcat# if the output is as follows Indicates that Tomcat was successfully installed root 2381 1 11 22:18 pts/0 00:00:02 / usr/local/jdk1.8.0_152/bin/java-Djava.util.logging.config.file=/usr/local/tomcat/apache-tomcat-8.5.47/conf/logging.properties-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager-Djdk.tls.ephemeralDHKeySize=2048-Djava.protocol.handler.pkgs=org.apache.catalina.webresources-Dorg.apache.catalina.security.SecurityListener.UMASK=0027-Dignore.endorsed .dirs =-classpath / usr/local/tomcat/apache-tomcat-8.5.47/bin/bootstrap.jar:/usr/local/tomcat/apache-tomcat-8.5.47/bin/tomcat-juli.jar-Dcatalina.base=/usr/local/tomcat/apache-tomcat-8.5.47-Dcatalina.home=/usr/local/tomcat/apache-tomcat-8.5.47-Djava.io.tmpdir=/usr/local/tomcat/apache-tomcat-8.5.47/temp org.apache.catalina .startup.Bootstrap startroot 2513 29060 0 22:18 pts/0 00:00:00 grep-- color=auto tomcat

Use the browser to access Tomcat, address Linux's ip:8080, and my ip port here is http://47.106.106.158:8080/. As shown in the figure below, you have successfully installed and started Tomcat in the Linux (CentOS7) environment. Give yourself a like.

Note: open port 8080 or turn off the firewall

Additional: open port 8080 or turn off the firewall. In the case of Aliyun, you can only configure the open port on the Aliyun console.

# Open port 8080 firewall-cmd-- zone=public-- add-port=8080/tcp-- permanent# query port number 8080 whether firewall-cmd is enabled-- query-port=8080/tcp# restart firewall firewall-cmd-- reload# view open port list firewall-cmd-- list-port# command meaning-- zone # scope-- add-port=8080/tcp # add port format: Port / communication protocol-- permanent# takes effect permanently No such parameter expires after reboot # close firewall systemctl stop firewalld.service # stop firewallsystemctl disable firewalld.service # disable firewall boot set Tomcat to boot item

Start Tomcat as above. If our virtual machine or server is shut down, Tomcat will be turned off after restarting the server, but we hope that after the virtual machine or server is restarted, Tomcat can start itself, so we need to set Tomcat as the boot item.

Create a setenv.sh file to add startup parameters for Tomcat

When catalina.sh executes, it calls setenv.sh under the sibling path to set additional environment variables, so create a setenv.sh file under the / usr/local/tomcat/apache-tomcat-8.5.47/bin path, as follows:

# set Tomcat's PID file CATALINA_PID= "$CATALINA_BASE/tomcat.pid" # add JVM option JAVA_OPTS= "- server-XX:PermSize=256M-XX:MaxPermSize=1024m-Xms512M-Xmx1024M-XX:MaxNewSize=256m"

Add JAVA_HOME and JRE_HOME at the beginning of the / usr/local/tomcat/apache-tomcat-8.5.47/bin/catalina.sh file, where / usr/local/jdk1.8.0_152 is the jdk installation directory

Export JAVA_HOME=/usr/local/jdk1.8.0_152export JRE_HOME=/usr/local/jdk1.8.0_152/jre

If JAVA_HOME and JRE_HOME are not configured in catalina.sh, the following error will be reported

[root@JourWon] # systemctl status tomcat ● tomcat.service-Tomcat Loaded: loaded (/ usr/lib/systemd/system/tomcat.service; enabled; vendor preset: disabled) Active: failed (Result: exit-code) since Mon 2019-10-21 19:54:54 CST 6s ago Process: 8746 ExecStart=/usr/local/tomcat/apache-tomcat-8.5.47/bin/startup.sh (code=exited, status=1/FAILURE) Oct 21 19:54:54 JourWon systemd [1]: Starting Tomcat...Oct 21 19:54:54 JourWon startup.sh [8746]: Neither the JAVA_HOME nor the JRE_...dOct 21 19:54:54 JourWon startup.sh [8746]: At least one of these environment... mOct 21 19:54:54 JourWon systemd [1]: tomcat.service: control process exited = 1Oct 21 19:54:54 JourWon systemd [1]: Failed to start Tomcat.Oct 21 19:54:54 JourWon systemd [1]: Unit tomcat.service entered failed state.Oct 21 19:54:54 JourWon systemd [1]: tomcat.service failed.Hint: Some lines were ellipsized, use-l to show in full.

Add the tomcat.service file under the / usr/lib/systemd/system path as follows:

[Unit] Description=TomcatAfter=network.target remote-fs.target nss-lookup.target [Service] Type=forkingTimeoutSec=0PIDFile=/usr/local/tomcat/apache-tomcat-8.5.47/tomcat.pidExecStart=/usr/local/tomcat/apache-tomcat-8.5.47/bin/startup.shExecReload=/bin/kill-s HUP $MAINPIDExecStop=/bin/kill-s QUIT $MAINPIDPrivateTmp= true [install] WantedBy=multi-user.target

In addition, after the service file is modified, you need to call the systemctl daemon-reload command to reload.

The purpose of configuring TimeoutSec=0 is to enable boot boot not to handle tomcat startup timeout, and to ensure that tomcat takes too long and will not be terminating by the system. If it is not configured, the following may occur

Oct 21 20:26:37 JourWon startup.sh [1634]: Existing PID file found during start.Oct 21 20:26:37 JourWon startup.sh [1634]: Removing/clearing stale PID file.Oct 21 20:26:37 JourWon startup.sh [1634]: Tomcat started.Oct 21 20:26:37 JourWon systemd [1]: PID file / usr/local/tomcat/apache-tomcat-8.5.47/tomcat.pid not readable (yet?) After start.Oct 21 20:26:38 JourWon polkitd: Unregistered AuthenticationAgent for unix-process:1628:19013 (system bus name: 1.23, object path / org/freedesktop/PolicyKit1/AuthenticationAgent, locaOct 21 20:28:07 JourWon systemd [1]: tomcat.service start operation timed out. Terminating.Oct 21 20:28:07 JourWon systemd [1]: Failed to start Tomcat.

Add Tomcat to boot self-boot

Systemctl enable tomcat.service

Restart the server

Reboot

After connecting again, check the service status

[root@JourWon] # systemctl status tomcat ● tomcat.service-Tomcat Loaded: loaded (/ usr/lib/systemd/system/tomcat.service; enabled; vendor preset: disabled) Active: activating (start) since Mon 2019-10-21 20:12:19 CST 8s ago Process: 9244 ExecStart=/usr/local/tomcat/apache-tomcat-8.5.47/bin/startup.sh (code=exited Status=0/SUCCESS) CGroup: / system.slice/tomcat.service └─ 9255 / usr/local/jdk1.8.0_152/jre/bin/java-Djava.util.logging.config.file=/usr/local/tomcat/apache-tomcat-8.5.47/conf/logging.properties-Djava.util.logging.manager=org.apac...Oct 21 20:12:19 JourWon systemd [1]: Starting Tomcat...Oct 21 20:12:19 JourWon startup.sh [9244]: Existing PID file found during Start.Oct 21 20:12:19 JourWon startup.sh [9244]: Removing/clearing stale PID file.Oct 21 20:12:19 JourWon startup.sh [9244]: Tomcat started.Oct 21 20:12:19 JourWon systemd [1]: PID file / usr/local/tomcat/apache-tomcat-8.5.47/tomcat.pid not readable (yet?) After start.

View boot list command

Systemctl list-unit-files | grep enabled

Check whether Tomcat is set to boot. If it is displayed as enabled, the setting is successful.

Parameter description

Static: indicates that the service is associated with other services, and the startup status of the service cannot be set separately

Disabled: forbids boot

Enabled: boot is allowed

[root@JourWon ~] # systemctl list-unit-files | grep tomcattomcat.service enabled is all the contents of the article "how to install Tomcat and set Tomcat as boot item for CentOS7". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report