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

Linux 環境打造生產級 JSP 應用伺服器:JDK + Apache + Resin 全棧部署實戰

編輯:關於伺服器

在 Linux 上部署 JSP 應用伺服器,核心棧為 JDK(執行環境)+ Apache HTTPD(靜態/反向代理)+ Resin(Servlet/JSP 容器)。本文基於主流發行版,提供標準化、可重現的部署流程,涵蓋編譯安裝、虛擬主機配置、服務化管理、DNS/FTP 協同,並針對生產環境給出調優建議。

軟體版本基線(建議生產環境版本)

組件建議版本獲取來源
JDKOpenJDK 11/17 LTS (Temurin/Eclipse Adoptium)https://adoptium.net/
Apache HTTPD2.4.58+https://httpd.apache.org/
Resin4.0.74+ (Open Source / Pro)https://caucho.com/

階段一:JDK 安裝與環境變數


# 以 Temurin JDK 17 為例
cd /opt
wget https://github.com/adoptium/temurin17-binaries/releases/download/jdk-17.0.10%2B7/OpenJDK17U-jdk_x64_linux_hotspot_17.0.10_7.tar.gz
tar xzf OpenJDK17U-jdk_x64_linux_hotspot_17.0.10_7.tar.gz
ln -s jdk-17.0.10+7 jdk17

# 環境變數 /etc/profile.d/jdk.sh
cat > /etc/profile.d/jdk.sh <<'EOF'
export JAVA_HOME=/opt/jdk17
export PATH=$JAVA_HOME/bin:$PATH
EOF
chmod +x /etc/profile.d/jdk.sh
source /etc/profile.d/jdk.sh
java -version  # 驗證

階段二:Apache HTTPD 編譯安裝(支援動態模組)


cd /usr/local/src
wget https://dlcdn.apache.org/httpd/httpd-2.4.58.tar.gz
tar xzf httpd-2.4.58.tar.gz
cd httpd-2.4.58
# 依賴 APR/APR-Util
wget https://dlcdn.apache.org/apr/apr-1.7.4.tar.gz
wget https://dlcdn.apache.org/apr/apr-util-1.6.3.tar.gz
tar xzf apr-1.7.4.tar.gz -C srclib && mv srclib/apr-1.7.4 srclib/apr
tar xzf apr-util-1.6.3.tar.gz -C srclib && mv srclib/apr-util-1.6.3 srclib/apr-util

./configure --prefix=/usr/local/apache2 \
            --enable-so \
            --enable-ssl \
            --enable-proxy \
            --enable-proxy-http \
            --enable-proxy-ajp \
            --enable-rewrite \
            --enable-headers \
            --enable-deflate \
            --with-included-apr
make -j$(nproc) && make install

階段三:Resin 安裝與 Apache 整合


cd /usr/local/src
wget https://caucho.com/download/resin-4.0.74.tar.gz
tar xzf resin-4.0.74.tar.gz
cd resin-4.0.74
./configure --prefix=/usr/local/resin --with-apache=/usr/local/apache2
make -j$(nproc) && make install

# 環境變數 /etc/profile.d/resin.sh
cat > /etc/profile.d/resin.sh <<'EOF'
export RESIN_HOME=/usr/local/resin
export PATH=$RESIN_HOME/bin:$PATH
EOF
chmod +x /etc/profile.d/resin.sh
source /etc/profile.d/resin.sh

階段四:Apache 虛擬主機與反向代理配置

1. 建立站點目錄結構


mkdir -p /www/www.example.com/{webapp,logs}
chown -R www:www /www/www.example.com

2. 配置 /usr/local/apache2/conf/extra/httpd-vhosts.conf


