一 JDK环境安装
1.1 Tomcat是用Java语言编写的,需要运行在Java虚拟机上,所以先安装JDK,以提供运行环境。
sudo apt install openjdk-8-jdk-headless
1.2 设置JDK环境变量
设置用户环境变量
sudo vi ~/.bashrc
按下i进入编辑模式,然后再尾部加上JDK路径
#java
export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
export CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib
export PATH=$JAVA_HOME/bin:$PATH
使其生效
sudo source ~/.bashrc
1.3 检查是否设置成功
root@localhost:~# java -version
openjdk version "1.8.0_181"
OpenJDK Runtime Environment (build 1.8.0_181-8u181-b13-0ubuntu0.16.04.1-b13)
OpenJDK 64-Bit Server VM (build 25.181-b13, mixed mode)
二 Tomcat服务器安装
2.1 当前最新版本为apache-tomcat-9.0.11.tar.gz,可以从官网下载,地址:https://tomcat.apache.org/download-90.cgi,此外,可以通过镜像下载:
wget https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-9/v9.0.11/bin/apache-tomcat-9.0.11.tar.gz
2.2 解压,进入bin目录,执行./startup.sh,启动Tomcat,若要停止Tomcat,可以执行./shutdown.sh.
tar -xvf apache-tomcat-9.0.11.tar.gz
cd apache-tomcat-9.0.11/bin
./startup.sh
2.3 Tomat Http默认端口是8080,若需要修改端口,在Tomcat解压目录下,进入conf目录,打开server.xml,修改8080端口即可(端口范围:1024-65535)。
62 <!-- A "Connector" represents an endpoint by which requests are received
63 and responses are returned. Documentation at :
64 Java HTTP Connector: /docs/config/http.html
65 Java AJP Connector: /docs/config/ajp.html
66 APR (HTTP/AJP) Connector: /docs/apr.html
67 Define a non-SSL/TLS HTTP/1.1 Connector on port 080
68 -->
69 <Connector port="8080" protocol="HTTP/1.1"
70 connectionTimeout="20000"
71 redirectPort="8443" />
2.4 查看系统防火墙是否设置了允许端口访问
root@localhost:~# iptables -L -n
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
2.5 添加访问端口到防火墙列表中
iptables -I INPUT -p tcp --dport 8080 -j ACCEPT
2.6 查询是否设置成功,在Chain INPUT(policy ACCEPT)中看到,已经添加了一条允许8080端口访问的记录。
root@localhost:~# iptables -L -n
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:8080
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
2.7 通过浏览器访问Tomcat自带的jsp页面,在浏览器地址栏输入http://192.168.10.204:8080,即可成功访问。
三 Tomcat服务器开机自启动
3.1 在/etc/init.d目录下新建tomcat文件
sudo cd /etc/init.d
sudo vi tomcat
3.2 添加启动、关闭、重启的shell脚本
#!/bin/sh
### BEGIN INIT INFO
# Provides: tomcat
# Required-Start: $remote_fs $network
# Required-Stop: $remote_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short Description: the tomcat Java Application Server
### END INIT INFO
RETVAL=0
export JRE_HOME=/usr/lib/jvm/java-8-openjdk-amd64
export CATALINA_HOME=/home/xxx/apache-tomcat-9.0.11 # xxx为自己的用户名
export CATALINA_BASE=/home/xxx/apache-tomcat-9.0.11 # xxx为自己的用户名
start()
{
if [ -f $CATALINA_HOME/bin/startup.sh ]
then
echo $"Starting Tomcat"
$CATALINA_HOME/bin/startup.sh
RETVAL=$?
echo "OK"
return $RETVAL
fi
}
stop()
{
if [ -f $CATALINA_HOME/bin/shutdown.sh ]
then
echo $"Stopping Tomcat"
$CATALINA_HOME/bin/shutdown.sh
RETVAL=$?
sleep 3
ps -fwwu tomcat | grep apache-tomcat | grep -v grep | grep -v PID | awk '{print $2}'| xargs kill -9
echo "OK"
return $RETVAL
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
echo $"Restarting Tomcat"
$0 stop
sleep 3
$0 start
;;
*)
echo $"Usage:$0{start|stop|restart}"
exit 1
;;
esac
exit $RETVAL
3.3 保存退出,授予tomcat文件执行权限
chmod +x tomcat
3.4 使tomcat服务生效,并使能开机自启动,然后reboot重启,即可Tomcat开机自启动生效.
sudo systemctl daemon-reload #对于新创建的unit文件或修改了的unit文件,要通知systemd重载此配置文件;
sudo systemctl enable tomcat #使能开机自启动
sudo reboot
3.5 重启后,执行systemctl status tomcat看看tomcat服务的状态。
root@localhost:~# systemctl status tomcat
● tomcat.service
Loaded: loaded (/etc/init.d/tomcat; bad; vendor preset: enabled)
Active: active (running) since Thu 2018-08-23 21:54:25 PDT; 44s ago
Docs: man:systemd-sysv-generator(8)
Process: 811 ExecStart=/etc/init.d/tomcat start (code=exited, status=0/SUCCESS)
CGroup: /system.slice/tomcat.service
└─832 /usr/lib/jvm/java-8-openjdk-amd64/bin/java -Djava.util.logging.config.file=/home/localhost/apache-tomcat-9.0.11/conf/logging.propertie
Aug 23 21:54:24 localhost systemd[1]: Starting tomcat.service...
Aug 23 21:54:25 localhost tomcat[811]: $Starting Tomcat
Aug 23 21:54:25 localhost tomcat[811]: Tomcat started.
Aug 23 21:54:25 localhost tomcat[811]: OK
Aug 23 21:54:25 localhost systemd[1]: Started tomcat.service.