以前经常使用java8 也就是java jdk1.8 和tomcat7开发web系统,怀旧记录一下在centos7系统里安装部署tomcat7,同时部署多个tomcat7的方法,解决tomcat7在centos7系统启动很慢的问题
apache tomcat7远离了,在tomcat官网上已经看不到tomcat7的身影了,我自己也早已不再用tomcat7,在整理曾经的保存的博客和项目时,简单记录一下tomcat7的安装使用过程,将来或许用不到了,或许遇到老项目时还是会用到,因此怀念记录一下tomcat7的安装和配置
首先在官网上找到老版本
https://archive.apache.org/dist/tomcat/
下载后上传到centos7服务器的/home目录下面
解压
tar -xvzf apache-tomcat-7.0.81.tar.gz
修改名称
mv apache-tomcat-7.0.81 tomcat7
启动之前确定当前系统有没有java环境(java8 jdk1.8)
[root@myw home]# java -version
java version "1.8.0_144"
Java(TM) SE Runtime Environment (build 1.8.0_144-b01)
Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)
[root@myw home]#
启动并确认是否启动成功
[root@myw home]# cd tomcat7/bin
[root@myw bin]# ls
bootstrap.jar catalina.sh commons-daemon.jar configtest.bat daemon.sh digest.sh setclasspath.sh shutdown.sh startup.sh tomcat-native.tar.gz tool-wrapper.sh version.sh
catalina.bat catalina-tasks.xml commons-daemon-native.tar.gz configtest.sh digest.bat setclasspath.bat shutdown.bat startup.bat tomcat-juli.jar tool-wrapper.bat version.bat
[root@myw bin]# ./startup.sh
Using CATALINA_BASE: /home/tomcat7
Using CATALINA_HOME: /home/tomcat7
Using CATALINA_TMPDIR: /home/tomcat7/temp
Using JRE_HOME: /usr/local/jdk18
Using CLASSPATH: /home/tomcat7/bin/bootstrap.jar:/home/tomcat7/bin/tomcat-juli.jar
Tomcat started.
[root@myw bin]# netstat -lnp|grep java
tcp6 0 0 :::8080 :::* LISTEN 27905/java
tcp6 0 0 127.0.0.1:8005 :::* LISTEN 27905/java
tcp6 0 0 :::8009 :::* LISTEN 27905/java
[root@myw bin]#
访问浏览器的8080端口(默认端口),保证服务器8080端口可访问(系统防火墙和云商安全组配置)
关闭停止
[root@myw bin]# ./shutdown.sh
Using CATALINA_BASE: /home/tomcat7
Using CATALINA_HOME: /home/tomcat7
Using CATALINA_TMPDIR: /home/tomcat7/temp
Using JRE_HOME: /usr/local/jdk18
Using CLASSPATH: /home/tomcat7/bin/bootstrap.jar:/home/tomcat7/bin/tomcat-juli.jar
[root@myw bin]#
查看控制台输出
在tomcat7/logs里
tail -200f catalina.out
[root@myw bin]# cd ..
[root@myw tomcat7]# ls
bin conf lib LICENSE logs NOTICE RELEASE-NOTES RUNNING.txt temp webapps work
[root@myw tomcat7]# cd logs
[root@myw logs]# ls
catalina.2023-01-29.log catalina.out host-manager.2023-01-29.log localhost.2023-01-29.log localhost_access_log.2023-01-29.txt manager.2023-01-29.log
[root@jpmlg4syqgfe22ch logs]# tail -200f catalina.out
Jan 29, 2023 5:37:22 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version: Apache Tomcat/7.0.81
Jan 29, 2023 5:37:22 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built: Aug 11 2017 10:21:27 UTC
Jan 29, 2023 5:37:22 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server number: 7.0.81.0
Jan 29, 2023 5:37:22 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name: Linux
Jan 29, 2023 5:37:22 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version: 3.10.0-1160.el7.x86_64
Jan 29, 2023 5:37:22 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture: amd64
Jan 29, 2023 5:37:22 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home: /usr/local/jdk18/jre
Jan 29, 2023 5:37:22 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version: 1.8.0_144-b01
Jan 29, 2023 5:37:22 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor: Oracle Corporation
Jan 29, 2023 5:37:22 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE: /home/tomcat7
Jan 29, 2023 5:37:22 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME: /home/tomcat7
Jan 29, 2023 5:37:22 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.util.logging.config.file=/home/tomcat7/conf/logging.properties
Jan 29, 2023 5:37:22 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
Jan 29, 2023 5:37:22 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djdk.tls.ephemeralDHKeySize=2048
Jan 29, 2023 5:37:22 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.endorsed.dirs=/home/tomcat7/endorsed
Jan 29, 2023 5:37:22 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=/home/tomcat7
Jan 29, 2023 5:37:22 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=/home/tomcat7
Jan 29, 2023 5:37:22 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.io.tmpdir=/home/tomcat7/temp
Jan 29, 2023 5:37:22 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
Jan 29, 2023 5:37:22 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Jan 29, 2023 5:37:22 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Jan 29, 2023 5:37:22 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 471 ms
Jan 29, 2023 5:37:22 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Jan 29, 2023 5:37:22 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.81
Jan 29, 2023 5:37:22 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /home/tomcat7/webapps/docs
Jan 29, 2023 5:37:22 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory /home/tomcat7/webapps/docs has finished in 258 ms
Jan 29, 2023 5:37:22 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /home/tomcat7/webapps/host-manager
Jan 29, 2023 5:37:22 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory /home/tomcat7/webapps/host-manager has finished in 74 ms
Jan 29, 2023 5:37:22 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /home/tomcat7/webapps/manager
Jan 29, 2023 5:37:22 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory /home/tomcat7/webapps/manager has finished in 64 ms
Jan 29, 2023 5:37:22 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /home/tomcat7/webapps/ROOT
Jan 29, 2023 5:37:22 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory /home/tomcat7/webapps/ROOT has finished in 46 ms
Jan 29, 2023 5:37:22 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /home/tomcat7/webapps/examples
Jan 29, 2023 5:37:23 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory /home/tomcat7/webapps/examples has finished in 139 ms
Jan 29, 2023 5:37:23 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Jan 29, 2023 5:37:23 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Jan 29, 2023 5:37:23 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 623 ms
部署多个tomcat7 重新解压并且修改为tomcat7-2
两个tomcat同时是无法启动的
<Server port="8005" shutdown="SHUTDOWN">
………………………………………………………………………………………………………………
<!-- A "Connector" represents an endpoint by which requests are received
and responses are returned. Documentation at :
Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
Java AJP Connector: /docs/config/ajp.html
APR (HTTP/AJP) Connector: /docs/apr.html
Define a non-SSL HTTP/1.1 Connector on port 8080
-->
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<!-- Define an AJP 1.3 Connector on port 8009 -->
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
</Server>
这个是tomcat7conf文件夹下server.xml的配置
tomcat7-2 的改动 8005 改成 8006
8080改成8081
8009改成8010
改的端口可以随意,根据实际需要
启动后
[root@myw ~]# netstat -lnp|grep java
tcp6 0 0 :::8080 :::* LISTEN 31034/java
tcp6 0 0 :::8081 :::* LISTEN 31104/java
tcp6 0 0 127.0.0.1:8005 :::* LISTEN 31034/java
tcp6 0 0 127.0.0.1:8006 :::* LISTEN 31104/java
tcp6 0 0 :::8009 :::* LISTEN 31034/java
tcp6 0 0 :::8010 :::* LISTEN 31104/java
[root@myw ~]#
可以看到2个tomcat7都启动了,各自占用三个端口
但是有些时候在centos7.x下面发现tomcat7启动很慢(有时候等好几分钟),特别是多个tomcat7启动的情况,这里官方和很多博文都有一些说明,说java.security.SecureRandom类要调用还是啥在是在linux(Centos)上,/dev/random和/dev/urandom啥原因,办法是修改一个东西就可以启动很快
在$JAVA_PATH/jre/lib/security/java.security目录下的这个文件改东西
打开这个文件后可以看到有一行配置是这样的
securerandom.source=file:/dev/random
#
# Sun Provider SecureRandom seed source.
#
# Select the primary source of seed data for the "SHA1PRNG" and
# "NativePRNG" SecureRandom implementations in the "Sun" provider.
# (Other SecureRandom implementations might also use this property.)
#
# On Unix-like systems (for example, Solaris/Linux/MacOS), the
# "NativePRNG" and "SHA1PRNG" implementations obtains seed data from
# special device files such as file:/dev/random.
#
# On Windows systems, specifying the URLs "file:/dev/random" or
# "file:/dev/urandom" will enable the native Microsoft CryptoAPI seeding
# mechanism for SHA1PRNG.
#
# By default, an attempt is made to use the entropy gathering device
# specified by the "securerandom.source" Security property. If an
# exception occurs while accessing the specified URL:
#
# SHA1PRNG:
# the traditional system/thread activity algorithm will be used.
#
# NativePRNG:
# a default value of /dev/random will be used. If neither
# are available, the implementation will be disabled.
# "file" is the only currently supported protocol type.
#
# The entropy gathering device can also be specified with the System
# property "java.security.egd". For example:
#
# % java -Djava.security.egd=file:/dev/random MainClass
#
# Specifying this System property will override the
# "securerandom.source" Security property.
#
# In addition, if "file:/dev/random" or "file:/dev/urandom" is
# specified, the "NativePRNG" implementation will be more preferred than
# SHA1PRNG in the Sun provider.
#
securerandom.source=file:/dev/random
注释掉修改成
#securerandom.source=file:/dev/random
securerandom.source=file:/dev/./urandom
这样,tomcat7和tomcat7-2就能快速地启动了(当然,有时候tomcat7不会存在启动慢的问题)
现在启动两个tomcat7只需要端口修改后,就各自是各自的,但是以前曾经记得部署两个tomcat7的时候,jdk有一个path配置
CATALINA_HOME
CATALINA_BASE
这个第一个tomcat7可以默认(一般也配置了的)
1.首先在/etc/profile里和java jdk配置放一起,配置后source /etc/profile指令
export CATALINA_HOME=/home/tomcat7
export CATALINA_BASE=/home/tomcat7
export CATALINA2_HOME=/home/tomcat7-2
export CATALINA2_BASE=/home/tomcat7-2
export JAVA_HOME=/usr/local/jdk18
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$JAVA_HOME/bin:$PATH
2.tomcat7和tomcat7-2的bin目录下的catalina.sh文件开头处各自增加配置信息
export CATALINA_BASE=$CATALINA_BASE
export CATALINA_HOME=$CATALINA_HOME
export CATALINA_BASE=$CATALINA2_BASE
export CATALINA_HOME=$CATALINA2_HOME
两个都配置好后可以关停重启后测试
tomcat配置ssl方式(阿里申请免费也有详细部署方式) 80端口重定向到443端口
-->
<Connector port="80" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="443" />
<Connector port="443" protocol="org.apache.coyote.http11.Http11Protocol"
maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS"
keystoreFile="/home/tomcat7/conf/xxx-xxxxxxx-com-tomcat-345456.jks"
keystorePass="12345678" />
这个.jks的文件要放在conf同级目录下面
tomcat7的默认编码是ISO-8859-1,习惯性使用UTF-8,那么修改一下配置
URIEncoding="UTF-8"
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="UTF-8"/>
当然还有一些其他配置,例如最大线程数,默认不配置大概是200 需要的可以配置maxThreads="1000",还有一些其他的配置,需要去官网看Connector节点的配置,一般默认已经能够满足需求了
一般tomcat7在centos7.x系统下要配置开机启动,防止系统出现问题重启后tomcat7没启动的情况
首先需要配置java jdk的路径,在tomcat7和tomcat7-2的bin目录下catalina.sh里配置
export JAVA_HOME=/usr/local/jdk18
export JRE_HOME=/usr/local/jdk18/jre
tomcat7/bin/catalina.sh
tomcat7-2/bin/catalina.sh
这里是配置了2个tomcat的,那么需要创建两个tomcat的服务
tomcat7的tomcat7.service
[Unit]
Description=tomcat7 web server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/home/tomcat7/bin/startup.sh
ExecStop=/home/tomcat7/bin/shutdown.sh
[Install]
WantedBy=multi-user.target
tomcat7-2的tomcat7-2.service
[Unit]
Description=tomcat7-2 web server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/home/tomcat7-2/bin/startup.sh
ExecStop=/home/tomcat7-2/bin/shutdown.sh
[Install]
WantedBy=multi-user.target
将tomcat7.service和tomcat7-2.service放在/usr/lib/systemd/system/下面
// service文件有新加和更改都需要重新加载
systemctl daemon-reload
// 启动
systemctl start tomcat7.service
systemctl start tomcat7-2.service
// 停止
systemctl stop tomcat7.service
systemctl stop tomcat7-2.service
// 查看启动状态
systemctl status tomcat7.service
systemctl status tomcat7-2.service
// 加入开机启动
systemctl enable tomcat7.service
systemctl enable tomcat7-2.service
// 取消开机启动
systemctl disable tomcat7.service
systemctl disable tomcat7-2.service
[root@myw ~]# systemctl enable tomcat7.service
Created symlink from /etc/systemd/system/multi-user.target.wants/tomcat7.service to /usr/lib/systemd/system/tomcat7.service.
[root@myw ~]# systemctl enable tomcat7-2.service
Created symlink from /etc/systemd/system/multi-user.target.wants/tomcat7-2.service to /usr/lib/systemd/system/tomcat7-2.service.
[root@myw ~]# systemctl start tomcat7.service
[root@myw ~]# systemctl start tomcat7-2.service
[root@myw ~]# netstat -lnp|grep java
tcp6 0 0 :::8080 :::* LISTEN 2133/java
tcp6 0 0 :::8081 :::* LISTEN 2187/java
tcp6 0 0 127.0.0.1:8005 :::* LISTEN 2133/java
tcp6 0 0 127.0.0.1:8006 :::* LISTEN 2187/java
tcp6 0 0 :::8009 :::* LISTEN 2133/java
tcp6 0 0 :::8010 :::* LISTEN 2187/java
[root@myw ~]# systemctl status tomcat7.service
?.tomcat7.service - tomcat7 web server
Loaded: loaded (/usr/lib/systemd/system/tomcat7.service; enabled; vendor preset: disabled)
Active: active (running) since Tue 2023-01-31 09:52:20 CST; 31s ago
Process: 2125 ExecStart=/home/tomcat7/bin/startup.sh (code=exited, status=0/SUCCESS)
Main PID: 2133 (java)
CGroup: /system.slice/tomcat7.service
?..2133 /usr/local/jdk18/jre/bin/java -Djava.util.logging.config.file=/home/tomcat7/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djav...
Jan 31 09:52:20 myw systemd[1]: Starting tomcat7 web server...
Jan 31 09:52:20 myw startup.sh[2125]: Tomcat started.
Jan 31 09:52:20 myw systemd[1]: Started tomcat7 web server.
[root@myw ~]# systemctl stop tomcat7.service
[root@myw ~]# systemctl stop tomcat7-2.service
[root@myw ~]# netstat -lnp|grep java
[root@myw ~]#
如此在centos下安装的两个tomcat7都可以实现开机启动了