Listen 80

    ServerName www.example.com
    ServerAlias example.com
    DocumentRoot /www/www.example.com/webapp
    ErrorLog /www/www.example.com/logs/error_log
    CustomLog /www/www.example.com/logs/access_log combined

    # 靜態資源由 Apache 直接服務
    
        Options -Indexes +FollowSymLinks
        AllowOverride None
        Require all granted
        
            ExpiresActive On
            ExpiresDefault "access plus 1 month"
        
    

    # JSP/動態請求代理至 Resin (AJP 協議)
    ProxyPass / ajp://127.0.0.1:8009/
    ProxyPassReverse / ajp://127.0.0.1:8009/

    # 健康檢查端點不代理
    ProxyPass /healthz !

3. 啟用虛擬主機配置


# 在 httpd.conf 末尾確保包含:
Include conf/extra/httpd-vhosts.conf
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_ajp_module modules/mod_proxy_ajp.so

階段五:Resin 配置(/usr/local/resin/conf/resin.xml)



  
    
        
    
  

  
    
    

    
    -Xms2g
    -Xmx4g
    -XX:+UseG1GC
    -XX:MaxGCPauseMillis=200

    
    
    
    
    
      jdbc/myapp
      
        com.mysql.cj.jdbc.Driver
        jdbc:mysql://db-host:3306/myapp?useSSL=false&serverTimezone=Asia/Taipei
        app_user
        StrongP@ssw0rd
        100
        50
      
    
  

階段六:服務化管理與啟動驗證


# Apache systemd
cat > /etc/systemd/system/httpd.service <<'EOF'
[Unit]
Description=Apache HTTP Server
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/apache2/bin/apachectl start
ExecReload=/usr/local/apache2/bin/apachectl graceful
ExecStop=/usr/local/apache2/bin/apachectl stop
KillMode=process
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF

# Resin systemd
cat > /etc/systemd/system/resin.service <<'EOF'
[Unit]
Description=Resin Application Server
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/resin/bin/resin.sh start
ExecStop=/usr/local/resin/bin/resin.sh stop
ExecReload=/usr/local/resin/bin/resin.sh restart
User=www
Group=www
[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl enable --now httpd resin
systemctl status httpd resin

階段七:DNS、FTP 協同與測試驗證

1. BIND DNS 區域配置(授權解析)


# /etc/named.conf 區域定義
zone "example.com" IN {
    type master;
    file "example.com.zone";
};
# /var/named/example.com.zone
$TTL 3600
@   IN  SOA ns1.example.com. admin.example.com. (2024010101 3600 1800 604800 86400)
    IN  NS  ns1.example.com.
    IN  A   203.0.113.10
www IN  A   203.0.113.10
ns1 IN  A   203.0.113.10

2. vsftpd 虛擬用戶映射站點目錄


# 建立系統用戶
useradd -d /www/www.example.com -s /sbin/nologin www_example
# 設定密碼或使用 PAM 虛擬用戶
# 配置 vsftpd.chroot_local_user=YES, user_sub_token=$USER, local_root=/www/$USER

3. 測試 JSP 執行


cat > /www/www.example.com/webapp/test.jsp <<'EOF'
<%@ page contentType="text/html;charset=UTF-8" %>

JSP Test: 1+1=<%=1+1%>

EOF curl -H "Host: www.example.com" https://localhost/test.jsp # 預期輸出:

JSP Test: 1+1=2

生產環境調優清單

  • Apache:啟用 mod_deflate 壓縮、mod_expires 快取、mod_security WAF、HTTP/2(Protocols h2 http/1.1)
  • Resin:配置 Session 複寫(叢集模式)、JVM 監控(JMX Exporter + Prometheus)、日誌輪轉(logrotate)
  • OS:調整 ulimit -n 65535、net.core.somaxconn=65535、關閉 Transparent Huge Pages、啟用 Swappiness=1
  • 安全:僅開放 80/443/22(SSH 金鑰登入),Resin 管理埠 8080 僅內網/VPN 訪問,定期掃描 CVE 並升級組件

遵循本文標準化流程,您將獲得一個穩定、可觀測、易維護的生產級 JSP 應用伺服器環境,為業務快速迭代奠定堅實基礎設施。

Copyright © Windows教程網 All Rights Reserved