在 Linux 安裝 tomcat 環境,並解決訪問manager 403

QIUJUN_FC發表於2020-12-08

全文共兩部分,安裝tomcat和配置tomcat。

安裝tomcat

1.首先安裝好jdk

yum install java-1.8.0-openjdk.x86_64 --安裝jdk1.8
java -version --檢查jdk是否安裝好

2.在官網下載apache-tomcat-7.0.107.tar.gz,並用ftp上傳到伺服器,並解壓。解壓好了即使安裝好了。

tar -xvf apache-tomcat-7.0.107.tar.gz

3.驗證是否安裝成功A。切換到apache-tomcat-7.0.107/bin目錄下,sh startup.sh,成功啟動,如下圖。
成功啟動
4.驗證是否安裝成功B。windows開啟瀏覽器,輸入http://伺服器IP:8080/,成功開啟tomcat主頁,如下圖,說明安裝成功。
安裝成功
配置tomcat

此時,如果我們訪問Server Status,會報錯403。這需要我們完成以下兩步配置:
1.修改配置檔案apache-tomcat-7.0.107/conf/tomcat-users.xml。替換成以下內容:

<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<tomcat-users xmlns="http://tomcat.apache.org/xml"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://tomcat.apache.org/xml tomcat-users.xsd"
              version="1.0">
<!--
  NOTE:  By default, no user is included in the "manager-gui" role required
  to operate the "/manager/html" web application.  If you wish to use this app,
  you must define such a user - the username and password are arbitrary. It is
  strongly recommended that you do NOT use one of the users in the commented out
  section below since they are intended for use with the examples web
  application.
-->
<!--
  NOTE:  The sample user and role entries below are intended for use with the
  examples web application. They are wrapped in a comment and thus are ignored
  when reading this file. If you wish to configure these users for use with the
  examples web application, do not forget to remove the <!.. ..> that surrounds
  them. You will also need to set the passwords to something appropriate.
-->
  <role rolename="tomcat"/>
  <role rolename="manager"/>
  <role rolename="manager-gui"/>
  <role rolename="manager-script" />
  <role rolename="admin-gui"/>
  <user username="tomcat" password="tomcat" roles="tomcat,manager,manager-gui,manager-script,admin-gui" />
</tomcat-users>

2.配置好了tomcat-users.xml,我們此時再訪問Server Status仍然會報錯404,因為apache-tomcat-8.5.59\webapps\manager\META-INF\context.xml配置檔案限制了訪問ip,預設只能環回地址才能訪問,而我們是通過ip遠端訪問,故我們修改這個配置檔案。只需要修改allow後面的內容,配置檔案全文如下:

<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<Context antiResourceLocking="false" privileged="true" >
  <Valve className="org.apache.catalina.valves.RemoteAddrValve"
  allow="\d+\.\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>

相關文章