Java环境搭建

安装Java

Java版本: java 1.8 (version 1.8.0_281)

下载地址
https://www.oracle.com/cn/java/technologies/javase-downloads.html
https://www.oracle.com/cn/java/technologies/javase/javase-jdk8-downloads.html (java8)

解压到目录: /usr/local/java8

设置环境变量
编辑文件/etc/profile或创建文件/etc/profile.d/java.sh

export JAVA_HOME=/usr/local/java8
export JRE_HOME=$JAVA_HOME/jre
export CLASSPATH=$JAVA_HOME/lib:$JRE_HOME/lib:$CLASSPATH
export PATH=${JAVA_HOME}/bin:$PATH

创建软链

ln -s /usr/local/java8/bin/java /usr/bin/java

检查

java -version

安装Tomcat

Tomcat版本: apache-tomcat-8.5.64

下载地址
https://tomcat.apache.org/download-80.cgi
https://mirror-hk.koddos.net/apache/tomcat/tomcat-8/v8.5.65/bin/apache-tomcat-8.5.65.tar.gz

解压到目录: /usr/local/tomcat-8.5.65

Tomcat指定JDK配置
如果系统中需要部署多Tomcat或多JDK,需要配置如下

#修改catalina.sh
vi /usr/tomcat/tomcat8/bin/catalina.sh
#增加如下配置:
export JAVA_HOME=/usr/java/jdk1.8.0_151
#修改setclasspath.sh
vi /usr/tomcat/tomcat8/bin/setclasspath.sh
#增加如下配置:
export JAVA_HOME=/usr/java/jdk1.8.0_151

Tomcat端口修改与防火墙配置

#修改server.xml(修改端口)
vim /usr/tomcat/tomcat8/conf/server.xml
<Connector port="8080" protocol="HTTP/1.1"
			connectionTimeout="20000"
			redirectPort="8443" />

系统设置

#开放8080端口
firewall-cmd --add-port=8080/tcp --permanent && firewall-cmd --reload
#重新加载防火墙规则
firewall-cmd --reload

创建运行用户

useradd -r -M -s /sbin/nologin tomcat
chown tomcat:tomcat -R /usr/local/tomcat

配置自动和自动启动
创建配置文件: /etc/systemd/system/tomcat8.service

[Unit]
Description=Apache Tomcat Web Application Container
After=syslog.target network.target

[Service]
User=tomcat
Group=tomcat
Type=forking
PIDFile=/usr/local/tomcat-8.5.65/temp/tomcat.pid
ExecStart=/usr/local/tomcat-8.5.65/bin/startup.sh
ExecReload=/usr/local/tomcat-8.5.65/bin/startup.sh
ExecStop=/usr/local/tomcat-8.5.65/bin/shutdown.sh
Restart=on-failure

[Install]
WantedBy=multi-user.target

启动tomcat

systemctl daemon-reload
systemctl start tomcat
systemctl enable tomcat # 设置开机自动启动