Centos下Tomcat8安装

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 检查输出信息确认jdk安装成功

step3:新增Tomcat专用用户

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

step4:下载并安装tomcat

cd ~
wget http://www-us.apache.org/dist/tomcat/tomcat-8/v8.5.33/bin/apache-tomcat-8.5.33.tar

sudo tar -xvf apache-tomcat-8*tar -C /opt/tomcat --strip-components=1

step5:设置权限

cd /opt/tomcat

# 给tomcat整个安装目录组的所有权:
sudo chgrp -R tomcat /opt/tomcat

# 该tomcat组读取对该conf目录及其所有内容的访问权限,并执行对该目录本身的访问:
sudo chmod -R g+r conf
sudo chmod g+x conf

# 让tomcat用户的拥有者webapps,work,temp,和logs目录:
sudo chown -R tomcat webapps/ work/ temp/ logs/

step6:安装系统单元文件

因为我们希望能够将Tomcat作为服务运行,所以我们将设置一个Tomcat Systemd单元文件。

通过运行以下命令创建并打开单元文件: sudo vi /etc/systemd/system/tomcat.service

粘贴到以下脚本中。您可能还想修改以下内容中指定的内存分配设置CATALINA_OPTS:

# Systemd unit file for tomcat
[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
UMask=0007
RestartSec=10
Restart=always

[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 systemctl start firewalld
sudo firewall-cmd --zone=public --permanent --add-port=8080/tcp
sudo firewall-cmd --reload

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

安装tomcat,配置好tomcat环境变量以后,访问manager app页面,出现403 Access Denied错误,解决的方法如下:

首先在conf/tomcat-users.xml文件里面,在前面添加如下代码:

<role rolename="manager-gui"/>
<user password="tomcat" roles="manager-gui" username="123321"/>

然后重启tomcat,重新访问manager app页面,如果此时仍然出现403 Access Denied错误,那么就是访问的ip地受到了限制,

我们打开/webapps/manager/META-INF/目录下context.xml文件,不是conf/目录下的context.xml文件,一定不要搞错了

<Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1|\d+\.\d+\.\d+\.\d+" />


评论(0 ) 点赞(19)


暂未登录,请登录之后发表评论。 QQ