Linux of CentOS7 Install Tomcat and set Tomcat as boot entry of tomcat8
- 2021-07-06 12:21:00
- OfStack
Installing Tomcat
Download the Tomcat compressed package
There are Tomcat7, Tomcat8 and Tomcat9 versions of Tomcat. At present, Tomcat8 is widely used by enterprises, so Tomcat8 is listed here
Enter 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, that is, the compressed package of Linux environment
Tomcat is mainly available in 3 installations
tar. gz: Compressed package in Linux environment, installation-free Windows. zip: Windows compressed package, free of installation, uncompressed and ready to use. At the same time, pay attention to downloading the corresponding compressed 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 systemsInstalling Tomcat
Will be downloaded
apache-tomcat-8.5.47.tar.gz
Put it in the specified directory, and I put it here
/usr/local/tomcat
As shown in the following figure
Enter
/usr/local/tomcat
Directory, extract Tomcat compressed package
# Enter /usr/local/tomcat Directory
cd /usr/local/tomcat
# Decompression Tomcat Compressed package
tar -zxvf apache-tomcat-8.5.47.tar.gz
Start Tomcat
Enter the bin directory of Tomcat and start Tomcat
# Enter Tomcat Adj. bin Directory, startup Tomcat
cd apache-tomcat-8.5.47/bin/
# Start Tomcat
./startup.sh
Check whether Tomcat started successfully
# View Tomcat Whether to start successful execution
ps -ef | grep tomcat
# If the output is as follows, explain Tomcat Installation succeeded
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 the browser to access Tomcat, and the address is ip: 8080 of Linux. The port of ip here is http://47.106.106.158:8080/. The following figure shows that Tomcat was successfully installed and started in Linux (CentOS7) environment. Click a like for yourself
Note: Open port 8080 or close the firewall
Additional: Open port 8080 or close firewall. If it is Alibaba Cloud, you can only configure the open port in Alibaba Cloud Console
# Open 8080 Port
firewall-cmd --zone=public --add-port=8080/tcp --permanent
# Query port number 8080 Whether to turn it on
firewall-cmd --query-port=8080/tcp
# Restart the firewall
firewall-cmd --reload
# View the list of open ports
firewall-cmd --list-port
# Command meaning
--zone # Scope
--add-port=8080/tcp # Add a port in the format: Port / Communication protocol
--permanent # Permanently effective, invalid after restarting without this parameter
# Close the firewall
systemctl stop firewalld.service # Stop firewall
systemctl disable firewalld.service # Prohibit firewall Startup startup
Set Tomcat as boot entry
Start Tomcat according to the above method. If our virtual machine or server is closed, Tomcat is closed after restarting the server, but we hope that Tomcat can start itself after restarting the virtual machine or server, so we need to set Tomcat as the startup item
Create an setenv. sh file to add startup parameters for Tomcat
catalina. sh is executed by calling setenv. sh under the sibling path to set additional environment variables, so the setenv. sh file is created under the path/usr/local/tomcat/apache-tomcat-8. 5.47/bin, as follows:
# Settings Tomcat Adj. PID Documents
CATALINA_PID="$CATALINA_BASE/tomcat.pid"
# Add JVM Options
JAVA_OPTS="-server -XX:PermSize=256M -XX:MaxPermSize=1024m -Xms512M -Xmx1024M -XX:MaxNewSize=256m"
In
/usr/local/tomcat/apache-tomcat-8.5.47/bin/catalina.sh
Add JAVA_HOME and JRE_HOME at the beginning of the file, 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_HOME and JRE_HOME are not configured in catalina. sh, the following error is 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_...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/usr/lib/systemd/system path, 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, sy needs to be called after the service file is modified
stemctl daemon-reload
Command to reload.
The purpose of configuring TimeoutSec=0 is to make the startup do not handle the startup timeout of tomcat, and ensure that tomcat will not be used by the system terminating when it takes too long. If it is not configured, the following situations 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 startup self-startup
systemctl enable tomcat.service
Restart the server
reboot
After connecting again, view 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
Off- 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 boot list command
systemctl list-unit-files | grep enabled
Check whether Tomcat is set as a boot entry. If it is displayed as enabled, it indicates that the setting is successful
Parameter description
static: Indicates that this service is associated with other services, and the startup status of this service cannot be set separately disabled: Indicates that boot is prohibited enabled: Indicates that boot is allowed
# Enter Tomcat Adj. bin Directory, startup Tomcat
cd apache-tomcat-8.5.47/bin/
# Start Tomcat
./startup.sh
0
Summarize