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
Will download good
apache-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
/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
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://184.108.40.206 : 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"
/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: Starting Tomcat... Oct 21 19:54:54 JourWon startup.sh: Neither the JAVA_HOME nor the JRE_...d Oct 21 19:54:54 JourWon startup.sh: At least one of these environment ...m Oct 21 19:54:54 JourWon systemd: tomcat.service: control process exited,...=1 Oct 21 19:54:54 JourWon systemd: Failed to start Tomcat. Oct 21 19:54:54 JourWon systemd: Unit tomcat.service entered failed state. Oct 21 19:54:54 JourWon systemd: 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 sy
stemctl 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: Existing PID file found during start. Oct 21 20:26:37 JourWon startup.sh: Removing/clearing stale PID file. Oct 21 20:26:37 JourWon startup.sh: Tomcat started. Oct 21 20:26:37 JourWon systemd: 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 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: tomcat.service start operation timed out. Terminating. Oct 21 20:28:07 JourWon systemd: Failed to start Tomcat.
Add Tomcat to boot
systemctl enable tomcat.service
Restart the server
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: Starting Tomcat... Oct 21 20:12:19 JourWon startup.sh: Existing PID file found during start. Oct 21 20:12:19 JourWon startup.sh: Removing/clearing stale PID file. Oct 21 20:12:19 JourWon startup.sh: Tomcat started. Oct 21 20:12:19 JourWon systemd: 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
- 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
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!