目录
一、获取安装文件
本文安装的Tomcat为8.5.82版本,可以从官网很方便的获取,点击链接可以直达:Apache Tomcat® - Apache Tomcat 8 Software Downloads
直接下载tar.gz的压缩包即可,也可以点击链接直接下载:apache-tomcat-8.5.82.tar.gzhttps://dlcdn.apache.org/tomcat/tomcat-8/v8.5.82/bin/apache-tomcat-8.5.82.tar.gz
将下载的安装包上传到服务器上,例如上传到/mnt/software/tools文件夹下
使用tar命令解压:
[root@gts03 tools]# ll
total 815884
-rw-r--r-- 1 root root 10610025 Aug 14 22:34 apache-tomcat-8.5.82.tar.gz
-rw-r--r-- 1 root root 148162542 Aug 14 22:27 jdk-8u341-linux-x64.tar.gz
-rw-r--r-- 1 root root 676676298 Aug 13 18:15 mysql-5.7.39-linux-glibc2.12-x86_64.tar.gz
[root@gts03 tools]# tar -zxvf apache-tomcat-8.5.82.tar.gz
把解压后的文件夹移动到/mnt/software目录下,并重命名为tomcat-8.5.82-gtmall
[root@gts03 tools]# mv apache-tomcat-8.5.82 ../tomcat-8.5.82-gtmall
[root@gts03 tools]# cd ..
[root@gts03 software]# ll
total 32
drwxr-xr-x 8 root root 4096 Aug 14 22:36 jdk8
drwx------ 2 root root 16384 Aug 14 17:57 lost+found
drwxr-xr-x 9 mysql mysql 4096 Aug 14 18:05 mysql
drwxr-xr-x 9 root root 4096 Aug 14 22:55 tomcat-8.5.82-gtmall
drwxr-xr-x 2 root root 4096 Aug 14 22:56 tools
二、启动tomat
进入tomcat的bin目录,启动tomcat:
[root@gts03 bin]# cd /mnt/software/tomcat-8.5.82-gtmall/bin
[root@gts03 bin]# ./startup.sh
Using CATALINA_BASE: /mnt/software/tomcat-8.5.82-gtmall
Using CATALINA_HOME: /mnt/software/tomcat-8.5.82-gtmall
Using CATALINA_TMPDIR: /mnt/software/tomcat-8.5.82-gtmall/temp
Using JRE_HOME: /mnt/software/jdk8
Using CLASSPATH: /mnt/software/tomcat-8.5.82-gtmall/bin/bootstrap.jar:/mnt/software/tomcat-8.5.82-gtmall/bin/tomcat-juli.jar
Using CATALINA_OPTS:
Tomcat started.
浏览器中输入IP:8080,如果出现以下界面,说明安装成功:
三、安装为系统服务
CentOS系统中所有的服务在 /etc/init.d/
目录下都对应一个脚本,新建一个服务也即新建一个脚本在该目录下。
这里新建 Tomcat
的启动脚本。
vim /etc/init.d/tomcat-gtmall
#!/bin/sh -e
###必须添加,才能使用chkconfig命令设为自启动,2345参数表示,在哪些运行级别启动,启动序号61;关闭序号61;两位数字即可
#chkconfig:2345 61 61
##JDK目录
export JAVA_HOME=/mnt/software/jdk8
export JRE_HOME=$JAVA_HOME/jre
export PATH=$PATH:$JAVA_HOME/bin
export CLASSPATH=.
export CATALINA_HOME=/mnt/software/tomcat-8.5.82-gtmall
case "$1" in
start)
${CATALINA_HOME}/bin/startup.sh
exit 0
;;
stop)
${CATALINA_HOME}/bin/shutdown.sh
exit 0
;;
debug)
${CATALINA_HOME}/bin/shutdown.sh
${CATALINA_HOME}/bin/catalina.sh run
exit 0
;;
force-reload|restart)
${CATALINA_HOME}/bin/startup.sh
${CATALINA_HOME}/bin/shutdown.sh
exit 0
;;
*)
echo "Usage: /etc/init.d/tomcat-gtmall {start|stop|restart|force-reload|debug}"
exit 1
;;
esac
exit 0
ESC:wq保存并退出。修改文件的权限:
chmod o+x /etc/init.d/tomcat-gtmall
添加为开机启动:
chkconfig --add tomcat-gtmall
chkconfig --list
先停止原来的服务,以服务形式启动:
[root@gts03 bin]# ./shutdown.sh
Using CATALINA_BASE: /mnt/software/tomcat-8.5.82-gtmall
Using CATALINA_HOME: /mnt/software/tomcat-8.5.82-gtmall
Using CATALINA_TMPDIR: /mnt/software/tomcat-8.5.82-gtmall/temp
Using JRE_HOME: /mnt/software/jdk8
Using CLASSPATH: /mnt/software/tomcat-8.5.82-gtmall/bin/bootstrap.jar:/mnt/software/tomcat-8r
Using CATALINA_OPTS:
[root@gts03 bin]# service tomcat-gtmall start
Using CATALINA_BASE: /mnt/software/tomcat-8.5.82-gtmall
Using CATALINA_HOME: /mnt/software/tomcat-8.5.82-gtmall
Using CATALINA_TMPDIR: /mnt/software/tomcat-8.5.82-gtmall/temp
Using JRE_HOME: /mnt/software/jdk8/jre
Using CLASSPATH: /mnt/software/tomcat-8.5.82-gtmall/bin/bootstrap.jar:/mnt/software/tomcat-8r
Using CATALINA_OPTS:
Tomcat started.
再次访问IP:8080,可以正常访问
执行./shutdown.sh时报错java.net.ConnectException: Connection refused
Using CATALINA_BASE: /mnt/software/tomcat-8.5.82-gtmall
Using CATALINA_HOME: /mnt/software/tomcat-8.5.82-gtmall
Using CATALINA_TMPDIR: /mnt/software/tomcat-8.5.82-gtmall/temp
Using JRE_HOME: /mnt/software/jdk8
Using CLASSPATH: /mnt/software/tomcat-8.5.82-gtmall/bin/bootstrap.jar:/mnt/software/tomcat-8.5.82-gtmall/bin/tomcat-juli.jar
Using CATALINA_OPTS:
Aug 15, 2022 12:00:16 AM org.apache.catalina.startup.Catalina stopServer
SEVERE: Could not contact [localhost:8005] (base port [8005] and offset [0]). Tomcat may not be running.
Aug 15, 2022 12:00:16 AM org.apache.catalina.startup.Catalina stopServer
SEVERE: Error stopping Catalina
java.net.ConnectException: Connection refused (Connection refused)
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:476)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:218)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:200)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:394)
at java.net.Socket.connect(Socket.java:606)
at java.net.Socket.connect(Socket.java:555)
at java.net.Socket.<init>(Socket.java:451)
at java.net.Socket.<init>(Socket.java:228)
at org.apache.catalina.startup.Catalina.stopServer(Catalina.java:511)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.catalina.startup.Bootstrap.stopServer(Bootstrap.java:391)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:481)
找到jdk安装目录下lib/security(/mnt/software/jdk8/jre/lib/security)文件夹下的java.security文件,将securerandom.source=file:/dev/random
修改为:securerandom.source=file:/dev/urandom
查看tomcat是否关闭ps -ef|grep java,如果有执行杀掉进程
[root@gts03 bin]# ps -ef|grep java
root 2399 1 0 Aug14 pts/2 00:00:02 /mnt/software/jdk8/jre/bin/java -Djava.util.loggint
root 2454 1 0 Aug14 pts/2 00:00:01 /mnt/software/jdk8/jre/bin/java -Djava.util.loggint
root 2540 1 0 Aug14 pts/2 00:00:02 /mnt/software/jdk8/bin/java -Djava.util.logging.cot
root 2756 2702 0 00:08 pts/3 00:00:00 grep --color=auto java
[root@gts03 bin]# kill -9 2399
[root@gts03 bin]# kill -9 2454
[root@gts03 bin]# kill -9 2540
[root@gts03 bin]# ps -ef|grep java
root 2756 2702 0 00:09 pts/3 00:00:00 grep --color=auto java