CentOS7安装 Tomcat 8 完整步骤

时间:2021-08-18 15:24:04



CentOS7安装 Tomcat 8 完整步骤

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

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

:若使用云主机,如阿里云ECS或百度云BCC等,注意相关端口的入口规则配置。

系统要求

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

Step1 升级CentOS系统

sudo yum install epel-release
sudo yum update -y && sudo reboot
  
  
  
  • 1
  • 2

Step2 安装JAVA

sudo yum install java-1.8.0-openjdk.x86_64
  
  
  
  • 1

安装完毕后,运行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)
  
  
  
  • 1
  • 2
  • 3

Step3 新增Tomcat专用用户

sudo groupadd tomcat
sudo useradd -s /bin/nologin -g tomcat -d /opt/tomcat tomcat
  
  
  
  • 1
  • 2
  • 3

注意,用户目录是 /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
  
  
  
  • 1
  • 2
  • 3

Step5 权限设置

cd /opt/tomcat
sudo chown -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/
  
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

Step6 为Tomcat创建systemd unit 文件

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

内容如下:

[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
  
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

Step7 安装haveged

sudo yum install haveged
sudo systemctl start haveged.service
sudo systemctl enable haveged.service
  
  
  
  • 1
  • 2
  • 3

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
  
  
  
  • 1
  • 2
  • 3
  • 4
  • 5

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

Step9 配置Tomcat 管理界面

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

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

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

之后重启Tomcat

sudo systemctl restart tomcat.service
  
  
  
  • 1
    <div class="article-bar-bottom">
                    <div class="tags-box artic-tag-box">
        <span class="label">文章标签:</span>
                    <a class="tag-link" href="http://so.csdn.net/so/search/s.do?q=tomcat&amp;t=blog" target="_blank">tomcat                     </a><a class="tag-link" href="http://so.csdn.net/so/search/s.do?q=web服务器&amp;t=blog" target="_blank">web服务器                     </a><a class="tag-link" href="http://so.csdn.net/so/search/s.do?q=centos&amp;t=blog" target="_blank">centos                     </a>
    </div>
                        </div>

<!-- !empty($pre_next_article[0]) -->
    </div>