Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> Linux 教學

CentOS/RHEL 使用 yum 安裝 Tomcat 完整部署指南

編輯:Linux 教學

環境準備

  • 作業系統:CentOS 6.3 / RHEL 6.x
  • 網路通暢、主機基礎配置完成

一鍵安裝 Tomcat 6

yum -y install tomcat6 tomcat6-webapps tomcat6-admin-webapps tomcat6-docs-webapp tomcat6-javadoc

優勢:自動解析相依性,包含 JRE 環境,無需單獨安裝 JDK/JRE。

安裝後目錄結構解析

路徑用途
/etc/tomcat6配置檔目錄 (server.xml, web.xml, tomcat-users.xml)
/var/lib/tomcat6/主程式目錄 (webapps, work, temp, logs 實體)
/usr/share/tomcat6符號連結入口,所有目錄掛載於此

/usr/share/tomcat6 符號連結結構

bin -> /usr/share/tomcat6/bin
conf -> /etc/tomcat6
lib -> /usr/share/java/tomcat6
logs -> /var/log/tomcat6
temp -> /var/cache/tomcat6/temp
webapps -> /var/lib/tomcat6/webapps
work -> /var/cache/tomcat6/work

關鍵配置修改

1. 修改預設埠口 8080 → 80

# 編輯 /etc/tomcat6/server.xml
<Connector port="80" protocol="HTTP/1.1"
           connectionTimeout="20000"
           redirectPort="8443" />

2. 解決 1024 以下埠口權限問題

# 編輯 /etc/tomcat6/tomcat6.conf
# 註解原行
# CONNECTOR_PORT="8080"

# 新增兩行
TOMCAT_USER="root"
CONNECTOR_PORT="80"

安全性警示:以 root 執行 Tomcat 存在安全風險,生產環境建議:

  • 使用 authbind 或 setcap 授權非 root 使用低埠口
  • 前端配置 Nginx/Apache 反向代理至 8080
  • 使用 iptables 端口轉發 iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080

3. 重啟服務驗證

service tomcat6 restart
# 或
systemctl restart tomcat6

驗證:瀏覽器直接輸入 IP 或主機名,無需埠號即可存取。

常用管理操作

# 部署 WAR 檔
cp myapp.war /var/lib/tomcat6/webapps/

# 查看日誌
tail -f /var/log/tomcat6/catalina.out

# 管理介面 (需配置 tomcat-users.xml)
# https://server:80/manager/html

# 設定開機自啟
chkconfig tomcat6 on
# 或
systemctl enable tomcat6

生產環境調優建議

  • 配置 JAVA_OPTS 調整 JVM 記憶體 (-Xms512m -Xmx1024m -XX:MaxPermSize=256m)
  • 啟用 Compression 與 ConnectionTimeout 優化
  • 配置 AccessLogValve 記錄存取日誌
  • 定期清理 work/ 與 logs/ 避免磁碟爆滿
Copyright © Windows教程網 All Rights Reserved