CentOS7安装 Tomcat 8 完整步骤

时间:2021-11-14 05:50:23

原文:https://www.vultr.com/docs/how-to-install-apache-tomcat-8-on-centos-7

Apache Tomcat 是特别为JAVA设计的开源WEB服务器,使用非常广泛。
以下是CentOS7中安装Tomcat 8 的步骤教程:

系统要求

1.CentOS7
2.可登陆的非root的ssh账号

Step1 升级CentOS系统

sudo yum install epel-release
sudo yum update -y && sudo reboot

Step2 安装JAVA

sudo yum install java-1.8.0-openjdk.x86_64

安装完毕后,运行java -version 将输出如下内容:

openjdk version "1.8.0_91"
OpenJDK Runtime Environment (build 1.8.0_91-b14)
OpenJDK 64-Bit Server VM (build 25.91-b14, mixed mode)

Step3 新增Tomcat专用用户

sudo groupadd tomcat
sudo mkdir /opt/tomcat
sudo useradd -s /bin/nologin -g tomcat -d /opt/tomcat tomcat

注意,用户目录是 /opt/tomcat

Step4 下载并安装最新版本Tomcat

cd ~
wget http://www-us.apache.org/dist/tomcat/tomcat-8/v8.0.33/bin/apache-tomcat-8.0.33.tar.gz
sudo tar -zxvf apache-tomcat-8.0.33.tar.gz -C /opt/tomcat --strip-components=1

Step5 权限设置

cd /opt/tomcat
sudo chmod -R tomcat .
sudo chgrp -R tomcat conf
sudo chmod g+rwx conf
sudo chmod g+r conf/*
sudo chown -R tomcat logs/ temp/ webapps/ work/

Step6 为Tomcat创建systemd unit 文件

sudo vi /etc/systemd/system/tomcat.service

内容如下:

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

[Service]
Type=forking

Environment=JAVA_HOME=/usr/lib/jvm/jre
Environment=CATALINA_PID=/opt/tomcat/temp/tomcat.pid
Environment=CATALINA_HOME=/opt/tomcat
Environment=CATALINA_BASE=/opt/tomcat
Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC'
Environment='JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom'

ExecStart=/opt/tomcat/bin/startup.sh
ExecStop=/bin/kill -15 $MAINPID

User=tomcat
Group=tomcat

[Install]
WantedBy=multi-user.target

Step7 安装haveged

sudo yum install haveged
sudo systemctl start haveged.service
sudo systemctl enable haveged.service

Step8 启动Tomcat 并开启防火墙

sudo systemctl start tomcat.service
sudo systemctl enable tomcat.service

sudo firewall-cmd --zone=public --permanent --add-port=8080/tcp
sudo firewall-cmd --reload

访问 http://[Your-Host-IP]:8080 预览是否正常。

Step9 配置Tomcat 管理界面

sudo vi /opt/tomcat/conf/tomcat-users.xml

<tomcat-users ...>...</tomcat-users>中加入一个admin账号:

<user username="yourusername" password="yourpassword" roles="manager-gui,admin-gui"/>

之后重启Tomcat

sudo systemctl restart tomcat.service