在 Linux 上部署 JSP 應用伺服器,核心棧為 JDK(執行環境)+ Apache HTTPD(靜態/反向代理)+ Resin(Servlet/JSP 容器)。本文基於主流發行版,提供標準化、可重現的部署流程,涵蓋編譯安裝、虛擬主機配置、服務化管理、DNS/FTP 協同,並針對生產環境給出調優建議。
| 組件 | 建議版本 | 獲取來源 |
|---|---|---|
| JDK | OpenJDK 11/17 LTS (Temurin/Eclipse Adoptium) | https://adoptium.net/ |
| Apache HTTPD | 2.4.58+ | https://httpd.apache.org/ |
| Resin | 4.0.74+ (Open Source / Pro) | https://caucho.com/ |
# 以 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 # 驗證
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
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
mkdir -p /www/www.example.com/{webapp,logs}
chown -R www:www /www/www.example.com
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 !
# 在 httpd.conf 末尾確保包含:
Include conf/extra/httpd-vhosts.conf
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
-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
# /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
# 建立系統用戶
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
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
mod_deflate 壓縮、mod_expires 快取、mod_security WAF、HTTP/2(Protocols h2 http/1.1)ulimit -n 65535、net.core.somaxconn=65535、關閉 Transparent Huge Pages、啟用 Swappiness=1遵循本文標準化流程,您將獲得一個穩定、可觀測、易維護的生產級 JSP 應用伺服器環境,為業務快速迭代奠定堅實基礎設施。