Installing Tomcat and setting Tomcat as boot item in Linux (centos7) (tomcat8 as an example)

Time:2021-5-11

Install Tomcat

Download Tomcat package

Tomcat has tomcat7, tomcat8 and tomcat9 versions. At present, tomcat8 is widely used by enterprises, so tomcat8 is listed here

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

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

There are three main versions of Tomcat

  • Tar.gz: compressed package under linux environment, installation free
  • Windows.zip: Windows compressed package, installation free, unzip and use. At the same time, pay attention to download the corresponding compressed package according to your computer’s 64 bit system or 32-bit system
  • Windows service installer: windows installation package, applicable to 32-bit and 64 bit versions of windows system

Install Tomcat

Will download goodapache-tomcat-8.5.47.tar.gzPut it in the specified directory. I’ll put it here/usr/local/tomcat, as shown in the figure below

get into/usr/local/tomcatDirectory, decompress Tomcat package

#Enter the / usr / local / Tomcat directory
cd /usr/local/tomcat
#Decompress the Tomcat package
tar -zxvf apache-tomcat-8.5.47.tar.gz

Start Tomcat

Enter the bin directory of Tomcat and start Tomcat

#Enter the bin directory of Tomcat and start Tomcat
cd apache-tomcat-8.5.47/bin/
#Start Tomcat
./startup.sh

Check whether Tomcat starts successfully

#Check whether Tomcat started successfully
ps -ef | grep tomcat
#If the output is as follows, Tomcat is installed successfully
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 start
root  2513 29060 0 22:18 pts/0 00:00:00 grep --color=auto tomcat

Use a browser to access Tomcat. The IP address of Linux is 8080. My IP port here is http://47.106.106.158 : 8080 /, as shown in the figure below, Tomcat is successfully installed and started in the Linux (centos7) environment. Like yourself

Note: open port 8080 or close firewall

Add: open port 8080 or close firewall. If it is alicloud, you can only configure open port in alicloud console

#Open port 8080
firewall-cmd --zone=public --add-port=8080/tcp --permanent
#Query whether port number 8080 is on
firewall-cmd --query-port=8080/tcp
#Restart firewall
firewall-cmd --reload
#View the list of open ports
firewall-cmd --list-port
#Command meaning
--Zone # scope
--Add port = 8080 / TCP # add port, format: port / communication protocol
--Permanent # takes effect permanently. Without this parameter, it will be invalid after restart
#Turn off firewall
Systemctl stop firewalld.service # stop firewall
Systemctl disable firewalld.service # disable firewalld.service

Set Tomcat as boot item

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

Create setenv.sh file and add startup parameters for Tomcat

Catalina.sh will call setenv.sh in the same level path to set additional environment variables during execution. Therefore, the setenv.sh file is created under the path of / usr / local / Tomcat / apache-tomcat-8.5.47/bin. The contents are as follows:

#Setting the PID file of Tomcat
CATALINA_PID="$CATALINA_BASE/tomcat.pid"
#Add JVM option
JAVA_OPTS="-server -XX:PermSize=256M -XX:MaxPermSize=1024m -Xms512M -Xmx1024M -XX:MaxNewSize=256m"

stay/usr/local/tomcat/apache-tomcat-8.5.47/bin/catalina.shAdd Java at the beginning of the file_ Home and JRE_ Home, where / usr / local / jdk1.8.0_ 152 is the installation directory of JDK


export JAVA_HOME=/usr/local/jdk1.8.0_152
export JRE_HOME=/usr/local/jdk1.8.0_152/jre

If Java is not configured in catalina.sh_ Home and JRE_ Home will report the following error


[[email protected] ~]# 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_...d
Oct 21 19:54:54 JourWon startup.sh[8746]: At least one of these environment ...m
Oct 21 19:54:54 JourWon systemd[1]: tomcat.service: control process exited,...=1
Oct 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 path of / usr / lib / SYSTEMd / system, as follows:


[Unit]
Description=Tomcat
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
TimeoutSec=0
PIDFile=/usr/local/tomcat/apache-tomcat-8.5.47/tomcat.pid
ExecStart=/usr/local/tomcat/apache-tomcat-8.5.47/bin/startup.sh
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target

In addition, after modifying the service file, you need to call systemctl daemon-reloadCommand to reload.

The purpose of configuring timeoutsec = 0 is to enable the boot to start without processing Tomcat startup timeout, so as to ensure that Tomcat will not be terminated by the system when it takes too long. If it is not configured, the following situation 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[464]: Unregistered Authentication Agent for unix-process:1628:19013 (system bus name :1.23, object path /org/freedesktop/PolicyKit1/AuthenticationAgent, loca
Oct 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

systemctl enable tomcat.service

Restart the server

reboot

After connecting again, view the service status


[[email protected] ~]# 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 as boot item. If it is shown as enabled, the setting is successful

Parameter description

  • Static: indicates that the service is associated with other services, and the start state of the service cannot be set separately
  • Disabled: indicates that the boot is prohibited
  • Enabled: indicates that boot is allowed

[[email protected] ~]# systemctl list-unit-files | grep tomcat
tomcat.service        enabled

summary

The above is Xiaobian’s introduction to Linux (centos7) installation of Tomcat and setting Tomcat as boot item (tomcat8 as an example). I hope it can help you. If you have any questions, please leave me a message and Xiaobian will reply you in time. Thank you very much for your support to developer!
If you think this article is helpful to you, please reprint, please indicate the source, thank you!

Recommended Today

Face mask detector based on retinanet

Author | guessCompile VKSource: analytics vidhya introduce Object detection is a very important field in computer vision, which is necessary for automatic driving, video surveillance, medical applications and many other fields. We are fighting an epidemic of unprecedented scale. Researchers around the world are trying to develop a vaccine or a treatment for cowid-19, while […]