本文参考了以下博客内容
如何在Ubuntu 18.04上安装Tomcat 8.5.31
tomcat(一):一次解决tomcat9无法登陆控制界面的经历
1.需要提前安装好JDK 和配置好JAVA_HOME
2.创建tomcat 用户
sudo useradd -m -U -d /opt/tomcat -s /bin/false tomcat
3.下载Tomcat
使用wget 来下载 ,如果没有安装过,用一下命令安装一下
sudo apt install wget
从apacha tomcat 官网下载页面 获得tomcat9的下载链接
wget http://mirrors.hust.edu.cn/apache/tomcat/tomcat-9/v9.0.8/bin/apache-tomcat-9.0.8.tar.gz
解压tomcat 并移动到 /opt/tomcat 目录
移动到 /opt/tomcat 目录
sudo mv apache-tomcat-*/ /opt/tomcat/
4.设置权限
5.创建 systemd 服务文件
sudo vim /etc/systemd/system/tomcat.service
在文件中写入一下内容(JAVA_HOME 替换成自己的路径)
[Unit]
Description=Apache Tomcat Web Application Container
After=network.target
[Service]
Type=forking
Environment=JAVA_HOME=JAVA_HOME内容/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=/opt/tomcat/bin/shutdown.sh
User=tomcat
Group=tomcat
UMask=0007
RestartSec=10
Restart=always
[Install]
WantedBy=multi-user.target
使用服务启动tomcat 设置开机启动
# 启动Tomcat服务
sudo systemctl start tomcat
# 查看Tomcat服务的状态
sudo systemctl status tomcat
# 关闭Tomcat服务
sudo systemctl stop tomcat
# 设置Tomcat开机自启
sudo systemctl enable tomcat
# 取消开机自启
sudo systemctl disable tomcat
6 配置tomcat-user.xml 文件
cd /opt/tomcat/conf/
# 编辑 tomcat-users.xml 文件
sudo vim tomcat-users.xml
写入以下内容
<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<role rolename="manager-jmx"/>
<role rolename="manager-status"/>
<user username="admin" password="111111" roles="manager-gui,manager-script,manager-jmx,manager-status"/>
修改 webapps/manager/META-INF/context.xml 文件
修改前:
<Context antiResourceLocking="false" privileged="true" >
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />
<Manager sessionAttributeValueClassNameFilter="java\.lang\.(?:Boolean|Integer|Long|Number|String)|org\.apache\.catalina\.filters\.CsrfPreventionFilter\$LruCache(?:\$1)?|java\.util\.(?:Linked)?HashMap"/>
</Context>
修改后:
<Context antiResourceLocking="false" privileged="true" >
<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+" />
<Manager sessionAttributeValueClassNameFilter="java\.lang\.(?:Boolean|Integer|Long|Number|String)|org\.apache\.catalina\.filters\.CsrfPreventionFilter\$LruCache(?:\$1)?|java\.util\.(?:Linked)?HashMap"/>
</Context>
添加了 正则表达式 d+\.\d+\.\d+\.\d+
匹配任意IP地址
7.重启Tomcat , 访问正常
全文完