1、下载Tomcat压缩包。本人下载的版本是:apache-tomcat-8.5.77.tar.gz
2、将压缩包上传到Ubuntu某个目录。
本人存放的目录是 /opt 目录下,
命令解压:
## 解压tomcat压缩包
tar -zxvf apache-tomcat-8.5.77.tar.gz
3、vim打开bin目录的setclasspath.sh文件
vim bin/setclasspath.sh
在文件顶部添加:
#添加 jdk 环境目录
export JAVA_HOME="/opt/jdk1.8.0_321"
export JRE_HOME="/opt/jdk1.8.0_321/jre"
4、在 /etc/systemd/system 目录下创建 tomcat.service 文件
## 创建 tomcat.service
sudo vim /etc/systemd/system/tomcat.service
5、在tomcat.service文件中输入以下内容
## 输入
[Unit]
Description=tomcat
After = network.target remote-fs.target syslog.target
After = mysql.service
[Service]
Restart=always
Type = forking
User=root
Group=root
# JDK 目录(根据自己情况写)
Environment="/opt/jdk1.8.0_321"
Environment="JAVA_OPTS=-Djava.security.egd=file:///dev/urandom -Djava.awt.headless=true"
Environment="CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC"
# (根据自己情况写)
Environment="CATALINA_BASE=/opt/apache-tomcat-8.5.77"
# (根据自己情况写)
Environment="CATALINA_HOME=/opt/apache-tomcat-8.5.77"
# (根据自己情况写)
# PIDFile=/opt/apache-tomcat-8.5.77/tomcat.pid
# (根据自己情况写)
ExecStart=/opt/apache-tomcat-8.5.77/bin/startup.sh
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
privateTmp=true
[Install]
WantedBy=multi-user.target
5、配置修改
## 修改权限
sudo chmod 777 tomcat.service
## 重新加载
sudo systemctl daemon-reload
## 设置开机自启
sudo systemctl enable tomcat
6、服务启动
## 停止
sudo systemctl stop tomcat.service
## 开启
sudo systemctl start tomcat.service
## 重启
sudo systemctl restart tomcat.service
## 查看服务状态
sudo systemctl status tomcat.service
## 查看是否开机自启
sudo systemctl list-unit-files | grep tomcat